2

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
  • 1
    To set a data for an action use action.setParams() not as action .setParam() – vignesh Aug 4 '18 at 4:55
  • That's exactly what was causing the issue...thank you! – fdc Aug 4 '18 at 5:05
  • 3
    @vignesh There is a setParam method, but it uses a different syntax. – sfdcfox Aug 4 '18 at 5:07
5

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.

  • That's exactly what was causing the issue...thank you! – fdc Aug 4 '18 at 5:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.