[SalesForce] Passing sObject List from Client Controller to Server Controller (Lightning Components)

Seems like I'm not being able to pass a sObject list to my server-side controller.

I've seen articles and other forum answers that propose using JSON.stringify. I've tried that method, but my server-side controller throws an error stating: "null input to JSON parser". System.debug() shows a 'null' value for the input String parameter. Not sure what's going on.

If I try passing the sObject, I also get a null value, but no error. I have verified that the argument I'm passing from the server-side is not null via console.log().

For the stringify method, I'm changing the action2.setParam() to:

var action2 = component.get('c.relatedOpportunities');
            action2.setParam({  
                openOppsString: JSON.stringify(oppList)
            });

Please note that I'm making two server calls for init.

Client-side Controller

({
doInit : function(component, event, helper) {

    var action = component.get('c.getOpenRenewals');
    action.setParams({
        accountId: component.get("v.recordId")
    })

    // the oppList variable below is to store the list of open renewals to pass on to the second server call
    var oppList = [];
    action.setCallback(this, function(actionResult) {
        component.set('v.openRenewals', actionResult.getReturnValue());
        oppList = actionResult.getReturnValue();

        // debugging
        console.log('>>> Log 1.0: ' + JSON.stringify(actionResult.getReturnValue()));
        console.log('>>> Log 1.1: ' + actionResult.getReturnValue());
        console.log('>>> Log 1.2: ' + JSON.stringify(oppList));
        console.log('>>> Log 1.3: ' + oppList);

        var action2 = component.get('c.relatedOpportunities');
        action2.setParam({  
            openOpps: oppList
        });

        action2.setCallback(this, function(actionResult) {
            component.set('v.associatedOpps', actionResult.getReturnValue());
            console.log('>>> Log 2: ' + JSON.stringify(actionResult.getReturnValue()));
        });

        $A.enqueueAction(action2);

    });

    $A.enqueueAction(action);
}
})

CLIENT LOGS

>>> Log 1.0: [{"Id":"0060x0000061FU0AAM","AccountId":"0014100000gZs1IAAS","Name":"Open Renewal - Test","StageName":"SBR 2 / Renewal Quote","Amount":1234,"CloseDate":"2018-08-03","OwnerId":"00541000001TeSwAAK","RecordTypeId":"012410000019XbpAAE","Owner":{"Name":"f","Id":"00541000001TeSwAAK"}}]

>>> Log 1.1: [object Object]

>>> Log 1.2: [{"Id":"0060x0000061FU0AAM","AccountId":"0014100000gZs1IAAS","Name":"Open Renewal - Test","StageName":"SBR 2 / Renewal Quote","Amount":1234,"CloseDate":"2018-08-03","OwnerId":"00541000001TeSwAAK","RecordTypeId":"012410000019XbpAAE","Owner":{"Name":"f","Id":"00541000001TeSwAAK"}}]

>>> Log 1.3: [object Object]

>>> Log 2: {}

Server-Side Controller (Snippet) 1

@auraEnabled     
public static Map<Id, List<Opportunity>> relatedOpportunities(List<Opportunity> openOpps) {

        system.debug(openOpps.size());
        system.debug(openOpps);

SERVER DEBUG LOGS 1

[43]|DEBUG|0
[44]|DEBUG|()

Server-Side Controller (Snippet) 2
(with this method, I am changing the input parameter in the client-side as well from 'openOpps' to 'openOppsString')

@auraEnabled
public static Map<Id, List<Opportunity>> relatedOpportunities(String openOppsString) {

    system.debug(openOppsString);
    List<Opportunity> openOpps;
    openOpps = (List<Opportunity>)System.JSON.deserializeStrict(openOppsString, List<Opportunity>.class);
    */

    system.debug(openOpps.size());
    system.debug(openOpps);

SERVER DEBUG LOGS 2

[37]|DEBUG|null

Best Answer

setParam expects two parameters, a name and the value to set. Either use setParams, or use the correct setParam syntax:

        action2.setParam("openOpps", oppList);

It is generally not necessary to JSON.stringify yourself, and you should avoid doing so when possible.