0

I use a managed package which publishes some events. I have created a "subscriber" apex class to subscribe to those events. Inputs are: String topic, String subject, Map<String, Object> payload.

The payload's Object is also a Map<Id, List<Id>>. How can I convert the Map<String, Object> to Map<Id, List<Id>>? My goal is to get into a list the values of the Object map, but whatever I have tried threw errors like: Invalid conversion from runtime type Map<String,ANY> to Map<String,Map<Id,List<Id>>>

//19/05/2021 @ggalaios: Apex Class triggered whenever a Product Configuration is cloned
//Purpose is to update the Opportunity's Included Product Families field

global class el_CSMessageSubscriber extends csutil.ASubscriber {

    global override void onMessage(String topic, String subject,
        Map<String, Object> payload) {
        
           try {
                System.debug(
                    'Topic: ' + topic + '; Subject: ' + subject
                    + '; Payload: ' + Json.serialize(payload)
                   );
                system.debug('Map values are: ' +Json.serialize(payload.values()));

                                       
                                       
           } catch (Exception e) {
                system.debug('Exception occured in subscriber... ' +e.getMessage() +e.getLineNumber());
             }
        }
}

Example Payload: Payload: {"ConfigIdsOldToNew":{"a1O1l000000vDgFEAU":["a1O5r0000009SSKEA2"]}}

Screenshot below from the debug logs: enter image description here

7
  • 2
    what are you getting when you are logging payload.values()? also can you add an example payload to question? May 19, 2021 at 17:50
  • Question updated! May 19, 2021 at 18:00
  • your payload doesn't seem like it'sMap<String, List<Id>>, it seems to be Map<String, Map<String, List<id>>>. reason being, look at the debug log Map values are : that is a Key value pair where Key is a string and Values are a list. May 19, 2021 at 18:08
  • 1
    I am preparing an example for you, will send in a few. May 19, 2021 at 18:30
  • Thank you @anxiousAvocado :) May 19, 2021 at 18:42

1 Answer 1

0

I found solution to my problem. All it was needed was to iterate through the map and then convert the embedded object to a map of the required type.

//19/05/2021 @ggalaios: Apex Class triggered whenever a Product Configuration is 
//cloned
//Purpose is to update the Opportunity's Included Product Families field

global class el_CSMessageSubscriber extends csutil.ASubscriber {

public class subscriberException extends Exception{}

global override void onMessage (String topic, String subject, Map<String, Object> payload) { 
  try {
      //Define new set to store the newly generated Product Configuration Ids
      set<Id> finalIds = new set<Id>();
      
      //Iterate through the payload map (String, Object)
      for (String key : payload.keySet()) {
          Object obj = payload.get(key);
          //Convert the Object to a Map<Id, List<Id>>
          Map<Id, List<Id>> tmpMap = (Map<Id, List<Id>>) obj;
      }
      
      
  } catch (Exception e) {
      system.debug('Exception occured in class el_CSMessageSubscriber: ' +e.getMessage() +'Line is: ' +e.getLineNumber());
      throw new subscriberException('Exception occured in class el_CSMessageSubscriber: ' +e.getMessage() +'Line is: ' +e.getLineNumber());
  }
  }
 }

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.