Here in this post I Will let you know how you can convert your leads into account , contact and opportunities by writing a short apex code .Most of us familiar with lead conversion using salesforce but what if there is any requirement that is demanading auto conversion of lead via apex.
Dont worry I have a solution for you . Here is the code snippet you have to just use the below code to convert leads using apex.
So here i am using a class named LeadConversion and i am calling this class via trigger so when any lead is inserted . a trigger is fired and convert that lead to contact and account .
here is the code snippets
For Trigger : AutoConvertLead
Trigger AutoConvertLead on Lead(After Insert) { For(Lead ld : trigger.new){ // here we are calling our class method to convert this lead AutoConvertLead.leadconversion(ld.id) } }
Apex Class :
Public class AutoConvertLead(){ public static void leadConversion(string leadid){ Database.LeadConvert Leadconvert = new Database.LeadConvert(); // here pass the leadid which need to be converted Leadconvert.setLeadId(leadid); LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1]; Leadconvert.setConvertedStatus(convertStatus.MasterLabel); Leadconvert.setDoNotCreateOpportunity(False); //Remove this line if you want to create an opportunity from Lead Conversion Database.LeadConvertResult Leadconverts = Database.convertLead(Leadconvert); System.assert(Leadconverts.isSuccess()); } }
Hope This will help you good luck …
Hits: 629