+ Start a Discussion
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student 

Web to Lead failing as a result of AutoLead Convert - Many times posted and yet to find a solution

Hey there, 

 

I have a problem which I have posted many times and I am yet to find a solution. Normally, the threads eventually just die. If an idea of what I have tried is what is needed. Here is a  recent post:

 

http://boards.developerforce.com/t5/Apex-Code-Development/AutoLead-Convert-trigger-stopping-Web2Lead/m-p/645307#M119482

 

I have an auto-leadConvert trigger which works perfectly, however my system needs to be designed so every new client comes through the web 2 lead on the website, it is then converted automatically into an account and a contact. When the trigger is active, it simply prevents the web2lead from creating the lead. I assume this is because so many processes are happening simultaneously. I have two options:

 

1. I can alter my code, so it still allows web2lead before automatically converting. This is my code:

 

AutoConvertLead Trigger


trigger ConvertLead on Lead (after insert, after update) {
for (Lead lead : Trigger.new) {
if (lead.isConverted == false) //to prevent recursion
{

Database.LeadConvert lc = new Database.LeadConvert();
lc.setLeadId(lead.Id);

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());


}
}
}

 

Or

 

2. I have altered the code, it now has another if statement that says the 'convertReady' field is checked. The web2lead was now creating leads, except I put in a timetriggered field update for the ConvertReady field to update to checked an hour after record creation. The rule just never activates. This is my other code:

 


trigger ConvertLead on Lead (after insert, after update) {
for (Lead lead : Trigger.new) {
if (lead.isConverted == false) //to prevent recursion
{
if (lead.ConvertReady__c == true) //To allow web to lead
{

Database.LeadConvert lc = new Database.LeadConvert();
lc.setLeadId(lead.Id);

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());


}
}
}
}

 

Please help me with a solution

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

Did I ever offer you a solution to this?

I wrote this code for someone else. Try it out and see if it helps:

 

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

 

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());
            }
        }
    }
}

Test code should be the same as any test code you already have for conversion.

 

This method uses a future method to convert asynchronously, which frees up the lead and prevents web2lead failures (and other means of conversion, such as email2lead).

All Answers

RossGRossG

What's up buddy.  Let's start simple.  Your original post...the requirements are not clear to me.  

 

Explain your problem to me like I'm 5 years old.

sfdcfoxsfdcfox

Did I ever offer you a solution to this?

I wrote this code for someone else. Try it out and see if it helps:

 

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

 

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());
            }
        }
    }
}

Test code should be the same as any test code you already have for conversion.

 

This method uses a future method to convert asynchronously, which frees up the lead and prevents web2lead failures (and other means of conversion, such as email2lead).

This was selected as the best answer
RossGRossG

This guy fox is awesome.  Hoping his solution works for you.

Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student

Hey RossG,

 

 

Basically I have my AutoLead convert. It works, it converts a lead into an account and a contact. Except should a lead come into the system via a web to lead. I will get an email from salesforce saying the lead could not be created and nothing appears in the system. Nothing appears in leads, cotnacts or accounts.

Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student

Hey sfdcfox,

 

I tried the code. I assumed the first bit of code was the trigger and the second was an apex class that it runs out (not 100% sure on apex yet). Regardless, I put the first code into the apex trigger area and the second into the apex classes. I did this in sandbox and i created a lead and it was not converted automatically. I copied and pasted it...or was this code designed mainly for web 2 lead and email 2 lead...Which is perfect if I think about it, considering the fact that should someone create a lead the old fashioned way (new lead button), they may as well just be clicking convert anyway.

 

If this is the case, let me know and I will deploy it to production and give it a whirl...

 

Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student

Thank you both soo much for your time.

 

mikie

Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student

Hey Sfdcfox,

 

I deployed the trigger to production. It allows web2lead (in regards to lead creation) and it converts a lead should I create it using the New lead button. However it does not automatically convert the leads which come through web 2 lead. Unless there is a slight delay. Let me know your thoughts. 

 

Thanks

Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student

Sorry it took me so long to reply guys

sfdcfoxsfdcfox

This code works in my test org. The most likely suspect is a validation rule. Make sure that your validation rules and/or required fields aren't causing web to lead to fail conversion. You won't get an error unless you modify the code to send you an email when a lead fails to convert.

Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student

What I mean to say is that is the code meant to always automatically convert the Lead, it does not matter if its created by the new lead button or through web 2 lead, or does it automatically convert the lead should it be created via new lead button and just  allow web 2 lead (but not convert them).

 

Basically, what I need is for the leads that come in through Web 2 Lead to be automatically converted. The code as it works for me: It will automaticlaly convert a lead into an account and a contact upon saving from the new lead button...however, although it does allow web 2 lead to create leads, it will not automatically convert them. 

 

Is the code for converting web 2 lead or jsut for converting leads from new lead button? If the former, you think I should check on validation rules?

 

Thank you so much for your help

 

mikie

sfdcfoxsfdcfox
The trigger is agnostic of the lead's origin, meaning it doesn't matter where the lead comes from, it should automatically be converted. If it's failing, there's some sort of validation rule or trigger that's causing the conversion itself to fail.

Check your validation rules and triggers for leads, accounts, contacts, and possibly opportunities. If you're still having problems, try using the Debug Logs (Setup > Monitoring > Debug Logs).
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student

Thankyou so much sfdcfox,

 

Once again you were right and once again you have solved another one of my problems. It turns out it was getting stopped because of a workflow rule (time trigger), which is set to update a field which would fill the criteria of an old trigger in order to convert. Thankyou for your help.