+ Start a Discussion
Girbson Bijou 7Girbson Bijou 7 

Test for Trigger which delete child record for a specific criteria of the parent

New in testing, I need  the test class for this below trigger

trigger ItemToDelete on Delivery__c(after update) {
 Set<Id> setDeliveryIds = new Set<Id>();
 for (Delivery__c delivery : trigger.new) {
  if (delivery.Delivery_status__c == 'Voided') {
  List<Item_Distributed__c> itemsToDelete = [SELECT id FROM Item_Distributed__c WHERE Delivery__c IN :setDeliveryIds];
   DELETE itemsToDelete;
Best Answer chosen by Girbson Bijou 7
Hi Girbson, check below test class :
public class testClassSample {
    static testMethod void testMethodOne() {
	   Delivery__c oDelivery = new Delivery__c();
	      oDelivery.Name = 'test del'; // remove this line if name is auto number field 
		   // add all requred fields here for Delivery__c object 
		  insert oDelivery;
		  Item_Distributed__c itemDis = new Item_Distributed__c();
		     itemDis.name = 'test'; // remove this line if name is auto number field 
			 itemDis.Delivery__c = oDelivery.Id;
			 // add all requred fields here for Item_Distributed__c object
	        insert itemDis;
		  oDelivery.Delivery_status__c = 'Voided';
		  update oDelivery;

Thank, let us know if it helps you