+ Start a Discussion
Phuc Nguyen 18Phuc Nguyen 18 

Unable to get map value

Having issue getting map value.  
Template is a lookup field.  If the lookfield has a value where Require_Num__c = true I want the same value on the Activity record to be true also.  There are several templates so I want to only update Activities where the template has the Require_Num__c = true.
I know I have the right map the map size is 1 since I have only set the field to True on one template.  Just not able to select it in the value.  Right now the activity record field is false when it should be true.
Thanks,
if (Trigger.isInsert){
            List<Id> activityIds = new List<Id>();
        
            for (Activity__c act :(List<Activity__c >) trigger.New)
                {
                    activityIds.add(act.Activity_Template__c);

                }

            if(activityIds.size() > 0){
                list<Activity_Template__c> activitytemplate = [Select Name,Id,Require_Num__c
                                    FROM Activity_Template__c 
                                    WHERE Name,Id,Require_Num__c = true
                                    AND Id in : activityIds]; 
                
                atMap = new Map<Boolean,Activity_Template__c>();
                
                for (Activity_Template__c acts : activitytemplate){
                    atMap.put(acts.Name,Id,Require_Num__c,acts);
                }
            }
            system.debug('map size' + atMap.size() );
            for (Activity__c activities : (List<Activity__c >) trigger.New){
                if (atMap.containsKey(activities.Activity_Template__r.Id)){
                    activities.Name,Id,Require_Num__c = true;
                }
            }
        }

 
sachinarorasfsachinarorasf
Hi Phuc Nguyen,
As I noticed many syntactical problems.
You initialized the key in a map as a boolean which can’t be unique. So you need to initialize the map as a Map<Id, Activity_Template__c> .

You can follow the below code.

Trigger

if (Trigger.isInsert && Trigger.isBefore){
        ActivityClass.updateActivity(trigger.new);
}

Class-
Public class ActivityClass{
    public static void updateActivity(List<Activity__c> activityList){
        If(activityList != null && activityList.size() > 0){
            Set<Id> activityIds = new Set<Id>();
            for (Activity__c act : activityList){
                activityIds.add(act.Activity_Template__c);
            } 
            List<Activity_Template__c> activitytemplate = new List<Activity_Template__c>();    
            if(activityIds.size() > 0){
                activitytemplate  = [Select Name,Id,Require_Num__c
                                     FROM Activity_Template__c 
                                     WHERE Require_Num__c = true
                                     AND Id IN : activityIds]; 
            }  
            Map<Id,Activity_Template__c> atMap = new Map<Id,Activity_Template__c>();    
            if(activitytemplate  != null && activitytemplate.size() > 0){          
                for (Activity_Template__c acts : activitytemplate){
                    atMap.put(act.Id,acts);
                }
            }      
            system.debug('map size' + atMap.size() );
            for (Activity__c activities : activityList){
                if (atMap.containsKey(activities.Activity_Template__c)){
                    activities.Require_Num__c = true;
                }
            }
        }
    }
}



I hope you find the above solution helpful. If it does, please mark it as Best Answer to help others too.
Thanks and Regards,
Sachin Arora
www.sachinsf.com
DaspereraDasperera
I hope following piece of code would be useful.
if (Trigger.isInsert){
    Map<Id, Activity_Template__c> atMap = new Map<Id, Activity_Template__c>();
    List<Id> activityIds = new List<Id>();

    for (Activity__c act :  Trigger.New)
    {
        activityIds.add(act.Activity_Template__c);

    }

    if(activityIds.size() > 0){
        list<Activity_Template__c> activitytemplate = [Select Name,Id,Require_Num__c
                            FROM Activity_Template__c 
                            WHERE Require_Num__c = true
                            AND Id in : activityIds]; 
        
        for (Activity_Template__c acts : activitytemplate){
            atMap.put(acts.Id, acts);
        }
    }
    system.debug('map size' + atMap.size() );
    for (Activity__c activities : Trigger.New){
        if (atMap.containsKey(activities.Activity_Template__r.Id)){
            activities.Require_Num__c = true;
        }
    }
}

 
Phuc Nguyen 18Phuc Nguyen 18
Thank you both for the reply.  The activitytemplate is still null so the atMap is never populated.  There are 2 Templates and 1 of the Templates has the field Require_Num__c = true so not sure what the issue is.  Any thoughts?