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
DrebinDrebin 

Make a test class for a before delete trigger

Hi everyone,

I have this trigger to delete a document when a lead is deleted :
trigger DeleteImageLead on Lead (before delete) {

    if (Trigger.isDelete) {
        for (Lead l : Trigger.old)
        {
            List<ContentDocument> myFeed = [SELECT LatestPublishedVersionId FROM ContentDocument WHERE Id In (SELECT ContentDocumentId FROM ContentVersion WHERE Id =:l.PhotoLead_Image__c)];
            delete myFeed;
        }
    }
}
When I deploy it on a production environment, I have a code coverage error because it is 0%.
Can you please help me with to make an apex test class for this code ? I tried to develop it but I'm still with 0% code coverage

Thanks a lot !

Frédéric
 
Best Answer chosen by Drebin
Amit Chaudhary 8Amit Chaudhary 8
Please create below test class
@isTest 
public class DeleteImageLeadTest 
{
    static testMethod void testMethod1() 
	{
			Lead newLead = new Lead() ;
			newLead.FirstName = 'Cole';
			newLead.LastName = 'Swain';
			newLead.Company = 'BlueWave';
			newLead.Status = 'contacted';
			insert newLead;

			try
			{
				Delete 	newLead;
			}
			catch(Exception ee)
			{}
    }
}
Then deploy above test class in production
 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Please create below test class
@isTest 
public class DeleteImageLeadTest 
{
    static testMethod void testMethod1() 
	{
			Lead newLead = new Lead() ;
			newLead.FirstName = 'Cole';
			newLead.LastName = 'Swain';
			newLead.Company = 'BlueWave';
			newLead.Status = 'contacted';
			insert newLead;

			try
			{
				Delete 	newLead;
			}
			catch(Exception ee)
			{}
    }
}
Then deploy above test class in production
 
This was selected as the best answer
DrebinDrebin
Hi Amit,

It works great, thanks !
DrebinDrebin
I have another test class to make for a StandardSetController :
public class ListLead {

    public ApexPages.StandardSetController setCon {
        get {
            if(setCon == null) {
                setCon = new ApexPages.StandardSetController(Database.getQueryLocator(
                    [SELECT LastName, Status FROM Lead Where OwnerId =:UserInfo.getUserId() AND LastName Like 'On the road%' ORDER BY CreatedDate DESC LIMIT 5]));
            }
            return setCon;
        }
        set;
    }

    public List<Lead> getLeads() {
        return (List<Lead>) setCon.getRecords();
    }
    
    public PageReference goBack()
    {
        return new PageReference('/apex/PhotoRoad');
    }
}
I tried the code on this website http://amitsalesforce.blogspot.fr/2015/06/best-practice-for-test-classes-sample.html, but it doesn't work.
Can you help me with that.

Thanks !
 
Amit Chaudhary 8Amit Chaudhary 8
try below code
@isTest 
public class DeleteImageLeadTest 
{
    static testMethod void testMethod1() 
	{
	
		Lead newLead = new Lead() ;
		newLead.FirstName = 'Cole';
		newLead.LastName = 'Swain';
		newLead.Company = 'BlueWave';
		newLead.Status = 'contacted';
		insert newLead;

		ListLead  obj = new ListLead ();
		obj.goBack();
		List<Lead> lstLed = obj.getLeads();
			
    }
}

 
DrebinDrebin
Ok, just need to call all methods in my class.

Thanks a lot, it's 100% code coverage now !
AchtungAchtung
I have a Trigger that works almost similar with it and trying to create a test class for before delete. 
 
Trigger SAJobsDeletion on Support_Contract__c (before delete) {
    try{     
        
        for(Support_Contract__c sa : Trigger.old) 
        {           
            List <Job__c> j = [SELECT Id, Service_Agreement__c FROM Job__c WHERE Service_Agreement__c =: sa.Id];
            if(j.size() > 0)
            {              
                sa.adderror('Service Agreement is currently associated with a Job and cannot be deleted.'); 
            }
        }  
    } Catch(Exception ex){  }
}

And here's my test class that returns 0% coverage:
 
@isTest
public class SAJobsDeletion_TestClass {
    
    static testMethod void CheckServiceAgreement(){   
        Test.startTest();
        
        Support_Contract__c a = new Support_Contract__c();
        a.Client__c = '0035D0000067lJrQAI';
        a.Funding_Type__c = 'Cash';
        a.Funding_Management__c = 'Participant';
        a.Start_Date__c = Date.newInstance(2018, 03, 01);
        a.End_Date__c = Date.newInstance(2019, 03, 01);
        insert a;  
        
        Job__c j = new Job__c();
        j.Service_Agreement__c = a.Id;
        j.Contact__c = '0035D0000067lJrQAI'; 
        j.Region__c = 'Australia';
        j.Duration__c = 60;
        insert j;   
        
        try{ 
            delete a;            
        }Catch (DMLException e){
            System.assert(e.getMessage().contains('Service Agreement cannot be deleted.'), 'Service Agreement cannot be deleted.');
        }
        Test.stopTest();        
    }
}

 
Jatin Goyal 14Jatin Goyal 14
I am facing similar issue there are multiple test class every other test class is passing but testDelete method is giving 0% code coverage