You need to sign in to do that
Don't have an account?

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
Here is the turotial I am referring to:https://developer.salesforce.com/trailhead/project/slds-lightning-components-workshop/slds-lc-6
https://developer.salesforce.com/blogs/developer-relations/2015/11/building-context-aware-lightning-components.html
All Answers
https://developer.salesforce.com/blogs/developer-relations/2015/11/building-context-aware-lightning-components.html
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.
In the code from "Building Context-Aware Lightning Components" there is the following in the client-side controller: For this code to work in my Javascript Helper, I had to remove the quotes around "accountId" which resulted in:
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>
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 });
Check here (https://devfacts.com/id-from-url-with-apex-and-lightning-component/)