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
Mayank_JoshiMayank_Joshi 

Unable to use Convert lead () Method using Trigger

Hi Team,

 

I am receiving below error :  

 

FATAL_ERROR|System.DmlException: ConvertLead failed. First exception on row 0; first error: INVALID_STATUS, invalid convertedStatus: Qualified: []

 

 I am not sure as this trigger is compiled successfully .I want to create Opportunity as well so I commented out "lc.setDoNotCreateOpportunity(false); " this statement .

  

Below is the complete trigger :

 

trigger LeadConvert on Lead (after insert,after update) {
for(Lead myLead : Trigger.new)
  {
   if((myLead.isconverted==false) && (myLead.Status == 'Open - Not Contacted'))
     //if((myLead.isconverted==false))
   {
    Database.LeadConvert lc = new database.LeadConvert();
    lc.setLeadId(myLead.Id);
    //lc.setconvertedStatus = 'Qualified';        //Database.ConvertLead(lc,true); 
    lc.setConvertedStatus('Qualified');
    //lc.setDoNotCreateOpportunity(false);  
    Database.LeadConvertResult lcr = Database.convertLead(lc);
    System.assert(lcr.isSuccess());     
   }
  }
}

 

Can you help me out ?

 

Thanks ,

Best Answer chosen by Admin (Salesforce Developers) 
Mayank_JoshiMayank_Joshi

No worries Sanj (I have resolve it by using statement if (lead.isConverted == false) //to prevent recursion)  And BTW thanks for looking into my issue  ,

 

I am now able to convert lead using Convert lead() Method . I am posting my trigger just in case if some body need this :

 

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

 

Regards,

 

All Answers

sanjdevsanjdev

Hi Mayank,

 

Please follow the below step and your trigger will work as expected.

 

1. Create a Picklist value as "Qualified" in the field Lead Status of Lead Object and check the field converted to True 

 

Mark it as resolved, if it helps you.

 

Sanj

Mayank_JoshiMayank_Joshi

No worries Sanj (I have resolve it by using statement if (lead.isConverted == false) //to prevent recursion)  And BTW thanks for looking into my issue  ,

 

I am now able to convert lead using Convert lead() Method . I am posting my trigger just in case if some body need this :

 

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

 

Regards,

 

This was selected as the best answer
PrasadVRPrasadVR

Hello Joshi,

 

    Is it possible to attach an exicting account like standard lead conversion process,here it will create the new account and contact for that one.....I don't want like this i want to add to an exciting account.