function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ 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}"  />
</aura:component>

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({ });
        $A.enqueueAction(save_action);


        
    }
})

Apex controller:

public class AccountFieldupdats {
 @AuraEnabled
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();
     ACC.id=Act_list[0].id;
     ACC.Call_List__c=False;
    update ACC;

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

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.

Component:
<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}"  />
</aura:component>

Controller:
({
    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") {
                $A.get('e.force:refreshView').fire();
            }
            else if (state === "ERROR") {
                var errors = response.getError();
                if (errors) {
                    if (errors[0] && errors[0].message) {
                        console.log("Error message: " + 
                                    errors[0].message);
                    }
                } 
                else {
                    console.log("Unknown Error");
                }
            }
        });
        $A.enqueueAction(action);
    },
    
    isRefreshed: function(component, event, helper) {
        location.reload();
    },
})

Apex:
public class UpdateAccCheckboxC {

    @AuraEnabled
    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

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi,

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.

Component:
<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}"  />
</aura:component>

Controller:
({
    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") {
                $A.get('e.force:refreshView').fire();
            }
            else if (state === "ERROR") {
                var errors = response.getError();
                if (errors) {
                    if (errors[0] && errors[0].message) {
                        console.log("Error message: " + 
                                    errors[0].message);
                    }
                } 
                else {
                    console.log("Unknown Error");
                }
            }
        });
        $A.enqueueAction(action);
    },
    
    isRefreshed: function(component, event, helper) {
        location.reload();
    },
})

Apex:
public class UpdateAccCheckboxC {

    @AuraEnabled
    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
This was selected as the best answer
Sophia GSophia G
Hi Khan, 

How would I add this into my existing string?

 @AuraEnabled
    public static void updateRecord(List <String> lstRecordId) {
        
        List<Opportunity> lstUpdate = new List<Opportunity>();
        
        for(Opportunity Opp : [SELECT Id, Name, SyncedQuoteId, RecordTypeId, Trip_File_LEAD__c FROM Opportunity WHERE Id IN : lstRecordId]){
        lstUpdate.add(Opp);
        }
        
        if(lstUpdate.size() > 0){
            update lstUpdate;

Thank you!