You need to sign in to do that
Don't have an account?
Saurabh Sood 12
Unable to get data from apex controller while I'm setting parameters with the help of setParams of recordId, I'm getting null recordId in apex controller
Aura App
<aura:application extends="force:slds">
<c:myContactCmp recordId='0017F00000S7UjN'/>
</aura:application>
Componet
<aura:component implements="flexipage:availableForRecordHome,force:hasRecordId" controller="myContactController" access="global" >
<aura:handler name="init" action="{!c.getContactList}" value="{!this}" />
<aura:attribute name="contactList" type="List"/>
</aura:component>
Component controller
({
getContactList: function(component, event, handler) {
var action = component.get("c.ContactsList");
//var accId = component.get("v.recordId");
alert('acc id '+ component.get("v.recordId"));
/*action.setParams({
accId : accId
});*/
action.setParams({ AccountId : component.get("v.recordId") });
action.setCallback(this, function(response){
var state = response.getState();
if(state == 'SUCCESS'){
var contactList = response.getReturnValue();
//alert("From server: " + contactList);
//console.log("From server: " + contactList);
component.set("v.contactList", contactList);
}else{
alert('alert in getting data');
}
});
$A.enqueueAction(action);
}
})
Apex Controller
It's shows null recordId
public class myContactController {
@AuraEnabled
public static List<Contact> ContactsList(Id accountId){
system.debug('acc id is '+accountId);
List<Contact> contacts = [SELECT ID,name,AccountId,Phone,Email from Contact where accountId =: accountId];
return contacts;
}
}
<aura:application extends="force:slds">
<c:myContactCmp recordId='0017F00000S7UjN'/>
</aura:application>
Componet
<aura:component implements="flexipage:availableForRecordHome,force:hasRecordId" controller="myContactController" access="global" >
<aura:handler name="init" action="{!c.getContactList}" value="{!this}" />
<aura:attribute name="contactList" type="List"/>
</aura:component>
Component controller
({
getContactList: function(component, event, handler) {
var action = component.get("c.ContactsList");
//var accId = component.get("v.recordId");
alert('acc id '+ component.get("v.recordId"));
/*action.setParams({
accId : accId
});*/
action.setParams({ AccountId : component.get("v.recordId") });
action.setCallback(this, function(response){
var state = response.getState();
if(state == 'SUCCESS'){
var contactList = response.getReturnValue();
//alert("From server: " + contactList);
//console.log("From server: " + contactList);
component.set("v.contactList", contactList);
}else{
alert('alert in getting data');
}
});
$A.enqueueAction(action);
}
})
Apex Controller
It's shows null recordId
public class myContactController {
@AuraEnabled
public static List<Contact> ContactsList(Id accountId){
system.debug('acc id is '+accountId);
List<Contact> contacts = [SELECT ID,name,AccountId,Phone,Email from Contact where accountId =: accountId];
return contacts;
}
}
While setting parameter from Component controller you used:-
action.setParams({ AccountId : component.get("v.recordId") });
and in 'myContactController' you defined property as -
public static List<Contact> ContactsList(Id accountId){}
So the problem is too small but that creating impact on getting recordId.
You just need to make "AccountId" as "accountId".
Replace - action.setParams({ AccountId : component.get("v.recordId") });
with
action.setParams({ accountId: component.get("v.recordId") });
Let me know in case of any concerns.
Please mark this answer as the solution/ best answer if it solves your purpose so that it can help other community members.
Thanks,
Pramod Nishane
Salesforce Consultant
Varasi LLC
www.varasi.com