User Story
As a user, I want the Lead Source field of an Opportunity record to be automatically updated with the value "Other" when a new Opportunity is created and the Lead Source field is left blank, so that I can ensure that all Opportunities are properly categorized.
Additionally, I want the Type field to be automatically updated with the value "New Customer" when the Lead Source is "Phone Inquiry", so that I can easily identify which Opportunities are coming from this particular source.
Solution 1
trigger OppTrigger on Opportunity (before insert, before update) {
for(Opportunity opp : Trigger.new){
if(String.isBlank(opp.LeadSource)){
opp.LeadSource = 'Other';
}
if(opp.LeadSource.equalsIgnoreCase('Phone inquiry')){
opp.Type = 'New Customer';
}
}
}
Solution 2
trigger OppTrigger on Opportunity (before insert, before update) {
for(Opportunity opp : Trigger.new){
if(opp.LeadSource==null || opp.LeadSource==''){
opp.LeadSource='Other';
}
if(opp.LeadSource=='Phone inquiry'){
opp.LeadSource='New Customer';
}
}
}