+ Start a Discussion
Connor ReillyConnor Reilly 

Apex trigger to update a picklist field

Hello, 
I'm trying to create a trigger to update a field based on the value of another field. The field I'm trying to update is a picklist and the values I'm trying to update it to are active picklist values. There are various HS_Original_Source__c values that if they are populated, I want the standard leadsource field to have it's equivilant value. Ex. HS_Original_Source__c is ORGANIC SEARCH and would map to leadsource of Organic Search.
 
trigger LeadSourceTrigger on Opportunity (before update, before insert) {
 List<Opportunity> oppList = new List<Opportunity>();
    
    //Go through the updated records coming into the database and update their custom fields 
    //according to the updated lead source 
    for(Opportunity opp : Trigger.new)
    {
        if(opp.LeadSource == '' && opp.HS_Original_Source__c != '' && opp.HS_Original_Source__c != 'OFFLINE')
        {
          if(opp.HS_Original_Source__c == 'ORGANIC SEARCH')
          {
               opp.LeadSource = 'Organic Search';
               System.debug('organic search');
           }
           /*
              if(opp.HS_Original_Source__c == 'next value')
          {
               opp.LeadSource = 'next value';
               System.debug('next value');
           }
           
           
            */
          
          //Put each updated record in the list created
          oppList.add(opp);  
        }
        
 }
 }

Any help would be much appreciated.
Best Answer chosen by Connor Reilly
Deepali KulshresthaDeepali Kulshrestha
Hi Connor,

I've gone through your code and you should do following changes or you can replace your code from below code:

(Note:--> if you are creating trigger before insert or before update ,then you should do changes on the trigger.new list only.
          Donot create another list for update)

trigger LeadSourceTrigger on Opportunity (before update, before insert) 
{

if((trigger.isInsert && trigger.isBefore) || (trigger.isUpdate && trigger.isBefore)) 
{
for(Opportunity opp : Trigger.new)
{
    if((opp.LeadSource == '' || opp.LeadSource==null) && (opp.HS_Original_Source__c != '' || opp.HS_Original_Source__c!=null)&& opp.HS_Original_Source__c != 'OFFLINE')
  {

      if(opp.HS_Original_Source__c == 'ORGANIC SEARCH')
      {
           opp.LeadSource = 'Organic Search';
           System.debug('organic search');
       }

          if(opp.HS_Original_Source__c == 'next value')
      {
           opp.LeadSource = 'next value';
           System.debug('next value');
       }

      }
      }

}
}


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com
 

All Answers

Naveen IlaNaveen Ila

Hi Connor Reilly, 

Are you able to see debug that we have in line no 13 ?

I also suggest to use String functions like String.isNotBlank since, the field LeadSource might be null in some cases. 
Use equalsIgnoreCase method when comparing two string variables. 



Regards,
Naveen Ila

Ajay K DubediAjay K Dubedi
Hi Connor,

The code below works fine, Use this code it auto-populate the LeadSource field from another picklist(HS_Original_Source__c) field:
 
//Trigger

trigger test_trigger on Opportunity (before update, before insert)
{
    List<Opportunity> oppList = new List<Opportunity>();
    //Go through the updated records coming into the database and update their custom fields 
    //according to the updated lead source 
    for(Opportunity opp : Trigger.new)
    {
        if(opp.LeadSource == Null && opp.HS_Original_Source__c != Null && opp.HS_Original_Source__c == 'ORGANIC SEARCH')
        {
            opp.LeadSource = 'Organic Search';
            System.debug('organic search');
        }
        
        //Put each updated record in the list created
        oppList.add(opp);  
    }
}


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com
Deepali KulshresthaDeepali Kulshrestha
Hi Connor,

I've gone through your code and you should do following changes or you can replace your code from below code:

(Note:--> if you are creating trigger before insert or before update ,then you should do changes on the trigger.new list only.
          Donot create another list for update)

trigger LeadSourceTrigger on Opportunity (before update, before insert) 
{

if((trigger.isInsert && trigger.isBefore) || (trigger.isUpdate && trigger.isBefore)) 
{
for(Opportunity opp : Trigger.new)
{
    if((opp.LeadSource == '' || opp.LeadSource==null) && (opp.HS_Original_Source__c != '' || opp.HS_Original_Source__c!=null)&& opp.HS_Original_Source__c != 'OFFLINE')
  {

      if(opp.HS_Original_Source__c == 'ORGANIC SEARCH')
      {
           opp.LeadSource = 'Organic Search';
           System.debug('organic search');
       }

          if(opp.HS_Original_Source__c == 'next value')
      {
           opp.LeadSource = 'next value';
           System.debug('next value');
       }

      }
      }

}
}


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com
 
This was selected as the best answer
Connor ReillyConnor Reilly
Thanks everyone! This was very helpful. I appreciate it.