You need to sign in to do that
Don't have an account?
Marcio 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?
Add the test class code
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.
The test class.
Missing additional update at the end.
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"
I think you get the error in the insert inside the class.
Try to wrap it with try-catch and debug it.
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.