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
alxalx 

Test Coverage for Lead Conversion Trigger

Hey everyone,

 

Having a bit of trouble getting coverage for a trigger i wrote on Lead Conversion. I think a big problem i'm facing is that i don't completely understand test coverage conceptually.

 

 

Here's my trigger, it's working in Sandbox 100%. It basically fills in some fields in the resulting opportunity during lead conversion and inserts a partner as well.

trigger ResellerLeadConvert on Lead (after update) {
 
  // no bulk processing; will only run from the UI
  if (Trigger.new.size() == 1) {
 
    if (Trigger.old[0].isConverted == false && Trigger.new[0].isConverted == true && (Trigger.old[0].LeadSource == 'ADPResellerDeal' || Trigger.old[0].LeadSource == 'ResellerDeal' || Trigger.old[0].LeadSource == 'ADPPartnerReferral')) {
 
      // if a new opportunity was created
      if (Trigger.new[0].ConvertedOpportunityId != NULL) {
 
        // update the converted opportunity with some text from the lead
        Opportunity opp = [Select o.Id, o.LeadSource, o.CloseDate, o.ADP_Deal__c, o.NextStep, o.Portal_Status__c from Opportunity o Where o.Id = :Trigger.new[0].ConvertedOpportunityId];
        opp.LeadSource = Trigger.old[0].LeadSource;
        opp.ADP_Deal__c = 'No';
        if(Trigger.old[0].LeadSource == 'ADPPartnerReferral' || Trigger.old[0].LeadSource == 'ADPResellerDeal'){
            opp.ADP_Deal__c = 'Yes';
        }

        opp.NextStep = 'Rep to qualify';
        opp.Portal_Status__c = 'This Oppty is a new sale / will create a new portal';
        
        date fwdCloseDate = date.today();
        opp.CloseDate = fwdCloseDate.addDays(60);
        update opp; 
      
      // gather Campaign information from lead   
  List<CampaignMember> ResellerMembers = new List<CampaignMember>();
        ResellerMembers = [Select LeadId, z_CoSponsorAccID__c from CampaignMember where LeadId = :Trigger.old[0].Id];
        Map<Id, Id> mapCampaignMember = new Map<Id, Id>();
        
        for(CampaignMember m : ResellerMembers){
            mapCampaignMember.put(m.LeadId, m.z_CoSponsorAccID__c);
        }
        
      // if lead has a campaign and campaign is tied to a co-sponosor account, a partner will be created  
    if(mapCampaignMember.get(Trigger.new[0].Id)!= NULL){
    Partner newPart = new Partner();
        newPart.AccountToId = mapCampaignMember.get(Trigger.new[0].Id);
        newPart.OpportunityId = opp.Id;
        newPart.IsPrimary = true;
        newPart.Role = 'VARS/Reseller';
        if(Trigger.old[0].LeadSource == 'ADPPartnerReferral'){
            newPart.Role = 'Referral Partner - Sourced';
        }
        insert newPart;
    }
       
 
      }         
 
    }
 
  }     
 
}

 

Here's my test class. I got as far as creating the elements that the trigger covers (Lead, Campaign, CampaignMember, the Campaign's Account) but i'm unclear as to how to 'test' the lead conversion part.

 

@isTest
private class testResellerLeadConvert {
    static testMethod void testConversion() {

    
        Lead L = new Lead(Company ='ABC Co', Email = '123@123.com', LastName = 'Freeman', LeadSource = 'ADPPartnerReferral', NumberOfEmployees = 100, Phone = '1234567', 
            Partner_Salesperson_Email_Address__c = '123@123.com',
            Partner_Salesperson_Contact_Details__c ='x',
            Opportunity_Pain_Compelling_Event__c = 'x',
            Opportunity_Timeline__c = 'x',
            Opportunity_Executive_Sponsor_s__c= 'x',
            IsConverted = TRUE);
        insert L;

        Account ResellerAcc = new Account(Name= 'BlackMesa');
        insert ResellerAcc;
        
        Campaign C = new Campaign(Name ='BMCampaign', Co_Sponsoring_Account__c = ResellerAcc.Id,
            Campaign_Vendor__c = 'ADP',
            Type = 'Referral Program (Opportunity Leads)',
            Topic__c = 'test topic',
            Campaign_Country__c = 'USA');
        insert C;
        
        CampaignMember CMember = new CampaignMember(LeadId = L.Id, Campaign = C, CampaignId = C.Id);
        insert CMember;
        
        
        Test.startTest();
        
        Database.LeadConvert lc = new Database.LeadConvert();
        lc.setLeadId(L.Id);
        
        Database.LeadConvertResult lcr = Database.convertLead(lc);
        System.assert(lcr.isSuccess());

        Test.stopTest();
        
    
    
    
    
    
    }
}

 

Any help is greatly appreciated.

Best Answer chosen by Admin (Salesforce Developers) 
Saikishore Reddy AengareddySaikishore Reddy Aengareddy

oops.. these two line

 

LeadStatus convertStatus = [Select Id, MasterLabel from LeadStatus where IsConverted=true limit 1];

lc.setConvertedStatus(convertStatus.MasterLabel);

All Answers

Saikishore Reddy AengareddySaikishore Reddy Aengareddy

In your test class you are trying to create a converted lead itself... and then trying to convert a converted lead..

 

Just remove IsConverted = TRUE and see it it works..

 

Thanks, Sam

alxalx

Killed that line, getting this error:

 

Do i have to define the convertedStatus after i start the test method?

 

 

Error MessageSystem.DmlException: ConvertLead failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, convertedStatus is required: []
Stack Trace

Class.testResellerLeadConvert.testConversion: line 38, column 1

Saikishore Reddy AengareddySaikishore Reddy Aengareddy

Add these two lines just below lc.setLeadId

 

convertStatus = [Select Id, MasterLabel from LeadStatus where IsConverted=true limit 1];

lc.setConvertedStatus(convertStatus.MasterLabel);

Saikishore Reddy AengareddySaikishore Reddy Aengareddy

oops.. these two line

 

LeadStatus convertStatus = [Select Id, MasterLabel from LeadStatus where IsConverted=true limit 1];

lc.setConvertedStatus(convertStatus.MasterLabel);

This was selected as the best answer
alxalx

Sam,

 

Awesome, it 'Passed' on the Apex Test Results.

 

Do i need to write anything after the Test.stopTest(); to check if the resulting account, opportunity are correct?

 

Thanks!

Saikishore Reddy AengareddySaikishore Reddy Aengareddy

Not required... If you want you can

alxalx

Nice. Thanks dude.