0

I'm getting error in the following apex method: System.TypeException: Invalid conversion from runtime type List to Map<String,ANY>

Please find the apex method:

@AuraEnabled(cacheable=true)  
    public static void updateQuantity(String recordId, Object quantity){
        System.debug('recordId--------->' + recordId + 'quantity------->' + quantity);
        
        Map<String, Object> quantityWrapper = (Map<String, Object>)JSON.deserializeUntyped(JSON.serialize(quantity));
               
        List<Map<String, Object>> quantityJSON = new List<Map<String, Object>>();
        
        for (Object instance : (List<Object>)quantityWrapper.get('quantityJSON'))
        quantityJSON.add((Map<String, Object>)instance);    
        
        system.debug('quantity:'+quantityJSON);
        
        
    }

JSON: Following are the draftValues from LWC Component.

[
  {
    "splitQuantityValue": "30",
    "Id": "row-0"
  },
  {
    "splitQuantityValue": "30",
    "Id": "row-1"
  },
  {
    "splitQuantityValue": "30",
    "Id": "row-2"
  }
]

Any kind of help is appreciated.

1 Answer 1

2

The error message you're getting provides a good hint here

System.TypeException: Invalid conversion from runtime type List to Map<String,ANY>`

In JSON, square braces [] indicate that you're working with an array, with a List. Curly braces {} indicate that you're working with an object, a Map<String, Object>.

Since the JSON you're working with starts with square braces, it's a List, and you need to deserialize it as a List, so:

  • quantityWrapper needs to be declared as a List<Object>
  • you need to cast the result of JSON.deserializeUntyped() as a List<Object>
  • you don't need the loop at all (unless you're trying to gather or sum the individual quantities)
1
  • Thank you @Derek F for the reply. It worked with the solution you provided. List<Object> quantityWrapper = (List<Object>)JSON.deserializeUntyped(JSON.serialize(quantity)); Thank you for your time.
    – Rose
    Aug 23 at 12:42

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.