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
Dev87Dev87 

Convert Lead - Apex

Hello, 
I have to convert lead to many opportunities (based on multiselect list Country) - creating as many opportunities as selected countries.
When converting a lead, there is always one more opportunity that is created (the standard opportunity).
How can I eliminate the creation of this opportunity?
Thanks
 
Raj VakatiRaj Vakati
You could do it as shown below  .
Take a look at the ConvertLead Operation. Specifically, the setDoNotCreateOpportunity method. Below is an example of how to convert a lead within Apex, without creating an opportunity.
 
public class LeadClass{
    public static void doConvert(Id leadId){

        Database.LeadConvert lc = new database.LeadConvert();
        lc.setLeadId(leadId);
        lc.setDoNotCreateOpportunity(True); //**IMPORTANT METHOD HERE**

        LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
        lc.setConvertedStatus(convertStatus.MasterLabel);
        Database.LeadConvertResult lcr = Database.convertLead(lc);
    }
}

 
Dev87Dev87
Thank you for your response.
But in my case, I have to create specific opportunities. My code allows the creation of these opportunities but there is always the standard opportunity that is created. If I add the method 'setDoNotCreateOpportunity', in my code there is no opportunity created.