I have a standard Aura Lightning Component with onChange handler, Javascript controller and Apex controller but even though everything seems to be fine, the server controller method is never called.
The lightning component handler:
<aura:handler name="change" value="{!v.contact.AccountId}" action="{!c.autoConsBP}"/>
and Javascript controller method:
autoConsBP: function (component, event, helper) {
var id = component.get("v.contact.AccountId");
var action = component.get("c.populateConsBPField");
action.setParams({
"accountId": id
});
action.setCallback(this, function (response) {
var state = response.getState();
if (state === "SUCCESS") {
console.log('Returned value: ' + response.getReturnValue());
}
if (state === "ERROR") {
console.log('Error');
}
});
$A.enqueueAction(action);
}
in Apex controller there is a method populateConsBPField(String accountId)
:
@AuraEnabled
public static List<String> populateConsBPField(String accountId) {
System.debug('Entering');
Account fromAccount = [Select Id, Consolidated_Business_Partner__c, Consolidated_Business_Partner__r.Name from Account where Id = :accountId];
System.debug('Account: '+ fromAccount);
List<String> consBP = new List<String>();
consBP.add(fromAccount.Consolidated_Business_Partner__c);
consBP.add(fromAccount.Consolidated_Business_Partner__r.Name);
System.debug('Leaving');
return consBP;
}
No matter what I do, the callback function always returns Error. What is also strange - logs indicate that my function is never even called:
18:33:49.0 (513471)|EXECUTION_STARTED
18:33:49.0 (519142)|CODE_UNIT_STARTED|[EXTERNAL]|Aura
18:33:49.0 (1662266)|CODE_UNIT_FINISHED|Aura
18:33:49.0 (2851055)|EXECUTION_FINISHED
This is everything the logs are saying but when I use Anyonymous Apex to call my function:
System.debug(msContactController.populateConsBPField('0011X00000G7KAwQAN'));
I get proper result that I would be expecting also when calling the Apex Controller from Lightning component:
18:35:21.58 (73544969)|USER_DEBUG|[71]|DEBUG|Entering
18:35:21.58 (95366704)|USER_DEBUG|[73]|DEBUG|Account: Account:{Id=0011X00000G7KAwQAN, Consolidated_Business_Partner__c=0011X00000G7Ju5QAF}
18:35:21.58 (95717963)|USER_DEBUG|[77]|DEBUG|Leaving
18:35:21.58 (95768752)|USER_DEBUG|[2]|DEBUG|(0011X00000G7Ju5QAF, Test ConsBP)
I have no idea why the server controller function might not be called by JS controller. The action is enqueued properly and the callback is still always resulting in error state. Why is this happening?
console.log( var errormsg = response.getError()[0].message)
in state === error condition and post what you got in console.log, it'll help to fix your issue<aura:component>
tag specify thecontroller
?