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.

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.

| improve this answer | |
  • Thanks much @sfdcfox – Sam Mar 7 '19 at 1:26

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.