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
Tasia Demuth 4Tasia Demuth 4 

Help with Test Class and Delete Trigger

Hi!

I have written a trigger that updates a case when an associated lead is deleted. 

The struggle I am having is I can't seem to figure out how to test the delete part. Below is my trigger and my class. I have 52% coverage. I have bolded the lines that are not covered. The community helped me earlier on a test class, and I thought I had a better handle on it. But, this is stumping me! 

Any help is appreciated. 

Best, 
Tasia

trigger UpdateCaseWhenLeadDeleted on Lead (after delete) {
if(Trigger.isAfter && Trigger.isDelete)
    {
       List<Case> listCase = new List<Case>();
        Set<Id> DeletedLeads = new Set<Id>();
        Map<String,Boolean> mapLeadDelete = new Map<String, Boolean>();
        for(Lead objLead : Trigger.old)
        {
            if(objLead.IsDeleted)
            {
            DeletedLeads.add(objLead.Id); 
           mapLeadDelete.put(objLead.Id, objLead.IsDeleted);  
  
        }
        }    
        for(Case objCase : [SELECT Lead__c FROM Case WHERE Lead__c IN : DeletedLeads])    
        {
            if(mapLeadDelete.containsKey(objCase.Lead__c))
        {       
            objCase.Lead__c = null;
            objCase.Status = 'Denied';
            objCase.Type = 'Web Account Denied';
            listCase.add(objCase);

        }
        }
        update listCase;
    }
}

@isTest
public class UpdateCaseWhenLeadDeletedTest {

    private static testMethod void LeadDeletedTest(){
        //create a Lead
        Lead lead=new Lead(LastName='Test',FirstName='Tester',Status='Known', Company = 'CD Test');
        insert lead;
        // create a Case
        Case cscase=new Case(Status = 'New', Priority = 'Medium', Origin = 'Email');
        cscase.Lead__c = lead.Id;
        insert cscase;
       
        //delete Lead
        delete lead;
        
        List<Case> lstCase = [Select id ,Status, Type from Case where Lead__c =:lead.id ];
        System.assert(lstCase.size() > 0 );
        System.assertEquals('Denied', lstCase[0].Status);
        System.assertEquals('Web Account Denied', lstCase[0].Type);
    }
}
 
Best Answer chosen by Tasia Demuth 4
Alain CabonAlain Cabon
You should not use:  if(objLead.IsDeleted)

First change.
 

All Answers

Alain CabonAlain Cabon
You should not use:  if(objLead.IsDeleted)

First change.
 
This was selected as the best answer
Tasia Demuth 4Tasia Demuth 4
Yes that helped! Thank you Alain. 

Now the lines that are not covered are below. This confuses me because I have asserts to test the different field values. 

  {
            if(mapLeadDelete.containsKey(objCase.Lead__c))
        {       
            objCase.Lead__c = null;
            objCase.Status = 'Denied';
            objCase.Type = 'Web Account Denied';
            listCase.add(objCase);
Alain CabonAlain Cabon
What is the relationship between the case and the lead defined by the relationship lookup field Lead__c  (prevents the deletion of the used lead ?)

So you need to delete the associated cases before the deletion of the lead itself perhaps.
Tasia Demuth 4Tasia Demuth 4
Yes that is why I was trying to set the Lead__c to null. Basically that field relates the case to a lead if a potential lead asks customer service a question through our website. However, if the lead is not legitimate, then we want to delete the lead and close the case. 

 
Tasia Demuth 4Tasia Demuth 4
I was actually able to resolve this by changing the trigger from being an After Delete, to being Before Delete. 

This allowed me to change the Lead__c to null before the lead was deleted and therefore not have an error.