3

It seems like collection types returned from the Apex to an Aura component omit null elements.

I noticed this when I was passing a List of Strings back from Apex. I was failing to get the second element of the List, despite having set the second element manually. Turns out the first element was not making it back to the Aura. I switched to a Map, which fixed the issue, but even Map elements with null values are being omitted.

Component:

<aura:component access="GLOBAL" controller="NullResponse_Apex" implements="lightning:isUrlAddressable">
    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
</aura:component>

doInit: function(component, event, helper) {
    helper.getList(component, event, helper);
    helper.getMap(component, event, helper);
}

getList: function(component, event, helper) {
    var action = component.get('c.returnListWithNull');

    action.setCallback(this, function(result){
        var status = result.getState();
        if (status==='SUCCESS') {
            console.log('List: ', result.getReturnValue());
        }
    });

    $A.enqueueAction(action);
},

getMap: function(component, event, helper) {
    var action = component.get('c.returnMapWithNull');

    action.setCallback(this, function(result){
        var status = result.getState();
        if (status==='SUCCESS') {
            console.log('Map: ', result.getReturnValue());
        }
    });

    $A.enqueueAction(action);
}

Apex:

@AuraEnabled
public static List<String> returnListWithNull() {
    List<String> strList = new List<String>();
    strList.add(NULL);
    strList.add('Test');
    return strList;
}

@AuraEnabled
public static Map<String,String> returnMapWithNull() {
    Map<String,String> strMap = new Map<String,String>();
    strMap.put('a',NULL);
    strMap.put('b','Test');
    return strMap;
}

Response in console:

List:  ["Test"]
Map:  {b: "Test"}

Is this expected behavior? Has anyone else run into this issue?

2
  • 2
    +1 for description and steps to reproduce. Good question. Commented Oct 22, 2019 at 19:19
  • 1
    It could be related to this: Starting with API version 28.0, null fields aren’t serialized and aren’t included in the JSON string, unlike in earlier versions. Source: Roundtrip Serialization and Deserialization
    – Jayant Das
    Commented Oct 22, 2019 at 19:33

1 Answer 1

6

Looks like Aura strips out JSON nulls.

You can use the workaround of sending String from Aura-enabled method and then parsing them in javascript

@AuraEnabled
public static String returnMapWithNull() {
    Map<String,String> strMap = new Map<String,String>();
    strMap.put('a',null);
    strMap.put('b','Test');
    return JSON.serialize(strMap);
}

JS:

getMap: function(component, event, helper) {
    var action = component.get('c.returnMapWithNull');

    action.setCallback(this, function(result){
        var status = result.getState();
        if (status==='SUCCESS') {
            console.log('Map: ', JSON.parse(result.getReturnValue()));
        }
    });

    $A.enqueueAction(action);
}

OP:

Map:  {b: "Test", a: null}

The only drawback is, the order is not maintained for Map, but for list it is

2
  • 1
    So I guess the answer is "It just be that way". That's unfortunate and weird, but I guess it's easy to get around. Why would Salesforce want to strip nulls? If I put a null in a List/Map, then by gawd, I expect it to stay there.
    – iwhitt567
    Commented Oct 23, 2019 at 15:01
  • Does anybody know why Salesforce does this?
    – gburnett
    Commented Nov 25, 2021 at 10:53

You must log in to answer this question.

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