You need to sign in to do that
Don't have an account?

Auto-populating the standard 'Lead Source' field with part of a string
I want to auto-populate the standard 'Lead Source' (picklist) field with a part of the string stored in another field called the Source Tracking Code. The Source Tracking Code looks something like this -- Offline>TradeShow>SHRM2018>>
For this I have created an Apex class to break the string into parts and then search for the part which matches the picklist values. Please note that I have created a custom picklist field Lead Source3 (using instead of Lead Source) for Testing
public class LeadSource {
public static void LeadSource(){
List<Lead> l = new List<Lead>();
for(Lead ls:l)
{
String alpha = ls.Manticore_Full_Promotion_Code__c;
List<String> lstAlpha = alpha.split('>');
Set<String> convertInSet = new Set<String>(lstAlpha);
String FinalValue;
if(convertInSet.contains('401k Exchange')){
FinalValue = '401k Exchange';
}
else if(convertInSet.contains('Affiliate')){
FinalValue = 'Affiliate';
}
else if(convertInSet.contains('Bing')){
FinalValue = 'Bing';
}
ls.Lead_Source3__c = FinalValue;
}
}
}
After this I created a Trigger to run this Apex class as below
trigger LeadSource on Lead (before insert, before update) {
for(Lead myLeads:Trigger.new){
LeadSource.LeadSource();
}
}
I am new to Programming and to Salesforce, could somebody please help me figure out why I am not able to get anything populated on the Laed Source3 field.
Thanks,
Tanushree
For this I have created an Apex class to break the string into parts and then search for the part which matches the picklist values. Please note that I have created a custom picklist field Lead Source3 (using instead of Lead Source) for Testing
public class LeadSource {
public static void LeadSource(){
List<Lead> l = new List<Lead>();
for(Lead ls:l)
{
String alpha = ls.Manticore_Full_Promotion_Code__c;
List<String> lstAlpha = alpha.split('>');
Set<String> convertInSet = new Set<String>(lstAlpha);
String FinalValue;
if(convertInSet.contains('401k Exchange')){
FinalValue = '401k Exchange';
}
else if(convertInSet.contains('Affiliate')){
FinalValue = 'Affiliate';
}
else if(convertInSet.contains('Bing')){
FinalValue = 'Bing';
}
ls.Lead_Source3__c = FinalValue;
}
}
}
After this I created a Trigger to run this Apex class as below
trigger LeadSource on Lead (before insert, before update) {
for(Lead myLeads:Trigger.new){
LeadSource.LeadSource();
}
}
I am new to Programming and to Salesforce, could somebody please help me figure out why I am not able to get anything populated on the Laed Source3 field.
Thanks,
Tanushree
Couple of points:
1) You do not need to do a split on Manticore_Full_Promotion_Code__c to check whether the value matches or not.
2) You need to pass Trigger.new from trigger to the method in handler class.
Please use below code and mark it as Solved if it works for you. Thanks.
All Answers
Your code is correct. But you have to pass the trigger.new to the method name lead source.
PFB the code:
Hope it helps. If it helps, mark it as the best answer.
Regards,
Navin S
Couple of points:
1) You do not need to do a split on Manticore_Full_Promotion_Code__c to check whether the value matches or not.
2) You need to pass Trigger.new from trigger to the method in handler class.
Please use below code and mark it as Solved if it works for you. Thanks.