You need to sign in to do that
Don't have an account?
DannyTK
Trigger to populate lookup field from picklist value
Good afternoon community,
I'm trying to populate a lookup field on Opportunity object called Data_Center_Location__c with the Name (or ID) from custom object Data_Center_Location__c, based on picklist field on Opportunity called Location__c. (so based on picklist selection on Location field on Opportunity, the lookup field will populate with the Name of the corresponding record from the Data_Center_Location__c object....which i'm using as a cross reference table). Field on the custom object to match with Opportunity.Location__c would be Data_Center_Location__c.Opportunity_Location_Code__c.
Seems like a simple concept, but i'm not a developer. So far i've gotten:
trigger UpdateDataCenterLocation on Opportunity (after insert, after update) {
List<String> Opportunity = new List<String>();
for (Opportunity obj: trigger.new){
Data_Center_Location__c.add(obj.Data_Center_Location__c.Name);
}
list<Data_Center_Location__c> DCLlist = [select Opportunity_Location_Code__c from Data_Center_Location__c where Location__c in :Opportunity];
if (DCLlist.size() > 0 ){
for (Integer i = 0; i < Trigger.new.size(); i++)
{
if (Trigger.new[i].Data_Center_Location__c != null)
{
Trigger.new[i].Data_Center_Location__c = DCLlist[i].ID;
}
else
{
Trigger.new[i].Data_Center_Location__c = null;
}
}
}
}
I don't think this is close to being right. Does anyone know how to clean this up so it works?...not sure where to begin...
I'm trying to populate a lookup field on Opportunity object called Data_Center_Location__c with the Name (or ID) from custom object Data_Center_Location__c, based on picklist field on Opportunity called Location__c. (so based on picklist selection on Location field on Opportunity, the lookup field will populate with the Name of the corresponding record from the Data_Center_Location__c object....which i'm using as a cross reference table). Field on the custom object to match with Opportunity.Location__c would be Data_Center_Location__c.Opportunity_Location_Code__c.
Seems like a simple concept, but i'm not a developer. So far i've gotten:
trigger UpdateDataCenterLocation on Opportunity (after insert, after update) {
List<String> Opportunity = new List<String>();
for (Opportunity obj: trigger.new){
Data_Center_Location__c.add(obj.Data_Center_Location__c.Name);
}
list<Data_Center_Location__c> DCLlist = [select Opportunity_Location_Code__c from Data_Center_Location__c where Location__c in :Opportunity];
if (DCLlist.size() > 0 ){
for (Integer i = 0; i < Trigger.new.size(); i++)
{
if (Trigger.new[i].Data_Center_Location__c != null)
{
Trigger.new[i].Data_Center_Location__c = DCLlist[i].ID;
}
else
{
Trigger.new[i].Data_Center_Location__c = null;
}
}
}
}
I don't think this is close to being right. Does anyone know how to clean this up so it works?...not sure where to begin...
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 triggers
https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_bestpract.htm
https://developer.salesforce.com/trailhead/force_com_programmatic_beginner/apex_triggers/apex_triggers_bulk
Regards,
Bhanu Mahesh
All Answers
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 triggers
https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_bestpract.htm
https://developer.salesforce.com/trailhead/force_com_programmatic_beginner/apex_triggers/apex_triggers_bulk
Regards,
Bhanu Mahesh
trigger locationDemo on Opportunity (Before insert, before update) {
set<String> ids= new Set<String>();
for(Opportunity opp1:Trigger.new)
{
if(opp1.LocationPickList__c!=null)
{
ids.add(opp1.LocationPickList__c);
}
}
List<DataCenterLocation__c> dclocation=[select id, name from DataCenterLocation__c where name in:ids];
for(Opportunity opp:trigger.new)
{
if(dclocation.size()>0)
{
opp.DataCenterLocation__c=dclocation[0].id;
}
else
{
opp.DataCenterLocation__c=null;
}
}
}
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!