1

I'm passing below Javascript map from aura component controller to apex controller but map is showing empty in debug log.

var obj = {'0QL8W000006RHl1WAG'=>1,'0QL8W000006RHl0WAG'=>2,'0QL8W000006RHl2WAG'=>3}

Aura component controller

var obj = {'0QL8W000006RHl1WAG'=>1,'0QL8W000006RHl0WAG'=>2,'0QL8W000006RHl2WAG'=>3}

var action= component.get('c.updateSortOrder');

action.setParams(
{"mapofIdSortOrder" : obj}
);        

Apex controller

@AuraEnabled

public static void updateSortOrder(Map<String,Integer> mapofIdSortOrder){

  system.debug('mapofIdSortOrder size---'+ mapofIdSortOrder.size());

}

Map<String,Integer> mapofIdSortOrder is showing empty in debug log in salesforce.

Seems it's not getting correctly passed from aura controller.

Please tell me where am I going wrong.

9
  • in aura controller are you doing $A.enqueueAction(action);
    – Aman Patil
    Mar 12 at 18:01
  • @AmanPatil Yes.. I figured it out. If I'm trying to pass the map in below format, it's not working. var obj = {'0QL8W000006RHl1WAG'=>1,'0QL8W000006RHl0WAG'=>2,'0QL8W000006RHl2WAG'=>3} Rather below format is working fine. obj["0QL8W000006RHl1WAG"] = 1; obj["0QL8W000006RHl0WAG"] = 2; obj["0QL8W000006RHl2WAG"] = 3; Any idea how can I convert it to above format ? Do I have to run any for loop to get this format ? Mar 12 at 18:15
  • try changing the obj to var obj = {'0QL8W000006RHl1WAG': 1, '0QL8W000006RHl0WAG': 2, '0QL8W000006RHl2WAG': 3}; see if this works
    – Aman Patil
    Mar 12 at 18:19
  • 1
    @AmanPatil That's the answer that you should add. I don't need the rep :D
    – sfdcfox
    Mar 12 at 18:28
  • @sfdcfox How can I convert the below format to the required one ? var obj = {'0QL8W000006RHl1WAG'=>1,'0QL8W000006RHl0WAG'=>2,'0QL8W000006RHl2WAG'=>3} var obj = {'0QL8W000006RHl1WAG':1,'0QL8W000006RHl0WAG':2,'0QL8W000006RHl2WAG':3} Please help. Mar 12 at 18:30

1 Answer 1

1

What is think is the Obj that you are referring is a map type is JS

you can convert that Map to Object with below code

var obj = {'0QL8W000006RHl1WAG'=>1,'0QL8W000006RHl0WAG'=>2,'0QL8W000006RHl2WAG'=>3}

const newObj = Object.fromEntries(obj);

var action= component.get('c.updateSortOrder');

action.setParams(
{"mapofIdSortOrder" : newObj}
);        


action.setCallback(this, function(response) {
    var state = response.getState();
    if (state === "SUCCESS") {
        // handle success
    } else if (state === "ERROR") {
        // handle error
    }
});

$A.enqueueAction(action);

8
  • Do you have the javascript code to convert the '=>' to ':' ? Please help. Mar 12 at 18:31
  • I have edited the answer see if this works
    – Aman Patil
    Mar 12 at 18:34
  • @AmanPatil That won't work--{x=>y} is not valid JavaScript, so this answer can't possibly be the solution.
    – sfdcfox
    Mar 12 at 18:36
  • @sfdcfox i think he is referring the obj as a map in JS that needs to be converted as a valid JS object
    – Aman Patil
    Mar 12 at 18:39
  • 1
    @AmanPatil Thanks a ton Aman.. It's working now :) Mar 12 at 19:08

You must log in to answer this question.

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