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
DannyTKDannyTK 

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...
Best Answer chosen by DannyTK
Bhanu MaheshBhanu Mahesh

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.
trigger 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;
		}
    }
}

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

Bhanu MaheshBhanu Mahesh

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.
trigger 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;
		}
    }
}

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
This was selected as the best answer
ra811.3921220580267847E12ra811.3921220580267847E12
Hi,

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;
}


}
}
DannyTKDannyTK
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!