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
Greg CooganGreg Coogan 

Get Current Record ID in Lightning Component's Apex Controller

I am developing a Lightning Component based off the tutorial for the Account List Lightning Component. In the Apex Controller, I want to modify the query to only select records related to the currently viewed record. I tried using ApexPages.currentPage().getParameters().get('id') but it didn't work. The user would be viewing the details of a particular Event when this code would run. How can I get the current Event record ID in the Apex Controller of a Lightning Component?

Here is the turotial I am referring to:https://developer.salesforce.com/trailhead/project/slds-lightning-components-workshop/slds-lc-6
Best Answer chosen by Greg Coogan
bob_buzzardbob_buzzard
The Apex controller has no knowledge of the request that went to the browser, all it has is information that has been passed to the method call. You'll need to get the record in the lightning component, which is what the 'force:hasRecordId' interface is for:

https://developer.salesforce.com/blogs/developer-relations/2015/11/building-context-aware-lightning-components.html

All Answers

bob_buzzardbob_buzzard
The Apex controller has no knowledge of the request that went to the browser, all it has is information that has been passed to the method call. You'll need to get the record in the lightning component, which is what the 'force:hasRecordId' interface is for:

https://developer.salesforce.com/blogs/developer-relations/2015/11/building-context-aware-lightning-components.html
This was selected as the best answer
Greg CooganGreg Coogan
Bob,
Thanks for the info about the Apex controller having no knowledge and it needs to be passed the RecordId. That helps narrow down the articles I've read. I've used the article you mentioned as a guide, as well as the following:
https://developer.salesforce.com/forums/?id=906F0000000BX1nIAG
https://developer.salesforce.com/forums/?id=906F0000000B11GIAS
https://developer.salesforce.com/blogs/developer-relations/2015/03/lightning-components.html

In the "Building Context-Aware Lightning Components" article, my Javascript Helper and Javascript Controller both have code similar to the Client-side controller source code from that article. I have my code set up this way because I was following the Salesforce tutorial.

I still seem to not be passing the RecordId. If you have time to look at the code, I can post it.
Greg CooganGreg Coogan
I got it to work!

In the code from "Building Context-Aware Lightning Components" there is the following in the client-side controller:
action.setParams({
            "accountId": component.get("v.recordId")
        });
For this code to work in my Javascript Helper, I had to remove the quotes around "accountId" which resulted in:
action.setParams({
        accountId : component.get("v.recordId")
    });
udaykumar kudaykumar k
HI Greg and Bob I am facing challenge here , I am not getting the id on to the controller...My code is as below..
Component:

<aura:component controller="dcontroller" implements='force:hasRecordId'>
    
    <link href="assets/styles/salesforce-lightning-design-system-vf.css" rel="stylesheet"/>
  <aura:handler name="init" value="{!this}" action="{!c.doInit}" /> 
    <aura:attribute name="recordId" type="Id" />

    <ui:inputSelect class="slds-col slds-media slds-media--center" aura:id="InputSelectDynamic"/>
</aura:component>

Controller:
public class dcontroller {
    
    @AuraEnabled
    public static List<String> getpickval(ID accId) {
        List<String> options = new List<String>();
       
        List<Contact> contactResult = [Select Name from Contact where AccountID =:accId];
         options.add('Contact ('+ contactResult.size() + ')');
        for (Contact f: contactResult) {
            options.add(f.Name);
        }       
        return options;
    }
}

Helper.js:

({
    doInit : function(component, event, helper) {
        var action = component.get("c.getpickval");
action.setParams({
            accId: component.get("v.recordId")
        });
        var inputsel = component.find("InputSelectDynamic");
        var opts=[];
        action.setCallback(this, function(a) {
            for(var i=0;i< a.getReturnValue().length;i++){
                opts.push({"class": "optionClass", label: a.getReturnValue()[i], value: a.getReturnValue()[i]});
            }
            inputsel.set("v.options", opts);
        });
        $A.enqueueAction(action); 
        }
})

App:

<aura:application access="GLOBAL" extends="ltng:outApp" >
     <aura:dependency resource="c:dComp"/>
</aura:application>

VF Page:

<apex:includeScript value="/lightning/lightning.out.js" />
<div id="lightning" />
    <script>
            $Lightning.use("c:dApp", function() {
            $Lightning.createComponent(
                "c:dComp",
                {},
                "InputSelectDynamic",
                function(cmp) {
                    console.log("Component created!");
                    console.log(cmp);
                });
            });
            
          
    </script>
    <div id="InputSelectDynamic"></div>
Igor Androsov 22Igor Androsov 22
To add here I had a problem passing several parameters to APEX method from Component, had to change helper.js code to have a single setParams call with multiple values JSON. Making several calls to setParms do not pass values
This is NULL value for record ID
       action.setParams({ "contactId" : recTempId, "community" : comName }); 
       action.setParams({ "community" : comName }); 

This one works well
        var recTempId = component.get("v.recordId");
        var comName = component.get("v.selectedSchool"); 
action.setParams({ "contactId" : recTempId, "community" : comName });
Kumaresan S 5Kumaresan S 5
I hope it will be useful for you

Check here (https://devfacts.com/id-from-url-with-apex-and-lightning-component/)