3

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?

4
  • I might have misunderstood but: if you get an error state callback there should be an errors array giving more information about the error. Commented May 29, 2019 at 16:48
  • 2
    add 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
    – sdandamud1
    Commented May 29, 2019 at 16:51
  • 4
    Does your <aura:component> tag specify the controller?
    – Adrian Larson
    Commented May 29, 2019 at 17:03
  • It may be that the change is not being detected. You can tell this by adding a console.log at the start of the autoConsBP method. Hard to know if it would or would not fire the change event without seeing the code. If you want to test your method, you can add a temporary button to your component that would have an action for the method so you can test it out while you try and figure out why the change event is not firing
    – Eric
    Commented May 29, 2019 at 22:47

1 Answer 1

0

Using sdandamud1 suggestion I added debugging to the main JS method:

var errormsg = response.getError()[0].message;
console.log(errormsg);

And it showed: Value provided is invalid for action parameter 'accountId' of type 'String' It turns out that in my Aura Component I was using lightning:recordEditForm as the input for AccountID and the value was being sent as an array!

I my JS controller I just needed to retrieve the value from the array and it worked:

var id = component.get("v.contact.AccountId")[0];

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .