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
PetyaPetya 

Automatically create account, opportunity and contact after incoming lead from the app exchange

Hello 

 

Automatically create account, opportunity and contact after incoming lead from the app exchange 

We are using custom objects for opportunity. We receive Leads in our system, when someone saves our app from the AppExchange.

We would like to convert immediately this leads to account, contact and opportunity (custom object).

 

Any ideas how to achieve this?

 

Thanks

 

Best Answer chosen by Admin (Salesforce Developers) 
ForceComForceCom

Petya, 

 

Please find below a sample code which you can expand upon. 

 

trigger leadconvert on Lead (after insert) {
//Declare a set.
set<String> emailset=new set<String>();

for(Lead mylead : Trigger.New){
              
              
              Database.LeadConvert lc = new database.LeadConvert();
              lc.setLeadId(mylead.id);
              LeadStatus convertStatus = [Select Id, MasterLabel from LeadStatus where IsConverted=true limit 1];
              lc.doNotCreateOpportunity=false;
              lc.setConvertedStatus(convertStatus.MasterLabel);

              Database.LeadConvertResult lcr = Database.convertLead(lc);
}
}
 
This code will convert your lead to contact, account and opportunity. 
 
Thanks,
Dharani
 

All Answers

wt35wt35

You will need Apex coding for this requirement.

An Apex trigger on the Lead object that would leverage the Database.LeadConvert class  for the Account and Contact creation:

 http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_dml_convertLead.htm

 

As you use custom objects for opportunities, you would need to build another method for the Lead -> custom object conversion.

ForceComForceCom

Petya, 

 

Please find below a sample code which you can expand upon. 

 

trigger leadconvert on Lead (after insert) {
//Declare a set.
set<String> emailset=new set<String>();

for(Lead mylead : Trigger.New){
              
              
              Database.LeadConvert lc = new database.LeadConvert();
              lc.setLeadId(mylead.id);
              LeadStatus convertStatus = [Select Id, MasterLabel from LeadStatus where IsConverted=true limit 1];
              lc.doNotCreateOpportunity=false;
              lc.setConvertedStatus(convertStatus.MasterLabel);

              Database.LeadConvertResult lcr = Database.convertLead(lc);
}
}
 
This code will convert your lead to contact, account and opportunity. 
 
Thanks,
Dharani
 
This was selected as the best answer
PetyaPetya

thank you very much! I will check this

PetyaPetya
many thanks !