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
Travis Lee 1Travis Lee 1 

Assistance with Lead autoconvert Trigger

Hi there,

I'm attempting to cobble together some code for a lead convert trigger that will (after update, after insert) ensure the resulting Account's name is the Last Name plus the last 4 digits of the phone number and I'm a little stuck. For example, if you were converting a lead named John Smith and his phone number was 123-456-7890, the resulting account would be called "Smith-7890". The piece for the trigger to autoconvert works fine, but the portion updating the account is giving me from trouble. Any advice would be appreciated!
trigger LeadConvert on Lead (after insert,after update) {
//Bulkified
    List<String> LeadNames = new List<String>{};
        for(Lead myLead: Trigger.new){
         if((myLead.isconverted==false) && (myLead.Status == 'Scheduled Appointment')) {
            Database.LeadConvert lc = new database.LeadConvert();
            lc.setLeadId(myLead.Id);
            lc.convertedStatus = 'Scheduled Appointment';
        //Database.ConvertLead(lc,true);
            lc.setDoNotCreateOpportunity(true);
            Database.LeadConvertResult lcr = Database.convertLead(lc);
            System.assert(lcr.isSuccess());
            }
        }
        List <Account> account = [SELECT acc.Id, acc.Description FROM Account acc WHERE acc.Id = :Trigger.new.ConvertedAccountId];
        for (Account acc: account) {
    	acc.Name = Trigger.new.LastName + Trigger.new.RIGHT(Phone, 4);
        update acc;
    }
}

 
Best Answer chosen by Travis Lee 1
Magesh Mani YadavMagesh Mani Yadav
HI Travis,

I have updated your trigger there were some missing syntax.Try the below code should work now.
trigger LeadConvert on Lead (after insert,after update) {
//Bulkified
    
    List<String> LeadNames = new List<String>{};
        for(Lead myLead: Trigger.new){
         if((myLead.isconverted==false) && (myLead.Status == 'Scheduled Appointment')) {
            Database.LeadConvert lc = new database.LeadConvert();
            lc.setLeadId(myLead.Id);
            lc.convertedStatus = 'Scheduled Appointment';
        //Database.ConvertLead(lc,true);
            lc.setDoNotCreateOpportunity(true);
            Database.LeadConvertResult lcr = Database.convertLead(lc);
            System.assert(lcr.isSuccess());
            }
        }
        List <Account> account = [SELECT acc.Id, acc.Description FROM Account acc WHERE acc.Id = :Trigger.new[0].ConvertedAccountId];
        for (Account acc: account) {
        acc.Name = Trigger.new[0].LastName+'-'+String.valueOf(Trigger.new[0].Phone).RIGHT(4);
        update acc;
        }
}

 

All Answers

Magesh Mani YadavMagesh Mani Yadav
HI Travis,

I have updated your trigger there were some missing syntax.Try the below code should work now.
trigger LeadConvert on Lead (after insert,after update) {
//Bulkified
    
    List<String> LeadNames = new List<String>{};
        for(Lead myLead: Trigger.new){
         if((myLead.isconverted==false) && (myLead.Status == 'Scheduled Appointment')) {
            Database.LeadConvert lc = new database.LeadConvert();
            lc.setLeadId(myLead.Id);
            lc.convertedStatus = 'Scheduled Appointment';
        //Database.ConvertLead(lc,true);
            lc.setDoNotCreateOpportunity(true);
            Database.LeadConvertResult lcr = Database.convertLead(lc);
            System.assert(lcr.isSuccess());
            }
        }
        List <Account> account = [SELECT acc.Id, acc.Description FROM Account acc WHERE acc.Id = :Trigger.new[0].ConvertedAccountId];
        for (Account acc: account) {
        acc.Name = Trigger.new[0].LastName+'-'+String.valueOf(Trigger.new[0].Phone).RIGHT(4);
        update acc;
        }
}

 
This was selected as the best answer
Travis Lee 1Travis Lee 1
Magesh, this is fantastic! Thanks so much for your help.
Travis Lee 1Travis Lee 1
Hey Magesh,

Follow up to this. Is there a way to avoid "null pointer exception" errors when they don't fill in a phone number with this trigger? 

Thanks,

Travis
Magesh Mani YadavMagesh Mani Yadav
Hi Travis,

replace these lines
for (Account acc: account) {
        acc.Name = Trigger.new[0].LastName+'-'+String.valueOf(Trigger.new[0].Phone).RIGHT(4);
        update acc;
        }

with this one 
for (Account acc: account) {
if(Trigger.new[0].Phone!=null){
      acc.Name = Trigger.new[0].LastName+'-'+String.valueOf(Trigger.new[0].Phone).RIGHT(4);
}else{
      acc.Name = Trigger.new[0].LastName
}       
update acc;       
}

 
Magesh Mani YadavMagesh Mani Yadav
please add simicolon at the end of line 5.
Travis Lee 6Travis Lee 6
Thanks Magesh! You're a life saver