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

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);
}
  • thanks, but how can I get all pairs e.g 'PHONE':'(512) ----' from Object? – pincet Nov 22 '19 at 10:48
  • Just use String.valueOf method. String phone = String.valueOf(values[0].get('PHONE')) (check that 'PHONE' key exists before) – Raman Silin Nov 22 '19 at 12:34

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.