0

Lets say, I have a map created in aura component controller. I want to send map to apex controler but unfortunately map cannot be serialized by JSON.stringify. My code

JSON.stringify([...draftValues.entries()])

where draftValues is a map

server side

List obj= (List)JSON.deserializeUntyped('[["0017F00000gqDetQAE",{"ACCOUNTNUMBER":"CD451796 -- new","PHONE":"(512) 757-6000 --"}],["0017F00000gqDevQAE",{"NAME":"Pyramid Construction Inc. --"}]]');

how can I retrive Id (map key) and value (map.values) from this list of objects?

1 Answer 1

1

You have to build the map in JS as an object:

let jsMap = {
    '0017F00000gqDetQAE': {
        'ACCOUNTNUMBER': 'CD451796 -- new',
        'PHONE': '(512) 757-6000 --'
    },
    '0017F00000gqDevQAE': {
        'NAME': 'Pyramid Construction Inc. --'
    }
};

let stringForSendingToAPEX = JSON.stringify(jsMap);

Then you will be able to deserialize this string stringForSendingToAPEX into Map<String, Object> in the APEX:

Map<String, Object> deserialized = (Map<String, Object>) JSON.deserializeUntyped(stringForSendingToAPEX);

So, the IDs will be deserialized.keySet() and values:

List<Map<String, Object>> values = new List<Map<String, Object>>();
for (Object obj : deserialized.values()) {
    values.add((Map<String, Object>) obj);
}
2
  • thanks, but how can I get all pairs e.g 'PHONE':'(512) ----' from Object?
    – pincet
    Commented Nov 22, 2019 at 10:48
  • Just use String.valueOf method. String phone = String.valueOf(values[0].get('PHONE')) (check that 'PHONE' key exists before) Commented Nov 22, 2019 at 12:34

You must log in to answer this question.

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