3

I am passing a List of Maps to my controller from the client-side javascript, and when trying to access an element of this list, an internal error is thrown.

Here is an example of the problem :

Javascript :

saveData : function(component) {
    var action = component.get("c.saveDipping");

    var indexList = [];
    indexList.push(new Map());
    indexList.push(new Map());
    indexList[0].sp = 1;
    indexList[0].sp2 = 1;
    console.log(indexList[0]);

    action.setParams({
        "indexListMap" : indexList
    });
    action.setCallback(this, function(response) {
        var state = response.getState();
        if (state === "SUCCESS") {
            alert("ok !");
        } else {
            var errors = response.getError();
            alert("code : " + errors[0].message);
        }
    });
    $A.enqueueAction(action);
}

Apex controller :

@AuraEnabled
public static void saveDipping (List<Map<String, String>> indexListMap) {

        System.debug(indexListMap);
        System.debug(indexListMap[0]);
}

Note that the first debug works fine, and we can check the form of the list, but the second one throws the internal error.

Is there a different way to pass the list of maps as argument to make this work ?

Thank you.

  • 1
    It looks like you're trying to coerce a String into a Decimal. Have you tried adding quotes? – sfdcfox Sep 19 '18 at 12:03
  • Yes, sorry, actually this was part of a previous code where I was trying to manipulate Integer instead of String. But this wasn't the origin of the problem. But thank you ! – lonic Sep 19 '18 at 12:49
0

I ran into this bug on a Visualforce page a few years ago and logged a support ticket, I guess they haven't fixed it. String.valueOf still works on the List, so you may also be able to use the JSON library. You could try doing:

indexListMap = (List<Map<String, String>>)JSON.deserialize(
                JSON.serialize(indexListMap), List<Map<String, String>>.class);

Additionally, you could serialize the List to JSON in your Lightning component and pass that to the controller, then deserialize it in the controller.

  • That worked perfectly, it solved my problem, thank you very much ! It also works on different kind of maps, such as List<Map<String, Integer>>. Thanks again ! – lonic Sep 19 '18 at 12:49

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.