You need to sign in to do that
Don't have an account?
ashu 6112
Test Class trigger
Hi All, I had a requirement in which there were two objects ABC and DEF with master detail relation, ABC as master. Both objects hav a checkbox field.Abc has Has_condition__c checkbox whereas Def has Active__c checkbox. NOw I had to write the trigger if anyone of the child record has checkbox=true, then master record must have checkbox true, otherwise false if there is no child record with checkbox=true. I wrote the trigger for this and that is working fine. NOw I have to wite test class for this using fatory data class. Please help me out in this..
trigger hasActiveCheckboxCondition on Def__c (After insert, After update)
{
if(Trigger.isAfter && (Trigger.isInsert || Trigger.isUpdate))
{
hasActiveCheckboxDef_Abc t = new hasActiveCheckboxDef_Abc();
t.insertCondition(trigger.new);
}
}
********************************
Apex class:
public class hasActiveCheckboxDef_Abc
{
public void insertdef(List<def__c> tab1)
{
Set<Id> stemp = new Set<Id >();
for(def__c c : tab1)
{
stemp.add(c.abc__c);
}
//Map<id,List<def__c>> mpDb=new Map<id,List<def__c>>();
List<abc__c> tabToBeUpdated = new List<abc__c>();
Map<id, abc__c> mapId = new Map<id,abc__c>();
for(abc__c tab : [select id,Has_Condition__c,(Select id,name,abc__c,Active__c from abcs__r) from abc__c where id IN :stemp])
{
Boolean flag = false;
for(def__c cond : tab.defs__r)
{
if(cond.Active__c) {
flag = true;
tab.Has_Condition__c = true;
break;
}
}
if(flag == false)
{
tab.Has_Condition__c = false;
}
mapId.put(tab.id,tab);
}
if(mapId != null)
update mapId.values();
}
}
trigger hasActiveCheckboxCondition on Def__c (After insert, After update)
{
if(Trigger.isAfter && (Trigger.isInsert || Trigger.isUpdate))
{
hasActiveCheckboxDef_Abc t = new hasActiveCheckboxDef_Abc();
t.insertCondition(trigger.new);
}
}
********************************
Apex class:
public class hasActiveCheckboxDef_Abc
{
public void insertdef(List<def__c> tab1)
{
Set<Id> stemp = new Set<Id >();
for(def__c c : tab1)
{
stemp.add(c.abc__c);
}
//Map<id,List<def__c>> mpDb=new Map<id,List<def__c>>();
List<abc__c> tabToBeUpdated = new List<abc__c>();
Map<id, abc__c> mapId = new Map<id,abc__c>();
for(abc__c tab : [select id,Has_Condition__c,(Select id,name,abc__c,Active__c from abcs__r) from abc__c where id IN :stemp])
{
Boolean flag = false;
for(def__c cond : tab.defs__r)
{
if(cond.Active__c) {
flag = true;
tab.Has_Condition__c = true;
break;
}
}
if(flag == false)
{
tab.Has_Condition__c = false;
}
mapId.put(tab.id,tab);
}
if(mapId != null)
update mapId.values();
}
}
All Answers
Sample Test Factory Class Pleass try below test class
Many Thnaks for the sol.
Could you please expalin the use of test.startTest() n test.stopTest() mehods.
Thank,
To make the utility class as reusable as possible (for other tests) you will have to determine what needs to be passed in when creating the records. Once you know that you can just reuse the majority of the code where I create the abc__c and def__c records (lines 5 - 12).