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
DustinLHDustinLH 

Test Class for Trigger (involves Lead conversion)

I modified the code from a trigger I found on here and created the following trigger. I deployed it to production via a change set, but am only getting 63% coverage. Can someone assist with a test class? I am not familiar enough with apex yet to figure this out.  Thanks!

 

trigger ConvertProspectReferral_Trigger on Lead (after update) {

  Map<Id, Lead> leadMap = new Map<Id,Lead>();
  Lead parent;
 
  for (Integer i = 0; i < Trigger.new.size(); i++){
    if (Trigger.new[i].IsConverted == true && Trigger.old[i].isConverted == false) {
      leadMap.put( Trigger.new[i].Id, Trigger.new[i]);
    }
  }
   
  if( leadMap.size() > 0 ) {
      Set<Id> leadIds = leadMap.keySet();
      List<Referral__c> allChildren =
        [select Id, Related_Opportunity__c, Client_Name__c, Related_Lead__c, RecordTypeId from Referral__c where Related_Lead__c in :leadIds];      
 
 System.debug(allChildren);
   
      for ( Referral__c child : allChildren ) {
        if ( leadMap.containsKey( child.Related_Lead__c ) ) {
           // lookup the parent lead
           parent = leadMap.get( child.Related_Lead__c );
           // update the fields on the child object
           child.related_opportunity__c = parent.ConvertedOpportunityId;
           child.client_name__c = parent.ConvertedAccountId;
           child.recordtypeid = '01280000000ErjI';
        }
      }

System.debug(allChildren);

    //try {
      update allChildren;
   // } catch( Exception e ) {
         // could put something here to notify on error
         // otherwise it fails silently
   // }
     
  }
}

 

MikeGillMikeGill

Please can you post the test class

DustinLHDustinLH

Mike,

 

I do not have a test class, I am in need of one. Sorry.

MikeGillMikeGill

Hi Dustin,

 

Sorry I took so long to reply.

 

You should always try and start with writing tests first. Then build up your code which eventually passes the test.

 

I have not tested this code as I have never needed to code around the lead conversion process as of yet. (also not setup for bulk BTW)

 

@isTest

private Class TestReferrals(){

  private static Id leadId;
   static (
     
Lead testLead = new Lead();
testLead.FirstName = 'Test First';
testLead.LastName = 'Test Last';
testLead.Company = 'Test Co';
insert testLead;
leadId = testLead.Id;

  )

static testMethod void Test_LeadConvert(){

test.Start();
Database.LeadConvert lc = new database.LeadConvert();
lc.setLeadId(testLead.id);

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

test.Stop();

}
   

}

 

I hope this gets you moving.

 

DustinLHDustinLH

Mike,

 

Thank you again for the reply. I am really new to this whole apex thing and have gotten no formal training, so I am trying to tie bits and pieces together where I can. I found the trigger on one of the posts in this forum and modified it and created it in my Sandbox. The trigger works perfectly so I thought I was golden, but then I ran into the lack of coverage issue. 

 

I copied/pasted your code and had to change a couple parenthesis to brackets and had to remove the () from after the name. Once I did that, I got an error that stated "Error: Compile Error: Variable does not exist: testLead.id at line 21 column 14".

 

If you are able to keep assisting, I would appreciate it. I am pasting the modified code below:

 

@isTest

private Class TestReferrals{

  private static Id leadId;
   static {
     
Lead testLead = new Lead();
testLead.FirstName = 'Test First';
testLead.LastName = 'Test Last';
testLead.Company = 'Test Co';
insert testLead;
leadId = testLead.Id;

  }

static testMethod void Test_LeadConvert(){

test.Start();
Database.LeadConvert lc = new database.LeadConvert();
lc.setLeadId(testLead.id);

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

test.Stop();

}
   

}

 

MikeGillMikeGill

it should have been leadId and not testLead.id - my fault a typo

 

Are you still struggling with it?

 

Where are you based?

DustinLHDustinLH

Mike,

 

Made the change and now get a new error: Error: Compile Error: Method does not exist or incorrect signature: test.Start() at line 19 column 1.

 

I am located in Aurora, IL (Chicago burbs).

 

@isTest

private Class TestReferrals{

  private static Id leadId;
   static {
     
Lead testLead = new Lead();
testLead.FirstName = 'Test First';
testLead.LastName = 'Test Last';
testLead.Company = 'Test Co';
insert testLead;
leadId = testLead.Id;

  }

static testMethod void Test_LeadConvert(){

test.Start();
Database.LeadConvert lc = new database.LeadConvert();
lc.setLeadId(leadId);

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

test.Stop();

}
   

}

 

MikeGillMikeGill

My fault - the test method is like this

 

test.StartTest();

Code

test.StopTest();

 

I would review the online documentation for Test Methods

DustinLHDustinLH

Mike,

 

I appreciate the help. This change allowed me to sucessfully save the class. It appears that my coverage only went from 63% to 67%, still under the 75% that I need. When I run a test on the class itself, it says 40% coverage.

 

I wish I could be of more help, but I really do not understand this stuff.

 

Dusty

MikeGillMikeGill

Would like to help, let's see if we can't have a webex / goto meeting next week and I will see what I can do.

DustinLHDustinLH

Mike,

 

I am free the remainder of the week. Let me know if you still have time. I appreciate your assistance on this.

 

Dusty

MikeGillMikeGill

add me to skype

 

mikegill74