Hi Danny,First, if you want to update the same record, then your event should be on Before context ie..before insert or before update. Then take as set which will have all these locations and then query from required object using this set.
Please refer the below links how to write and bulkify triggershttps://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_bestpract.htmhttps://developer.salesforce.com/trailhead/force_com_programmatic_beginner/apex_triggers/apex_triggers_bulkRegards,Bhanu Maheshtrigger UpdateDataCenterLocation on Opportunity (before insert, before update){
Set<String> locationSet = new Set<String>();
Map<String,Id> mapLocationSetwithDataCntr = new Map<String,Id>();
for (Opportunity obj: trigger.new){
if(!String.isBlank(obj.Location__c)){
locationSet.add(obj.Location__c);
}
}
if(!locationSet.isEmpty()){
for(Data_Center_Location__c dataCenter : [SELECT Id,Opportunity_Location_Code__c FROM Data_Center_Location__c WHERE Opportunity_Location_Code__c IN :locationSet]){
mapLocationSetwithDataCntr.put(dataCenter.Opportunity_Location_Code__c,dataCenter.Id);//Expecting there will be one record for each location, otherwise this map will have last Data_Center_Location__c Id
}
}
for (Opportunity obj: trigger.new){
if(!String.isBlank(obj.Location__c) && mapLocationSetwithDataCntr != null && mapLocationSetwithDataCntr.get(obj.Location__c) != null){
obj.Data_Center_Location__c = mapLocationSetwithDataCntr.get(obj.Location__c);
}
else{
obj.Data_Center_Location__c = null;
}
}
}
Thanks Bhanu and rba⌗,Applied Bhanu's changes first but i'm sure rba's would have worked as well. Looks like your fix worked Bhanu, I appreciate the assistance!
error
Close
3 answers