You need to sign in to do that
Don't have an account?
Test for Undelete
hello,
i want to write a test class for a trigger but my test class cant cover Trigger on undelete.
Trigger
trigger triggerOnMyCount on Student__c ( after insert , after delete ,after undelete) {
Map<Id,Class__c> claMap= new Map<id,Class__C>([Select c.NumberOfStudents__c,c.MyCount__c From Class__c c ]);
if(Trigger.isInsert){
for( student__c std : trigger.new ){
class__c cls=claMap.get(std.class__c);
cls.MyCount__c=cls.NumberOfStudents__c+1;
update cls;
}
}
if(trigger.isdelete){
for(student__c std : trigger.old){
class__c cls=claMap.get(std.class__c);
cls.MyCount__c=cls.NumberOfStudents__c-1;
update cls;
}
}
if(Trigger.isUnDelete){
for( student__c std : trigger.new ){
class__c cls=claMap.get(std.class__c);
cls.MyCount__c=cls.MyCount__c+1;
update cls;
}
}
}
TEST CLASS
@isTest
private class test_triggerOnMyCount{
static testmethod void triggerTest(){
class__C cls=new class__C();
cls.Name__c = 'Apex' ;
insert cls;
System.assertEquals(null,cls.MyCount__c);
Student__C std = new Student__C();
std.Last_Name__c='Tejpal';
std.class__C=cls.id;
insert std;
cls=[select MyCount__c from class__C where id=: cls.id];
System.assertEquals(1,cls.MyCount__c);
delete std;
cls=[select MyCount__c from class__C where id=: cls.id];
System.assertEquals(0,cls.MyCount__c);
}
}
please help
i solved my problem.
its a silly problem.
TEST class
@isTest
private class test_triggerOnMyCount{
static testmethod void triggerTest(){
class__C cls=new class__C();
cls.Name__c = 'Apex' ;
cls.MaxSize__c=1;
insert cls;
System.assertEquals(null,cls.MyCount__c);
Student__C std = new Student__C();
std.Last_Name__c='Tejpal';
std.class__C=cls.id;
insert std;
cls=[select MyCount__c from class__C where id=: cls.id];
System.assertEquals(1,cls.MyCount__c);
delete std;
cls=[select MyCount__c from class__C where id=: cls.id];
System.assertEquals(0,cls.MyCount__c);
undelete std;
cls=[select MyCount__c from class__C where id=: cls.id];
System.assertEquals(1,cls.MyCount__c);
}
}
All Answers
i solved my problem.
its a silly problem.
TEST class
@isTest
private class test_triggerOnMyCount{
static testmethod void triggerTest(){
class__C cls=new class__C();
cls.Name__c = 'Apex' ;
cls.MaxSize__c=1;
insert cls;
System.assertEquals(null,cls.MyCount__c);
Student__C std = new Student__C();
std.Last_Name__c='Tejpal';
std.class__C=cls.id;
insert std;
cls=[select MyCount__c from class__C where id=: cls.id];
System.assertEquals(1,cls.MyCount__c);
delete std;
cls=[select MyCount__c from class__C where id=: cls.id];
System.assertEquals(0,cls.MyCount__c);
undelete std;
cls=[select MyCount__c from class__C where id=: cls.id];
System.assertEquals(1,cls.MyCount__c);
}
}
Hi,
Tryout this sample test code to get your requirement, it may help you.
Trigger
trigger ObjectOneUpdate on ObjectTwo__c (after insert, after update, after delete, after undelete) {
double sub;
Map<Id,ObjectOne__c> objMap= new Map<id,ObjectOne__c>([Select c.id, c.NumberOne__c From ObjectOne__c c ]);
if(Trigger.isUpdate || Trigger.isInsert || Trigger.isundelete){
for( ObjectTwo__c objc : trigger.new ){
ObjectOne__c objp=objMap.get(objc.ObjectOne_Look__c);
objp.NumberOne__c=objp.NumberOne__c+objc.NumberTwo__c;
update objp;
}
}
else if(trigger.isdelete){
for(ObjectTwo__c objc : trigger.old){
ObjectOne__c objp=objMap.get(objc.ObjectOne_Look__c);
objp.NumberOne__c=objp.NumberOne__c - objc.NumberTwo__c;
update objp;
}
}
}
Test Class
@isTest
public class ObjectOneUpdate_Test {
static testmethod void triggerTest(){
ObjectOne__c objp=new ObjectOne__c();
objp.Name = 'Apex' ;
insert objp;
System.assertEquals(null,objp.NumberOne__c);
ObjectTwo__C objc = new ObjectTwo__C();
objc.NumberTwo__c=2;
objc.ObjectOne_Look__c=objp.id;
insert objc;
System.assertEquals(2,objp.NumberOne__c);
objp=[select NumberOne__c from ObjectOne__C where id=: objp.id];
delete objc;
System.assertEquals(null,objp.NumberOne__c);
objp=[select NumberOne__c from ObjectOne__C where id=: objp.id];
undelete objc;
System.assertEquals(2,objp.NumberOne__c);
objp=[select NumberOne__c from ObjectOne__C where id=: objp.id];
System.assertEquals(2,objp.NumberOne__c);
}
}
The Scenario is: There are 2 objects (ObjectOne__c, ObjectTwo__c). They both have lookup relationship and ObjectOne is a parent in the relationship.
There is a Number field (NumberTwo__c) in ObjectTwo.
I need the sum of the NumberTwo field value of all child records (of NumberTwo object) in the field (NumberOne__c) of ObjectOne (which is the parent object). And also when delete, the value of NumberOne in parent should be decremented.