You need to sign in to do that
Don't have an account?
Abhilash Mishra 13
Auto converting lead with trigger
Hi guys, I have been trying to write a trigger to convert lead if someone changes its status to closed converted the code is:
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger WkAbhi.convertlead caused an unexpected exception, contact your administrator: WkAbhi.convertlead: execution of AfterUpdate caused by: System.DmlException: ConvertLead failed. First exception on row 0; first error: UNKNOWN_EXCEPTION, System.DmlException: Update failed. First exception on row 0 with id 00Q28000001ovz7EAA; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, WkAbhi.convertlead: execution of AfterUpdate caused by: System.DmlException: ConvertLead failed. First exception on row 0; first error: CANNOT_UPDATE_CONVERTED_LEAD, cannot reference converted lead: [] Trigger.WkAbhi.convertlead: line 17, column 1: [] (System Code) : []: Trigger.WkAbhi.convertlead: line 17, column 1
Please help....
trigger convertlead on Lead ( after update ,after insert) { list<lead> mylead= trigger.new; for(lead l : mylead) { if(l.status=='Closed - Converted') { Database.LeadConvert lc = new Database.LeadConvert(); lc.setConvertedStatus('Closed - Converted'); lc.setLeadId(l.id); Database.LeadConvertResult lcr = Database.convertLead(lc); System.debug(lcr.isSuccess()); } } }its giving follwoing error:
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger WkAbhi.convertlead caused an unexpected exception, contact your administrator: WkAbhi.convertlead: execution of AfterUpdate caused by: System.DmlException: ConvertLead failed. First exception on row 0; first error: UNKNOWN_EXCEPTION, System.DmlException: Update failed. First exception on row 0 with id 00Q28000001ovz7EAA; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, WkAbhi.convertlead: execution of AfterUpdate caused by: System.DmlException: ConvertLead failed. First exception on row 0; first error: CANNOT_UPDATE_CONVERTED_LEAD, cannot reference converted lead: [] Trigger.WkAbhi.convertlead: line 17, column 1: [] (System Code) : []: Trigger.WkAbhi.convertlead: line 17, column 1
Please help....
Try the following code it will be work.
Let me know if you need any help regarding this.
Thanks,
Vijay
Abhilash Mishra 13
your code is absolutely fine,
I'm assuming that this also included an error Id? If so, your only real option is to reach out to Salesforce support with the error id. Typically this type of thing will happen if there is a problem with the back end schema or something similar.
to to prevent it from attempting to convert the lead a second time.
Thanks
I have tried autoLeadConversion and it is working fine. My query is, i have created one custome field "Account Manager" (lookup relation with user) on Lead object. When AutoLeadConversion trigger will run then New Opportunity, New Account and Contact is creating after that I want "Account Manager" (User Lookup) field Update (SetOwnerId) as a Owner of New Opportunity, New Account & New Contact.
Can you help me ?
I have tried autoLeadConversion (Account and Contact) When Status is Closed-Converted:
Class:
public class LeadConversionHandler {
public void leadConversionMethod(List<Lead> lstLead) {
List<Contact> lstContact = new List<Contact>();
List<Account> lstAccount = new List<Account>();
system.debug('Trigger Content '+lstLead);
if(!lstLead.isEmpty()) {
for(Lead objLead :lstLead) {
if(objLead.Status == 'Closed - Converted') {
Account objAccount = new Account();
objAccount.Name = objLead.Company;
lstAccount.add(objAccount);
Contact objContact = new Contact();
objContact.FirstName = objLead.FirstName;
objContact.LastName = objLead.LastName;
// objContact.AccountId = objLead.ConvertedAccountId;
objContact.Email = objLead.Email;
lstContact.add(objContact);
system.debug('Contact Object '+objContact);
}
}
insert lstContact;
insert lstAccount;
system.debug('List of Contact '+lstContact);
}
}
}
Trigger:
trigger LeadConversionTrigger on Lead (After insert,After update) {
system.debug('In Trigger');
LeadConversionHandler objLeadConversionHandler = new LeadConversionHandler();
objLeadConversionHandler.leadConversionMethod(Trigger.new);
}
If it is helping you then please mark as best.