+ Start a Discussion

Convert Lead - Apex

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?
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.setDoNotCreateOpportunity(True); //**IMPORTANT METHOD HERE**

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

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.