-1

Want to write Test case and unit test for this trigger

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

1 Answer 1

0

if you insert/update the lead the trigger will get coverge

Test Class:

 @isTest
 public class ConvertLead_Test{
  public static testmethod void testLead(){
  Lead le=new Lead();
   le.Company = 'JohnMiller';
   le.LastName = 'Mike';
   le.Status = 'Open';
     //add all the required fields
    le.isConverted=false;
   insert le;
   //update a field in lead
     le.LastName='John';
    update le;

Please check Trailead modules for basic understating for test class:

https://trailhead.salesforce.com/en/content/learn/modules/apex_testing/apex_testing_intro

6
  • Thank you Friend Commented Mar 31, 2020 at 7:56
  • If it resolves your query.Please make it as a answer
    – Annappa PH
    Commented Mar 31, 2020 at 8:58
  • But New lead is not creating after the test success Commented Mar 31, 2020 at 9:38
  • /What do you mean by newLead?
    – Annappa PH
    Commented Mar 31, 2020 at 9:40
  • While i test the code above this page.It should create a lead know? Commented Mar 31, 2020 at 9:46

Not the answer you're looking for? Browse other questions tagged .