handleSave(event) {        
    this.saveDraftValues = event.detail.draftValues; 
    includeSelectedAccountsToList({ accObj: this.selectedContactIdList, propId: this.recordId, isInclude: true ,jsonString:JSON.stringify(event.detail.draftValues)})
    .then(() => {
        this.template.querySelector('lightning-datatable').selectedContactRows = [];
        const toastEvent = new ShowToastEvent({
            title: 'Success!',
            message: 'Selected Hospitals have been added to inclusion list Successfully',
            variant: 'success'
        const closeQA = new CustomEvent('close');
        // Dispatches the event.
        return refreshApex(this.wireContact);

 public static void includeSelectedAccountsToList(List<String> accObj,String propId,boolean isInclude,String jsonString){
  List<Account> accObjItem = new   List<Account>();

   Map<String, Object> output =  (Map<String,Object>)JSON.deserializeUntyped(jsonString);
  //Set<String> existingSet= existingTableRecords(propId);
  List<CPQ_IDN_Hospital_Inclusion_List__c> idnList= new list<CPQ_IDN_Hospital_Inclusion_List__c>();
  for(Account accRec:[Select Id,CPQ_IDN_Name__c, Name  From Account Where Id IN:accObj]){
   CPQ_IDN_Hospital_Inclusion_List__c rec= new CPQ_IDN_Hospital_Inclusion_List__c();
   insert idnList;

JSON String in Apex: [{"CPQ_Total_Socket__c":"45","Id":"00102000004wiYUAAY"}]

Getting error :

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


The clue is in the exception message.

When using JSON.deserializeUntyped a JSON object, like {"property": "value"}, is converted to an Apex Map<String, Object>. However, a JSON array, like [1, 2, 3] or [{...}, {...}], is converted to an Apex List<Object>.

Since your JSON is a JSON array at the outermost level parsing it will return a List<Object> but you try to cast it to a Map<String, Object> (I assume - you haven't shown the actual code that is getting the exception here) and hence the exception is raised.

As such you need to do:

List<Object> myObjects = (List<Object>) JSON.deserializeUntyped(jsonString);

Note that if your JSON string is correctly defined as always a list of Account records you could instead use:

List<Account> myAccounts = (List<Account>) JSON.deserialize(jsonString, List<Account>.class);
  • Hi Phil, Thank you for the response. But Even after usng Map<String, Object> getting same error. I have updated apex method with : Map<String, Object> output = (Map<String,Object>)JSON.deserializeUntyped(jsonString);.Unforutnately it is not working Feb 23 at 8:30
  • @surindersingh sorry that you misunderstood me. See updated answer.
    – Phil W
    Feb 23 at 8:37
  • Thanks Phil this worked completely fine. Feb 23 at 10:20
  • @surindersingh great. The normal way to reflect that an answer is good for you is to upvote it and mark as best answer. That way your question is classified as an "answered question" instead of an "open question".
    – Phil W
    Feb 23 at 10:33
  • Hi Phil,I tried to upvode but seems like as I am new to portal so system is not allowing me to do and getting this message : Thanks for the feedback! Votes cast by those with less than 15 reputation are recorded, but do not change the publicly displayed post score. Feb 23 at 10:48

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.