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
Qiuyan LIUQiuyan LIU 

Can't get the return value of apex function In lightning component controller

Hello everyone,

In a Lightning component, I am trying to call Apex function and get a String return value,
The function I called in Apex : 
public with sharing class UserSearch {
    ....
    @AuraEnabled
    public static String createPersonAccount(Account account) {
       System.debug('createPersonAccount Function begins...');
       return 'Test !'; 
    }
...
}

in the comononent controller : 
   var account = component.get("v.account");
        var action = component.get("c.createPersonAccount");
        action.setParams({
            'account' : account
        });  
        action.setCallback(this, function(actionResult) { 
            var returnValue = actionResult.getReturnValue();           
        });
        $A.enqueueAction(action);

With the same code, sometimes I can get the return value correctly, but most of the time, what I get is like this :

Sorry the photo is not very clear, 
the message are  : 
   error: Array[1]
        0: Object
             message: "Disconnected or Canceled"
         __proto__: Object
    length: 1
    __proto__: Array[0]
 
id: "158;a"
returnValue: undefined
state: "INCOMPLETE"
__proto__: Action

User-added image

Could any one help me please ? 
 
Adrian  GawryszewskiAdrian Gawryszewski
What is v.account? Is it an object? Id? Try to serialize it before sending. Also remove quotes from 'account' and try again.
Qiuyan LIUQiuyan LIU
Thank you for your reply, Adrian, 

The v.account is an component attribute, it is defined like this :
<aura:attribute name="account" type="Account" default="{ sobjectType: 'Account' }"/>

I tried to serialized before sending the return value, nothing changed, I also tried just return a simple String like 'Test' . It is always the same, sometimes I can get the return value, most of the time I CAN NOT. It is kinda weird for me !
Adrian  GawryszewskiAdrian Gawryszewski
I'll be honest passing an object or a map from Aura to Apex causes a lot of troubles (tried that on few occasions and didn't work well). If I were you I would try to pass an AccountId instead of an object and SOQL it in the function. 
Adrian  GawryszewskiAdrian Gawryszewski
Try to serialize the account object before sending to APEX so on the Aura side.. Sorry missed that bit.
Qiuyan LIUQiuyan LIU
I will try to serialized all parameters, Hope it will work, Thank you Adrian .