Sunday, January 3, 2021

How to pass recordId from LWC to apex controller?

In this blog post we are going to learn "How to pass recordId from LWC to apex controller?"



    <div >

        <p>Displaying Account Information</p>

        <template if:true={accountRecord}>

            <template for:each={accountRecord} for:item="item" for:index="index">

                <p key={item.Id}>

                   Name: {item.Name}


                    Industry: {item.Industry}




        <template if:true={error}>

            Some error occured.






import { LightningElement,wire,track,api } from 'lwc';

import getAccountData from '@salesforce/apex/getAccountRecord.getAccountRecordMethod';


export default class WireaFunctionExample extends LightningElement {

    @api recordId;

    @track accountRecord;

    @track error;



    @wire(getAccountData,{ recordIdAccount: '$recordId'}) 

    accountsData({ error, data }) {

        if (data) {

            //console.log('RecordId is'+recordId);

            this.accountRecord = data;

            this.error = undefined;

        } else if (error) {

            //console.log('Error block');

            this.error = error;

            this.accountRecord = undefined;





<?xml version="1.0" encoding="UTF-8"?>

<LightningComponentBundle xmlns="">








public with sharing class getAccountRecord {



    public static List<Account> getAccountRecordMethod(String recordIdAccount) {

    List<Account> accList=[SELECT Id, Name, Industry

        FROM Account

        Where id=:recordIdAccount];

        return accList;




How to pass recordId from LWC to apex controller?

No comments:

Post a Comment