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
vikas  malikvikas malik 

Needing assistance on a test class

Need help with a test class as I am still a newbie at APEX coding....

I have the following trigger>>

trigger CaseTriggers on Case (after insert, before update) {
   
    if(trigger.isInsert){
        list<Lead> ldlist = new List<Lead>();
        for(case cse : trigger.new){
            if((cse.Assigned_To__c == null || cse.Assigned_To__c  == '') && cse.CLASS_Application_Status__c == '0-Pending Incomplete'){
                Lead ld = new Lead();
                ld.FirstName = cse.Applicant_First_Name__c;
                ld.LastName = cse.Applicant_Last_Name__c;
                ld.Phone = cse.Applicant_Primary_Phone__c;
                //if(cse.Applicant_Primary_Phone__c != null)
                    //ld.Phone = '('+cse.Applicant_Primary_Phone__c.substring(0, 3) + ') ' +cse.Applicant_Primary_Phone__c.substring(3, 6) + '-' +
cse.Applicant_Primary_Phone__c.substring(6, 10);
                ld.Email = cse.Applicant_Email__c;
                ld.Company = 'SpringLeaf Financial';
                ld.State = cse.Applicant_State__c;
                ld.CLASS_Lead_Source__c = cse.CLASS_Lead_Source__c;
                ld.Have_Auto_Approval_Flag__c = cse.Have_Auto_Approval_Flag__c;             
                if( cse.Time_Zone__c == 'EST')                   
                    ld.LiveOpsApp__Lead_Timezone__c = '(GMT-05:00) Eastern Standard Time (America/New_York)';
                if( cse.Time_Zone__c == 'CST')                   
                    ld.LiveOpsApp__Lead_Timezone__c = '(GMT-06:00) Central Standard Time (America/Chicago)';               
                if( cse.Time_Zone__c == 'MST')                   
                     ld.LiveOpsApp__Lead_Timezone__c = '(GMT-07:00) Mountain Standard Time (America/Denver)';
                if( cse.Time_Zone__c == 'PST')                   
                     ld.LiveOpsApp__Lead_Timezone__c = '(GMT-08:00) Pacific Standard Time (America/Los_Angeles)';
                ldlist.add(ld);              
            }
           
        }  
    if(ldlist.size()>0)
        insert ldlist;


I created a test class that when run is not even updating the Code Coverage.... It appears like the test class is not even testing. HELP..what I am doing wrong!

 

@isTest
private class testlead {

static testMethod void  insertNewLead() {
      
Lead ld = new Lead();
       ld.FirstName = 'David';
       ld.LastName  = 'Liu';
       ld.phone = '5555551236';
       ld.Email  = 'simon.williams@google.com';
       ld.Company = 'Dalfie';
       ld.State = 'IN';
       ld.CLASS_Lead_Source__c ='R2D2';
        if( cse.Time_Zone__c == 'EST')                   
                    ld.LiveOpsApp__Lead_Timezone__c = '(GMT-05:00) Eastern Standard Time (America/New_York)';
                if( cse.Time_Zone__c == 'CST')                   
                    ld.LiveOpsApp__Lead_Timezone__c = '(GMT-06:00) Central Standard Time (America/Chicago)';               
                if( cse.Time_Zone__c == 'MST')                   
                     ld.LiveOpsApp__Lead_Timezone__c = '(GMT-07:00) Mountain Standard Time (America/Denver)';
                if( cse.Time_Zone__c == 'PST')                   
                     ld.LiveOpsApp__Lead_Timezone__c = '(GMT-08:00) Pacific Standard Time (America/Los_Angeles)';ld.LiveOpsApp__Lead_Timezone__c = 'TEST';
   
    insert ld;
    }
}
Best Answer chosen by vikas malik
Gaurav NirwalGaurav Nirwal

Make sure you do these things in order to get coverage

1. First of all this trigger is on Case not on lead. So create a case record.
2. Case must full fill this condition 
if((cse.Assigned_To__c == null || cse.Assigned_To__c  == '') && cse.CLASS_Application_Status__c == '0-Pending Incomplete')

All Answers

PratikPratik (Salesforce Developers) 
Hi Vikas,

As per your trigger code, you are creating the Lead on Case creation/updation with the mentioned criteria.
So in your test class, you should insert/update the Case record as per trigger criteria only. In your test class as above, you are trying to insert the Lead record which is not correct so it's not giving you any coverage.

Hope this will help you.

Thanks,
Pratik
Gaurav NirwalGaurav Nirwal

Make sure you do these things in order to get coverage

1. First of all this trigger is on Case not on lead. So create a case record.
2. Case must full fill this condition 
if((cse.Assigned_To__c == null || cse.Assigned_To__c  == '') && cse.CLASS_Application_Status__c == '0-Pending Incomplete')
This was selected as the best answer