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
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student 

Lead Conversion Trigger

Hey there,

 

I have this trigger, except it will not let me deploy it via changesets without testing the code coverage. I need to test for this, I was wondering if someone could help me out with some code in order to test the trigger?

 

The trigger is written as:

 

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

 

How would I also go about removing the creation of an opportunity? Do i just remove the section that goes:

 

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

 

Any help I can get would be very much appreciated.

 

Mikie

 

Best Answer chosen by Admin (Salesforce Developers) 
CoolSurenCoolSuren

Hi miki,

 

By using the below code snnipet u can test your trigger. and u can deploy the trigger. just copy thic code and create a new apex class. then run the test, now u can see the code coverage.

 

@isTest(seealldata=true)
private class ConvertLead_Test{
    static testMethod void invenCheck(){
        Lead a = new Lead(LastName = 'TestLead', Company = 'Test', Status = 'Open - Not Contacted', isConverted = false);
        insert a;  

   }

}

All Answers

Avidev9Avidev9

remove that line and replace them with

 

lc.setDoNotCreateOpportunity(true);

 

Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student

Thankyou very much. How would I go about testing this?

CoolSurenCoolSuren

Hi miki,

 

By using the below code snnipet u can test your trigger. and u can deploy the trigger. just copy thic code and create a new apex class. then run the test, now u can see the code coverage.

 

@isTest(seealldata=true)
private class ConvertLead_Test{
    static testMethod void invenCheck(){
        Lead a = new Lead(LastName = 'TestLead', Company = 'Test', Status = 'Open - Not Contacted', isConverted = false);
        insert a;  

   }

}

This was selected as the best answer
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student

Thankyou sooo much Surren and Avid,

 

You have helped me out big time. I really appreciate it.

 

Mikie

Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student

Hey Surren,

 

I have recently deployed the trigger. Except when attempting to use the web to lead, the lead was not created because of error:

 

caused by: System.DmlException: ConvertLead failed. First exception on row 0; first error: UNKNOWN_EXCEPTION, System.DmlException: Update failed. First exception on row 0 with id 00Q9000000JsHTXEA3; first error: CANNOT_UPDATE_CONVERTED_LEAD, cannot reference converted lead: []

(System Code)
: []: Trigger.ConvertLead: line 14, column 1
    Lead Capture Page: Not available.

 

 

Do you know how I may rectify this?

 

Mikie