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
Danielle Pulley 10Danielle Pulley 10 

Is there a way to automatically convert a lead to an account and contact but not opportunity?

Hello,

is there a way way to convert a lead that comes in from web to lead automatically? 
If the lead status is new and lead source is advertisement then we want the lead to convert automatically into an account  and contact but not an opportunity. 

I am new to apex and figure this this may need code, how should this be written or is it possible? 

Thanks, 
Danielle

 
shashikant pandeyshashikant pandey
Hi Danielle Pulley,
Use this code:-
public class LeadClass {
    public static void doConvert(Id leadId){ 
    Database.LeadConvert lc = new Database.LeadConvert();
    lc.setLeadId(leadId);

    Lead convertStatus = [SELECT Id,  LeadSource FROM Lead WHERE LeadSource = 'advertisement ' LIMIT 1];
    lc.setConvertedStatus(convertStatus.LeadSource);

    Database.LeadConvertResult lcr = Database.convertLead(lc);
    System.assert(lcr.isSuccess());
    }
}

If you feel it is relivent then choose best answer.
Thanks.
Malni Chandrasekaran 2Malni Chandrasekaran 2
Danielle, 
As Shashi has mentioned there is a Le adConvert Class which helps to automate the lead convertion. You may use "isDoNotCreateOpportunity()" method for not to create opportunity.

You may please refer the link, for more details
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_dml_convertLead.htm

To automate this, you may write a insert /update trigger or use batch job to do it on a daily basis.

Please mark it as solved if this helps to solve your problem.
Danielle Pulley 10Danielle Pulley 10
Shashikant, 

Is this code for a trigger or Apex class? I am still having issues. 

Thanks.
shashikant pandeyshashikant pandey
Hi Danielle Pulley,
This is for Apex Class.

Thanks.
shashikant pandeyshashikant pandey
Even you can use trriger Insert/Update for this.
Danielle Pulley 10Danielle Pulley 10
I am still getting errors. what is wrong? Is says an existing contact but this is on a new lead being created. 


trigger AutoConvertLeads on Lead (after insert) {

List<String> LeadNames = new List<String>{};
    LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];

   
for(Lead myLead: Trigger.new){
 if((myLead.isconverted==false) ) {
Database.LeadConvert lc = new database.LeadConvert();
        lc.setLeadId(myLead.Id);
        lc.setConvertedStatus(convertStatus.MasterLabel);
        Database.ConvertLead(lc,true);
        lc.setDoNotCreateOpportunity(true);
        Database.LeadConvertResult lcr = Database.convertLead(lc);
        System.assert(lcr.isSuccess());
     system.debug('lead is converted');
        }
        }



ERROR:

Apex trigger AutoConvertLeads caused an unexpected exception, contact your administrator: AutoConvertLeads: execution of AfterInsert caused by: System.DmlException: ConvertLead failed. First exception on row 0; first error: CANNOT_UPDATE_CONVERTED_LEAD, This lead was already converted to the contact Pulley on 8/23/2017.: []: ()
Danielle Pulley 10Danielle Pulley 10
I now got it to work. But how do I add more than one lead source? I only want it to run when the lead source is Academy Registration, User Registration or Organization Registration. 

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.LeadSource = 'Academy Registration')) {
Database.LeadConvert lc = new database.LeadConvert();
        lc.setLeadId(myLead.Id);
        lc.convertedStatus = 'Qualified';
        //Database.ConvertLead(lc,true);
        lc.setDoNotCreateOpportunity(true);
        Database.LeadConvertResult lcr = Database.convertLead(lc);
        System.assert(lcr.isSuccess());
        }
        }
}
Malni Chandrasekaran 2Malni Chandrasekaran 2
Danielle,
Please try replacing
 if((myLead.isconverted==false) && (myLead.LeadSource = 'Academy Registration')) {

with

 if((myLead.isconverted==false) && (myLead.LeadSource = 'Academy Registration'  || myLead.LeadSource = 'User Registration'  || myLead.LeadSource = 'Organization Registration' )) {