You need to sign in to do that
Don't have an account?
Connor 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.
Any help would be much appreciated.
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.
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
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
The code below works fine, Use this code it auto-populate the LeadSource field from another picklist(HS_Original_Source__c) field:
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
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
how to write trigger for the status(field) in order object to get updated to 'invoice generated' whenever a new record in invoice(custom object) is created