+ Start a Discussion
Marcio Zima.ax1538Marcio Zima.ax1538 

Test for trigger with new function

I'm having problem to code the test for the following trigger code that automatically generate relatedlist according to a value of a picklist. Until now it's with 75% code coverage.

 

trigger AddRelatedList on Obj1__c (after update) {
    List<Obj2__c> listRisks = new List<Obj2__c>();
    List<Obj3__c> listChecks = new List<Obj3__c>();
    
    //Assign the context before and after the change into a Map
    Map<Id, Obj1__c> newStatusMap = Trigger.newMap;
	Map<Id, Obj1__c> oldStatusMap = Trigger.oldMap;

	//Loop through the map
	for(Id itemId:newStatusMap.keySet()){
		Obj1__c myNewItem = newStatusMap.get(itemId);
		Obj1__c myOldItem = oldStatusMap.get(itemId);
		
		if (myNewItem.F_O__c <> myOldItem.F_O__c){
		    for (Obj1__c a : Trigger.new) {
		    	if(a.F_O__c == 'Firm'){        
			        Obj2__c clt = new Obj2__c(Line_Item__c = a.Id);
			        listRisks.add(clt);
			        
			        Obj3__c clt2 = new Obj3__c(Line_Item__c = a.Id);
			        listChecks.add(clt2);
		        }
		    }		   
		 }
	}
    insert listRisks;
    insert listChecks;
}

 It's with these 4 lines not tested:

 

Obj2__c clt = new Obj2__c(Line_Item__c = a.Id);
listRisks.add(clt);
			        
Obj3__c clt2 = new Obj3__c(Line_Item__c = a.Id);
listChecks.add(clt2);

 

Anyone knows how to help?

 

liron169liron169
Hi,
Add the test class code
Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan

Yes,

 

Its so simple. You are checking the condition before these tow lines, as if(a.F_O__c == 'Firm').

This is the reason it is not covered. Because to cover these lines you need to insert the record for the object Obj1__c, by satisfying that condition a.F_O__c == 'Firm'.

 

Do the following,

 

@IsTest(SeeAllData = true)

public class testAddRelatedList{

  public static testMethod void test1(){

.....

       Obj1__c ob1 = new Obj1__c(name ='test', a.F_O__c = 'Firm');
       insert ob1;
       obj2__c o2 = new Obj1__c(name ='test', a.Line_Item__c = obj1__C.id);
        insert o2;
       obj3__c o3 = new Obj1__c(name ='test', a.Line_Item__c = obj1__C.id);
       insert o3;

.....

}

}

 

 

Hope so this helps you...!

Please mark this answer a Solution and please give kudos by clicking on the star icon, if you found this answer as helpful.

 

Marcio Zima.ax1538Marcio Zima.ax1538
@isTest
private class AddRelatedList_Test {

    static testMethod void myUnitTest() {
    	List<Obj2__c> listRisks = new List<Obj2__c>();
    	List<Obj3__c> listChecks = new List<Obj3__c>();

    	Obj4__c a = new Obj4__c();
        insert a;
        Obj1_c item = new Obj1_c(Apttus__AgreementId__c = a.Id);
        insert item;

    	item.F_O__c = 'Option';
    	update item;
		
    	item.F_O__c = 'Firm';   	
    }
}

 The test class.

liron169liron169

Missing additional update at the end.

Marcio Zima.ax1538Marcio Zima.ax1538

When I include the last update it return the following failure "Update failed. First exception on row 0 with id... CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY"

liron169liron169

I think you get the error in the insert inside the class.

 

Try to wrap it with try-catch and debug it.

 

 

try{
insert listRisks; insert listChecks;
}
catch(Exception e){
system.debug('Error when insert: ' + e);
}
Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan

Hi Marcio,

 

I have slightly modified your code, try with this code now.

 

Try the following,

 

@isTest
private class AddRelatedList_Test {
    static testMethod void myUnitTest() {  
        Obj4__c a = new Obj4__c(name = 'Test');
        insert a;
        Obj1__c item = new Obj1__c(name='test2',Apttus__AgreementId__c = a.Id, F_O__c = 'Options');
        insert item;
        item.F_O__c = 'Firm';
        update item;       
        obj2__c o2 = new Obj1__c(name ='test', a.Line_Item__c = item.id); //After updating Obj1__c object create Obj2__c & Obj3__c
        insert o2;
       obj3__c o3 = new Obj1__c(name ='test', a.Line_Item__c = item.id);
       insert o3;       
    }
}

 

 

 

Hope so this helps you...!

Please mark this answer a Solution and please give kudos by clicking on the star icon, if you found this answer as helpful.