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
webdesignerjameel1.392198370693491E12webdesignerjameel1.392198370693491E12 

Automatically convert lead to contact with opportunity?


Hi,

I am Very new to SF... I need to do automatically convert lead to contact with opportunity. If contact is not exist we need to create new contact, if contact is exist we need not create contact, and this lead move into opportunity.

Please give Trigger Code for this task.

Thanks in advance.
Elie.RodrigueElie.Rodrigue
I guess you wont want to use a trigger, it will most likely be a button that will, through some means, run apex code. 

Have a look at the leadconvert class doc : 
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_dml_convertLead.htm

You will need your apex code to figure out if you need to create an account or not.

I know you would rather had a complete code solution, but you'll learn much more if you figure it out using clues :)

webdesignerjameel1.392198370693491E12webdesignerjameel1.392198370693491E12
Hi Elie,

Thanks for your replay, I have taken below this code form net.

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

String oppName = lead.Name;
lc.setOpportunityName(oppName);

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

}
}
}

this is converting lead to opportunity but not immediately. After adding lead this code executed and ” Converted Lead ” page showing with contact, account and Opportunity values. After clicking this value only this record move to opportunity.

Why we need to click, after that only record move to contact , account , opportunity. I need auto convert.

Please advise.