0

I'm trying to pass the old & newmaps as arguments to a future method using JSON method. I see the below error when it tries to deserialize the data. reference-Passing SObjects to Future

|FATAL_ERROR| System.TypeException: Invalid conversion from runtime type OpportunityLineItem to Map<Id,OpportunityLineItem>

//Trigger

if(Trigger.isUpdate && Trigger.isAfter){
            if(CheckOpportunityLineItemRecursive_AC.runAfterUpdateOnce()){
                if(!System.isFuture() && !System.isBatch()){
                    String jsonOldMap = JSON.serialize(Trigger.oldMap);
                    String jsonNewMap = JSON.serialize(Trigger.newMap);                       OpportunityLineItemTriggerHandler_AC.OnAfterUpdateAsync(jsonOldMap, jsonNewMap);
                }
            }

//Future method

 public static void OnAfterUpdateAsync(String strOldMap, String strNewMap){
    Map<Id, OpportunityLineItem> oldMap = (Map<Id, OpportunityLineItem>)JSON.deserialize(strOldMap, System.Type.forName('OpportunityLineItem'));
    Map<Id, OpportunityLineItem> newMap = (Map<Id, OpportunityLineItem>)JSON.deserialize(strNewMap, System.Type.forName('OpportunityLineItem')); //do something

Please help me find what Im missing here. Thanks.

1 Answer 1

3

You should be using the correct type for the second parameter to JSON.deserialize:

 Map<Id, OpportunityLineItem> oldMap = (Map<Id, OpportunityLineItem>)
    JSON.deserialize(strOldMap, Map<Id, OpportunityLineItem>.class);

Or:

Map<Id, OpportunityLineItem> oldMap = (Map<Id, OpportunityLineItem>)
    JSON.deserialize(strOldMap, System.Type.forName('Map<Id,OpportunityLineItem>'));

(I would personally recommend the first format to avoid typos).

You can get the type for collections (Map, List, and Set) via the .class notation as demonstrated above.

1
  • Thanks much @sfdcfox
    – Sam
    Commented Mar 7, 2019 at 1:26

You must log in to answer this question.

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