How to get current record page Id in APEX class

get current record id salesforce
how to get current record page id in apex class

@api recordId property in lighting component will give you record id of current record page .

For example – if your using custom lighting component on account record page then using @api recordId you can get record id of current account record in your LWC .

Now in order to send that recordId to apex class follow me :

Here I am assuming that your having the lightning component and apex class with method ,where your trying to send current recordId from lighting component .

Sample APEX method

@AuraEnabled(cacheable = true)
public static void getCurentRecordId(String recordId){ 
    System.debug('recordid '+ recordId)
}

Now import this method and declare @api recordId into your lighting component .

Here is code of LWC JavaScript file , where we call that getCurentRecordId method into connectedCallback which will send recordId to our apex method.

Sample LWC JavaScript Code

import { LightningElement } from 'lwc';
import {api , track  } from 'lwc'
import getCurentRecordId from '@salesforce/apex/GetRecordId.getCurentRecordId'

export default class GetRecordID extends LightningElement {
    @api recordId;  
    @track currentRecordid = '';
    connectedCallback() { 
        this.currentRecordid = this.recordId;
        curentRecordId(recordId : this.currentRecordid)
        .then((result) => {
            if (result) {
               console.log('Yo yo....found')
            } else {
                console.log('opps something is wrong')
            }
        }).catch((error) => {
            console.log('Catch Error')
        });
    }
}

this how simple it is to send current record page Id your apex method , hope we help you to solve your issue , if you have any question and feel to ask , we are happy to help you.

Thank you…..

Leave a Reply