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
Edward EastEdward East 

Automatically convert Lead to Account if Status has changed to "signed".

I have just created a formassembly form which we sent to potential clients. Those potential clients are Leads and a lot of their information is in the database already. Once they have completed the form and returned it to us the Lead status automatically changes from "Identified" to "Signed". At that point I ould like to set up a trigger that immediately converts those leads into a Person - Account. Anyone knows how I can achieve this?
Best Answer chosen by Edward East
Dipak NikamDipak Nikam
Hi Edward,

Please try below code and make sure you have all required field populated for Account and Contact if any (Maybe through "Map Lead Fields" section ). 

trigger ConvertToAccountOnStatusSigned on Lead (after insert,after update) {
    
    LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
    
    List<Database.LeadConvert> lcList = new List<Database.LeadConvert>();
    
    for (Lead lead : Trigger.new) { 
        if (lead.isConverted == false && lead.Status=='Signed') {
            Database.LeadConvert lc = new Database.LeadConvert();
            lc.setLeadId(lead.Id);
            
            String oppName =  lead.Name;
            //if set true, no opportunity will be created
            lc.setDoNotCreateOpportunity(True);
            lc.setConvertedStatus(convertStatus.MasterLabel);
            lcList.add(lc);
         }
     }
     
     if (lcList.size() > 0 ){
        List<Database.LeadConvertResult> lcrList = Database.convertLead(lcList,false);
     }
}

Regards,
Dipak NIkam

All Answers

Dipak NikamDipak Nikam
Hi Edward,

Please check below, this can be of your help. 

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_dml_convertLead.htm?SearchType=Stem&Highlight=lead%7Cleading%7CLead%7Cleads%7CLeads%7C%7Cconvert%7Cconverting%7Cconverts%7Cconverted%7CConverting%7CConvert%7CConverts

Regards,
Dipak Nikam
Edward EastEdward East
I have found this script in a different discussion, but I'm not sure how applicable it is to my situation (sorry, I'm really bad at scripting), and the initial trigger is for sure not what I want (which is the status has to change to a value called "signed" before converting):

trigger ConvertLead on Lead (after insert, after update) {

    for (Lead lead : Trigger.new) {
      if (lead.isConverted == false) 

      {
       
        Database.LeadConvert lc = new Database.LeadConvert();
        lc.setLeadId(lead.Id);
       
        String oppName =  lead.Name;
        lc.setDoNotCreateOpportunity(True);

        LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
        lc.setConvertedStatus(convertStatus.MasterLabel);
       
        Database.LeadConvertResult lcr = Database.convertLead(lc);
        System.assert(lcr.isSuccess());
       
             
    }
  }
}
Dipak NikamDipak Nikam
Hi Edward,

Please try below code and make sure you have all required field populated for Account and Contact if any (Maybe through "Map Lead Fields" section ). 

trigger ConvertToAccountOnStatusSigned on Lead (after insert,after update) {
    
    LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
    
    List<Database.LeadConvert> lcList = new List<Database.LeadConvert>();
    
    for (Lead lead : Trigger.new) { 
        if (lead.isConverted == false && lead.Status=='Signed') {
            Database.LeadConvert lc = new Database.LeadConvert();
            lc.setLeadId(lead.Id);
            
            String oppName =  lead.Name;
            //if set true, no opportunity will be created
            lc.setDoNotCreateOpportunity(True);
            lc.setConvertedStatus(convertStatus.MasterLabel);
            lcList.add(lc);
         }
     }
     
     if (lcList.size() > 0 ){
        List<Database.LeadConvertResult> lcrList = Database.convertLead(lcList,false);
     }
}

Regards,
Dipak NIkam
This was selected as the best answer