-1

I have the following code. Although it seems i give the same map, the "get" on line 36 returns me null. You can run on anonymous console the testOrchestratorClass.startOrderOrchestrationProcess(); command. Could you please assist me on what i am doing wrong?

public class testOrchestratorClass {
    
    public class mtd
    {
        public string Product_Service_Type {get;set;}
        public string Record_Type {get;set;}
        public string Change_Type {get;set;}
        public string level {get;set;}
    }
    
    public static Map<mtd, String> mapOrchestratorProcessTemplates() {
        Map<mtd, String> orchestrationProcessTemplateMap = new Map<mtd, String>(); //initiate a new map    
        for(Integer i=0; i<1; i++){
            mtd tmpMetadata = new mtd();
            tmpMetadata.Product_Service_Type = null;
            tmpMetadata.Record_Type = 'Individual';
            tmpMetadata.Change_Type = null;
            tmpMetadata.level = 'csord__Order__c';
            String currentProcessTemplate = 'New Residential Electricity Order';
            orchestrationProcessTemplateMap.put(tmpMetadata, currentProcessTemplate);
        }
        system.debug(orchestrationProcessTemplateMap);
        return orchestrationProcessTemplateMap;
    }
    
    public static void startOrderOrchestrationProcess(){
        try{
            Map<mtd, String> orchestrationProcessTemplateMap = mapOrchestratorProcessTemplates(); 
            system.debug(orchestrationProcessTemplateMap);
            mtd mtdOrder = new mtd();
            mtdOrder.Change_Type = null;//order.csordtelcoa__Opportunity__r.csordtelcoa__Change_Type__c;
            mtdOrder.Product_Service_Type=null;
            mtdOrder.Record_Type = 'Individual';
            mtdOrder.level = 'csord__Order__c';
            system.debug(mtdOrder);
            system.debug(orchestrationProcessTemplateMap.get(mtdOrder));
        } catch (Exception e) {
            system.debug('error');
        }
    }
}
2
  • 1
    tmpMetadata used in mapOrchestratorProcessTemplates method and mtdOrder used in startOrderOrchestrationProcess method are not the same, even though both are of type mtd. So, in line # 36, you are trying to get an entry in the map that doesn't exist yet. What did you actually want to achieve in this code?
    – arut
    Nov 19 '20 at 16:36
  • I want to achieve avoiding iteration in my code for every item of the map. So, because i have lots of attributes, i created the sub-class and stored them there. I have to take the information from a "matrix" custom metadata types and i was a bit confused on how i can optimize this code. Nov 20 '20 at 7:25
1

The key in map is hashed. So your key changes when you try to do use a new keyword while fetching the value from orchestrationProcessTemplateMap

If you need to fetch the value fetch it by iterating the key from map like shown below.

But again, what are you trying to achive by keeping key as a class and value as string.

public static void startOrderOrchestrationProcess(){
    try{
        Map<mtd, String> orchestrationProcessTemplateMap = mapOrchestratorProcessTemplates();
        system.debug(orchestrationProcessTemplateMap);
        //mtd mtdOrder = new mtd();
        //mtdOrder.Product_Service_Type = null;
        //mtdOrder.Record_Type = 'Individual';
        //mtdOrder.Change_Type = null;
        //mtdOrder.level = 'csord__Order__c';
        //system.debug(mtdOrder);
        // system.debug(orchestrationProcessTemplateMap.get(mtdOrder));
        for(mtd eachKey : orchestrationProcessTemplateMap.keySet()){
            system.debug(orchestrationProcessTemplateMap.get(eachKey));
        }
    } catch (Exception e) {
        system.debug('error');
    }
}

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.