You need to sign in to do that
Don't have an account?
Phuc 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,
P
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,
P
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; } } }
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