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
NavneethNavneeth 

Lead to account conversion trigger

Hi i need a Lead Conversion Trigger code. The Trigger should automatically convert the leads to account. It should be an after insert trigger. Since i m new to apex development i am requesting you people to provide me the exact full code.

 

Also the Exact Skeleton of How the trigger should work is given below.

 

Please find the exact details/skeleton of lead conversion 

Trigger on Lead
Trigger on lead( after insert)
{
Get the trigger size (Integer)-

take list of type <database.leadStatus>---this list is used to hold the leads that needs to be converted

Query the lead Status  Isconverted=true(where condition)[ id,Masterlable, should be fetched]

iterate For( i=0;<triggerSize;i++)

{
Create new instance of database.leadconvert
 
set the following values to instance 

set the leadID("id from trigger"),
notCreateOpportunity to(" true")
set convertedstatus(this value from the leadStatus Query)

after setting the values to instance add it to the list of type<database.leadStatus>-declared at the top
}


//create Database.LeadConvertResult[] array — Shown below [you can use exact line shown below]

Database.LeadConvertResult[] leadResult= Database.convertLead(pass the listOfleads that needs to be converted,false);


//itereate the leadResult and check the system.debug to know the success or failure

 

sfdcfoxsfdcfox
trigger leadAutoConvert on Lead (after insert) {
	Database.LeadConvert[] conversions = new Database.LeadConvert[0];
    LeadStatus status = [SELECT Id,MasterLabel FROM LeadStatus WHERE IsConverted = TRUE LIMIT 1];
    for(Lead record:Trigger.new) {
        Database.LeadConvert convert = new Database.LeadConvert();
        convert.setLeadId(record.Id);
        convert.setDoNotCreateOpportunity(true);
        convert.setConvertedStatus(status.MasterLabel);
        conversions.add(convert);
    }
    Database.LeadConvertResult[] results = Database.convertLead(conversions,false);
    for(Database.LeadConvertResult result:results) {
        if(!result.isSuccess()) {
            System.debug(System.LoggingLevel.ERROR,'Failed to convert '+result.getLeadId()+' because '+result.getErrors()[0].getMessage());
        }
    }
}

 

NavneethNavneeth

Hey the lead will be inserted from a web to lead form and then the trigger should be able to convert the lead into an account. At present this code is not converting the lead to account. 

 

Please help asap


sfdcfox wrote:
trigger leadAutoConvert on Lead (after insert) {
	Database.LeadConvert[] conversions = new Database.LeadConvert[0];
    LeadStatus status = [SELECT Id,MasterLabel FROM LeadStatus WHERE IsConverted = TRUE LIMIT 1];
    for(Lead record:Trigger.new) {
        Database.LeadConvert convert = new Database.LeadConvert();
        convert.setLeadId(record.Id);
        convert.setDoNotCreateOpportunity(true);
        convert.setConvertedStatus(status.MasterLabel);
        conversions.add(convert);
    }
    Database.LeadConvertResult[] results = Database.convertLead(conversions,false);
    for(Database.LeadConvertResult result:results) {
        if(!result.isSuccess()) {
            System.debug(System.LoggingLevel.ERROR,'Failed to convert '+result.getLeadId()+' because '+result.getErrors()[0].getMessage());
        }
    }
}

 


 

NavneethNavneeth

I tried this code but the lead is not getting converted into account.

 

I used developer console to check the error.

 

I got the following error

 

Exec Anon Error
 
line 1, column 0: required (...)+ loop did not match anything at input 'trigger'

 

sfdcfoxsfdcfox

My logs didn't even show that error, but, in the spirit of being adventurous, I decided I'd try an async method instead:

 

public class delayedConversion{
    @future public static void convertLeads(Set<Id> leadIds) {
        Database.LeadConvert[] conversions = new Database.LeadConvert[0];
        LeadStatus status = [SELECT Id,MasterLabel FROM LeadStatus WHERE IsConverted = TRUE LIMIT 1];
        for(Id leadId:leadIds) {
            Database.LeadConvert convert = new Database.LeadConvert();
            convert.setLeadId(leadId);
            convert.setDoNotCreateOpportunity(true);
            convert.setConvertedStatus(status.MasterLabel);
            conversions.add(convert);
        }
        Database.LeadConvertResult[] results = Database.convertLead(conversions,false);
        for(Database.LeadConvertResult result:results) {
            if(!result.isSuccess()) {
                System.debug(System.LoggingLevel.ERROR,'Failed to convert '+result.getLeadId()+' because '+result.getErrors()[0].getMessage());
            }
        }
    }
}

 

trigger leadAutoConvert on Lead (after insert) {
    if(!System.isFuture()) {
    	delayedConversion.convertLeads(Trigger.newMap.keySet());
    }
}

This worked correctly for Web to Lead. It appears there's some sort of race condition with Web to Lead that prevents leads from being converted directly, so waiting until the Web to Lead transaction completes via @future works.