Skip to main content The Trailblazer Community will be unavailable from 2/1/2025 to 2/2/2025. Please plan your activities accordingly.
error

We can't find this post.

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...
3 answers
  1. Feb 18, 2015, 4:45 AM

    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
  2. Feb 18, 2015, 5:30 PM
    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!
0/9000