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
Deepak KalaDeepak Kala 

Apex code problem

Hi I am trying to implement a code which prefix Dr before lead name when a lead is inserted or updated. I was able to do with trigger.  Now I tried to implement it with handler pattern.

My trigger

trigger PrefixDoctorTrigger on Lead (before insert, before Update) {
PrefixDoctorTriggerHandler doctor = new PrefixDoctorTriggerHandler ();
//Before Insert
if(Trigger.isInsert && Trigger.isBefore){
doctor.OnBeforeInsert(Trigger.New);
}
// Before Update
else
if(Trigger.isUpdate && Trigger.isBefore){
doctor.OnBeforeUpdate( Trigger.old, Trigger.New, Trigger.newMap);
}
}

Handler class:

public with sharing class PrefixDoctorTriggerHandler {
public void OnBeforeInsert(List<Lead> newLeads) {
for(Lead l:newLeads){
l.FirstName = 'Dr.'+ l.Firstname;
}
}
public void OnBeforeUpdate(List<Lead> oldLeads, List<Lead> updatedLeads, map<Id, Lead> LeadMap) {
/*
what would come here*/
}
}

How should I write this portion of On Before Update, so that my code give correct result?

 

Regards,
Deepak

Best Answer chosen by Deepak Kala
ANUTEJANUTEJ (Salesforce Developers) 
Hi Deppak,

You can try the below snippet once:
 
My trigger

trigger PrefixDoctorTrigger on Lead (before insert, before Update) {
PrefixDoctorTriggerHandler doctor = new PrefixDoctorTriggerHandler ();
if(Trigger.isInsert && (Trigger.isBefore || trigger isupdate)){
doctor.DrPrefix(Trigger.New);
}
}

Handler class:

public with sharing class PrefixDoctorTriggerHandler {
public void DrPrefix(List<Lead> newLeads) {
for(Lead l:newLeads){
if(l.FirstName.startsWithIgnoreCase('Dr.')) continue;
            else{ l.FirstName = 'Dr.'+ l.Firstname; }
}
}
}

So the above functionality works fine for me I removed the before update and called the same method as we are not conditionally updating to add the dr prefix and also I have added a condition to check if the first name has a prefix before adding the Dr. again to firstname. I have done this as the end goal is the same for both updates and insert i.e., to just add a prefix.

You can modify this to fit your scenario i.e., for example of annual revenue is increased and if it is greater than a certain amount or something like that if that is the case then you can add update block and check and then add prefix.

Also, I have removed two conditional checks and changed it to only one in trigger code.

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.

All Answers

ANUTEJANUTEJ (Salesforce Developers) 
Hi Deppak,

You can try the below snippet once:
 
My trigger

trigger PrefixDoctorTrigger on Lead (before insert, before Update) {
PrefixDoctorTriggerHandler doctor = new PrefixDoctorTriggerHandler ();
if(Trigger.isInsert && (Trigger.isBefore || trigger isupdate)){
doctor.DrPrefix(Trigger.New);
}
}

Handler class:

public with sharing class PrefixDoctorTriggerHandler {
public void DrPrefix(List<Lead> newLeads) {
for(Lead l:newLeads){
if(l.FirstName.startsWithIgnoreCase('Dr.')) continue;
            else{ l.FirstName = 'Dr.'+ l.Firstname; }
}
}
}

So the above functionality works fine for me I removed the before update and called the same method as we are not conditionally updating to add the dr prefix and also I have added a condition to check if the first name has a prefix before adding the Dr. again to firstname. I have done this as the end goal is the same for both updates and insert i.e., to just add a prefix.

You can modify this to fit your scenario i.e., for example of annual revenue is increased and if it is greater than a certain amount or something like that if that is the case then you can add update block and check and then add prefix.

Also, I have removed two conditional checks and changed it to only one in trigger code.

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.
This was selected as the best answer
Deepak KalaDeepak Kala

Thanks a lot Anutej. That really helped me.

However I made a small change in trigger as it wasnt working for me in update condition. And now it worked.
trigger PrefixDoctorTrigger on Lead (before insert, before Update) {
PrefixDoctorTriggerHandler doctor = new PrefixDoctorTriggerHandler ();
//Before Insert
if(Trigger.isBefore){
if (Trigger.isInsert || Trigger.isUpdate)
{
doctor.DrPrefix(Trigger.New);
}
}
}

Regards,
Deepak