function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Phuc Nguyen 18Phuc Nguyen 18 

Issue getting value from map

I am trying to get a vlaue from a map to use in an If statement
I need to get the status value from the m_cl map.  How do I do that?
Thank you,
P
 
Map<Id,Checklist__c> m_cl = new Map<Id, Checklist__c>();
Set<Id> checkListIds = new Set<Id>();
        Set<Id> prjIds = new Set<Id>();
        Set<Id> prjNcIds = new Set<Id>();
        Set<Id> checkListNcIds = new Set<Id>();

        if(Trigger.isUpdate) {            
            for (SObject so : Trigger.New) {
              Checklist__c newCL = (Checklist__c) so;
              Checklist__c oldCL = Trigger.oldMap != null && Trigger.oldMap.containsKey(newCL.Id)
                ? (Checklist__c) Trigger.oldMap.get(newCL.Id)
                : null;

                    if(newCL.Status__c != oldCL.Status__c && (newCL.Status__c == 'Complete' || oldCL.Status__c == 'In Progress')){
                        m_cl.put(newCL.Activity__c, newCL);
                        checkListIds.add(newCL.Id);
                        prjIds.add(newCL.Project__c);
                    }

            }

            if(checkListIds.size()> 0){
                act = [Select id,Name,Project__c, Project__r.RC_Completed__c, Project__r.M5_Completed__c,Checklist__r.Status__c,Checklist__r.Name,Checklist__c,Form_Complete__c
                      FROM Activity__c 
                      WHERE Form_Complete__c = false 
                      AND Checklist__c IN : checkListIds
                      ];

                prj = [Select id, Name, RC_Completed__c ,M5_Completed__c 
                       FROM Project__c 
                       WHERE Id IN : prjIds];
              
                if( act.size() > 0){
                    for(Activity__c acts : act){
                        formName = acts.Checklist__r.Name.split(':');    
                        if(formName[1] == 'Pre-M5 Check List Form' && m_cl.get('Status__c') == 'Complete'){
                            acts.M5_Completed__c = true;
                            actToUpdate.add(acts);
                        }
                        else {
                            acts.RC_Completed__c = true;
                            actToUpdate.add(acts);
                        }
                    }
                }

 
ANUTEJANUTEJ (Salesforce Developers) 
Hi Phuc,

The map is a structure that stores data as key-value pairs, so in the above snippet the key is id and value is Checklist__c record so while using get function you need to pass the key of the value you are trying to get.

so in the above snippet I see you used m_cl.get in one of the if statements and you use status__c inside the get method.

You will have to alter it and pass id in the place of status__c to get the record.

You need to change the below snippet from below :
if(formName[1] == 'Pre-M5 Check List Form' && m_cl.get('Status__c') == 'Complete'){
                            acts.M5_Completed__c = true;
                            actToUpdate.add(acts);
                        }
to something like below:
if(formName[1] == 'Pre-M5 Check List Form' && m_cl.get(idOfRecord).status__c == 'Complete'){
                            acts.M5_Completed__c = true;
                            actToUpdate.add(acts);
                        }

To learn more about how to use get method on map you can check the below link:

>> https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_map.htm

under get method, you can check how to use it.

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.
Suraj Tripathi 47Suraj Tripathi 47
Hi Phuc Nguyen 18,
Actually you have create Map like this Map<Id,Checklist__c> m_cl = new Map<Id, Checklist__c>() but try to put value Like this m_cl.put(newCL.Activity__c, newCL) which is not correct try to insert value in map Like this m_cl.put(newCL.Id, newCL.Activity__c).
I have made changes in your code, which you can get  by passing the id of Checklist__c Like this 
String newCL_Activity = m_cl.get(newCL.Id)

Map<Id,Checklist__c> m_cl = new Map<Id, Checklist__c>();
Set<Id> checkListIds = new Set<Id>();
        Set<Id> prjIds = new Set<Id>();
        Set<Id> prjNcIds = new Set<Id>();
        Set<Id> checkListNcIds = new Set<Id>();

        if(Trigger.isUpdate) {            
            for (SObject so : Trigger.New) {
              Checklist__c newCL = (Checklist__c) so;
              Checklist__c oldCL = Trigger.oldMap != null && Trigger.oldMap.containsKey(newCL.Id)
                ? (Checklist__c) Trigger.oldMap.get(newCL.Id)
                : null;

                    if(newCL.Status__c != oldCL.Status__c && (newCL.Status__c == 'Complete' || oldCL.Status__c == 'In Progress')){
                        m_cl.put(newCL.Id, newCL.Activity__c);
                        checkListIds.add(newCL.Id);
                        prjIds.add(newCL.Project__c);
                    }

            }

            if(checkListIds.size()> 0){
                act = [Select id,Name,Project__c, Project__r.RC_Completed__c, Project__r.M5_Completed__c,Checklist__r.Status__c,Checklist__r.Name,Checklist__c,Form_Complete__c
                      FROM Activity__c 
                      WHERE Form_Complete__c = false 
                      AND Checklist__c IN : checkListIds
                      ];

                prj = [Select id, Name, RC_Completed__c ,M5_Completed__c 
                       FROM Project__c 
                       WHERE Id IN : prjIds];
              
                if( act.size() > 0){
                    for(Activity__c acts : act){
                        formName = acts.Checklist__r.Name.split(':');    
                        if(formName[1] == 'Pre-M5 Check List Form' && m_cl.get('Status__c') == 'Complete'){
                            acts.M5_Completed__c = true;
                            actToUpdate.add(acts);
                        }
                        else {
                            acts.RC_Completed__c = true;
                            actToUpdate.add(acts);
                        }
                    }
                }
If you find your Solution then mark it as the best answer. 

Thanks and Regards
Suraj Tripathi.
Phuc Nguyen 18Phuc Nguyen 18
Thank you both for your reply.
So I updated the map to 
checkListIds.add(newCL.Id);

But I keep getting invalid id error when I try to get the value from the map
Tried 
string status = m_cl.get('Id').Status__c;
String.valueof(m_cl.get('Status__c'));
Any thoughts?
 
Phuc Nguyen 18Phuc Nguyen 18
Thought this would work but getting error:
Attempt to de-reference a null object