You need to sign in to do that
Don't have an account?
Danielle 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
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
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.
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.
Is this code for a trigger or Apex class? I am still having issues.
Thanks.
This is for Apex Class.
Thanks.
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.: []: ()
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());
}
}
}
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' )) {