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
Tanushree Singh 11Tanushree Singh 11 

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
 
Best Answer chosen by Tanushree Singh 11
Ravi Dutt SharmaRavi Dutt Sharma
Hi 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.
 
trigger LeadTrigger on Lead (before insert) {
	
    if(Trigger.IsBefore && Trigger.isInsert){
        LeadTriggerHandler.populateLeadSource(Trigger.new);
    }
}
 
public class LeadTriggerHandler {
    
    public static void populateLeadSource(List<Lead> newLeads){
        for(Lead ld: newLeads){
            if(ld.Manticore_Full_Promotion_Code__c.contains('401k Exchange')){
                ld.LeadSource = '401k Exchange';
            }
            else if(ld.Manticore_Full_Promotion_Code__c.contains('Affiliate')){
                ld.LeadSource = 'Affiliate';
            }
            else if(ld.Manticore_Full_Promotion_Code__c.contains('Bing')){
                ld.LeadSource = 'Bing';
            }
        }
    }
}

 

All Answers

Navin Selvaraj23Navin Selvaraj23
Hi,

Your  code is correct. But you have to pass the trigger.new to the method name lead source.

PFB the code:
public class LeadSource {

public static void LeadSource(List<Lead> leadObj){

for(Lead ls:leadObj)
{
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) {

LeadSource.LeadSource(Trigger.New);

}

Hope it helps. If it helps, mark it as the best answer.

Regards,
Navin S
Ravi Dutt SharmaRavi Dutt Sharma
Hi 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.
 
trigger LeadTrigger on Lead (before insert) {
	
    if(Trigger.IsBefore && Trigger.isInsert){
        LeadTriggerHandler.populateLeadSource(Trigger.new);
    }
}
 
public class LeadTriggerHandler {
    
    public static void populateLeadSource(List<Lead> newLeads){
        for(Lead ld: newLeads){
            if(ld.Manticore_Full_Promotion_Code__c.contains('401k Exchange')){
                ld.LeadSource = '401k Exchange';
            }
            else if(ld.Manticore_Full_Promotion_Code__c.contains('Affiliate')){
                ld.LeadSource = 'Affiliate';
            }
            else if(ld.Manticore_Full_Promotion_Code__c.contains('Bing')){
                ld.LeadSource = 'Bing';
            }
        }
    }
}

 
This was selected as the best answer
Tanushree Singh 11Tanushree Singh 11
Thank you Navin and Anil for the quick turnaround! Your code did work