+ Start a Discussion
The new LearnerThe new Learner 

How to fetch current Record id in lighting and pass to controller

HI Experts,

I am new to Lighting , i have requirment i need to update Account fields by creating button ,  i have created component and helper and controller its working fine when i pass id in my controller but i need to make my code dyanmic means what ever account record i pick randomly that account field has to update. can anyone help me pls below is my code. Thanks in advace

Component :

<aura:component controller="AccountFieldupdats" implements="force:lightningQuickActionWithoutHeader,flexipage:availableForRecordHome,force:hasRecordId,force:hasSObjectName">
    <!--<aura:attribute name="updateCheckBox" type="Account" default="{'sobjectType':'Account'}"/>-->
    <lightning:button variant="brand" label="Update A Checkbox" onclick="{!c.updateCheck11}"  />

Controller :

    updateCheck11  : function(component, event, helper) {
        alert('inside controller');
        helper.updateCheck11_helper(component, event, helper);

Helper :
    updateCheck11_helper  : function(component, event, helper) {
        alert('inside helper');
        var save_action = component.get("c.updateCheck");
        save_action.setParams({ });


Apex controller:

public class AccountFieldupdats {
public static void updateCheck(){
list<Account> Act_list = new list<Account>();
    Act_list = [select id,Call_List__c from Account where id=''];
     Account ACC =new Account();
    update ACC;

Best Answer chosen by The new Learner
Khan AnasKhan Anas (Salesforce Developers) 

I trust you are doing very well.

You'll need to get the record Id in the lightning component, which is what the force:hasRecordId interface is for. It is automatically initialized from the context if you declare the interface force:hasRecordId.

Please try the below code. Kindly modify the code as per your requirement.

<aura:component controller="UpdateAccCheckboxC"
                implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
    <aura:handler event="force:refreshView" action="{!c.isRefreshed}" />
    <lightning:button variant="brand" label="Update A Checkbox" onclick="{!c.updateCheck}"  />

    updateCheck : function(component, event, helper) {
        var rid = component.get("v.recordId");
        var action = component.get("c.updateChk");
        action.setParams({key : rid});
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
            else if (state === "ERROR") {
                var errors = response.getError();
                if (errors) {
                    if (errors[0] && errors[0].message) {
                        console.log("Error message: " + 
                else {
                    console.log("Unknown Error");
    isRefreshed: function(component, event, helper) {

public class UpdateAccCheckboxC {

    public static void updateChk(String key){
        Account acc = [SELECT Id, Name, Cb__c FROM Account WHERE Id=:key];
        acc.Cb__c = true;
        UPDATE acc;

Finally, add this component on the Account record page.

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Thanks and Regards,
Khan Anas