You need to sign in to do that
Don't have an account?
pls help me test case of trigger
Hi all,
can help me test case of my trigger .
This is trigger code,
global class Monthly{
public static void ValAdd()//Set<Id> InquiryId
{
List<Inquiry__c> Inquiry= [SELECT KPIInquiryContent__c,KPIInquiryContent__r.Account__c,CommodityName__c,ModifyDate__c,Status__c from Inquiry__c ];
{
Inquiry__c Fr= Inquiry[0];
Date startDate=fr.ModifyDate__c.toStartOfMonth();
Date endDate=fr.ModifyDate__c.toStartOfMonth().addMonths(1);
LIST<AggregateResult> countmonth=[SELECT count(KPIInquiryContent__c) coun from Inquiry__c where KPIInquiryContent__r.Account__c=:fr.KPIInquiryContent__r.Account__c and ModifyDate__c >= :startDate AND ModifyDate__c < :endDate and PayoffObject__c=:'○' and Status__c IN('対応中' ,'対応完了')];
//LIST<AggregateResult> calmonth=[Select CALENDAR_MONTH(WorkDate__c), count(ImportCount__c) icount from WorkResultsMonth__c where Account_Name__c=:acname GROUP BY CALENDAR_MONTH(WorkDate__c)];
LIST<AggregateResult> calmonth=[Select count(ImportCount__c) icount from WorkResultsMonth__c where WorkDate__c >= :startDate and WorkDate__c < :endDate and Account_Name__c=:fr.KPIInquiryContent__r.Account__c];
List<KPIAccident__c> acc= [SELECT Date__c,MonthlyGeneratedCount__c,MonthlyPPM__c from KPIAccident__c where Account_Name__c=:fr.KPIInquiryContent__r.Account__c and Date__c >= :startDate and Date__c < :endDate]; //
if(!acc.isEmpty() )
{
KPIAccident__c accupdate = acc[0];
Integer totalcountmonth=Integer.Valueof(countmonth[0].get('coun'));
accupdate.MonthlyGeneratedCount__c=totalcountmonth;
Integer totalmonthwork= Integer.Valueof(calmonth[0].get('icount'));
accupdate.MonthlyPPM__c=((totalmonthwork/totalcountmonth)*1000000);
update accupdate;
}else
{
KPIAccident__c kpi = new KPIAccident__c();
List<KPIAccident__c> ToUpdate = new List<KPIAccident__c>();
kpi.Date__c=fr.ModifyDate__c;
kpi.Account__c=fr.KPIInquiryContent__r.Account__c;
kpi.itemname__c= fr.CommodityName__c;
Integer totalcountmonths=Integer.Valueof(countmonth[0].get('coun'));
kpi.MonthlyGeneratedCount__c=totalcountmonths;
Integer totalmonthworks= Integer.Valueof(calmonth[0].get('icount'));
kpi.MonthlyPPM__c=((totalmonthworks/totalcountmonths)*1000000);
ToUpdate.add(kpi);
insert ToUpdate;
}
}
}}
my test case code is:
static testMethod void testAMethod1()
{
Test.startTest();
Account a2 = new Account();
a2.name='pradeep';
insert a2;
Inquiry__c f1=new Inquiry__c(CommodityName__c='pradeep',ModifyDate__c=date.parse('03/09/2012'),Status__c ='good');
insert f1;
List<KPIAccident__c> ac2=[SELECT Date__c,MonthlyGeneratedCount__c,MonthlyPPM__c from KPIAccident__c];
if(ac2.isEmpty())
{
KPIAccident__c m5=ac2[0];
m5.MonthlyGeneratedCount__c= 129;
m5.MonthlyPPM__c=2563;
//m5.Date__c=f1.ModifyDate__c;
insert m5;
}
else
{
List<KPIAccident__c> listEt = new List<KPIAccident__c>();
KPIAccident__c d1=new KPIAccident__c ();
d1.MonthlyGeneratedCount__c=145;
d1.MonthlyPPM__c=2569;
listEt.add(d1);
try
{
insert listEt;
}
catch(Exception e){}
Test.stopTest();
}
}
82 }
my trigger is:
trigger AddValue_to_Accident on Inquiry__c (after insert,after update)
{
Monthly.ValAdd();
}
The above red mark not coverage of my test case of trigger. The tese case was written but it should be coverage 61%.The red mark data not covered. pls help me my test case of trigger..
If my test case in build of trigger. it should be covered in 65% but an error is
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, FBR_AddValue_to_Accident: execution of AfterInsert caused by: System.ListException: List index out of bounds: 0 Class.FBR_InquiryMonthly.ValAdd: line 38, column 1 Trigger.FBR_AddValue_to_Accident: line 5, column 1: [].
can any help me test case of trigger and solution of error.
Cheers
Pradeep Kumar.p
Hi
create a record of KPIAccident__c object after
Inquiry__c f1=new Inquiry__c(CommodityName__c='pradeep',ModifyDate__c=date.parse('03/09/2012'),Status__c ='good');
insert f1;
KPIAccident__c kpiTest = new KPIAccident__c(field.,Date__c = somedate value.............);
insert kpiTest ;
Now try to update....like
kpiTest.Date__c = f1.ModifyDate__c ;
kpiTest.itemname__c= f1.CommodityName__c
Like this please update or two values... by looking this....kpi.Account__c=fr.KPIInquiryContent__r.Account__c;
kpi.itemname__c= fr.CommodityName__c;
Now write
Update kpiTest ;
Did this post answers your problem If so please mark it solved .
Thanks
asish
Hi asish.
Thanks for ur solution solved.
But it should be covered in 53% inbuilt of trigger.
But Individual the code covered in 61% .But not covered else condition of trigger,.pls help me else condition of trigger class
cheers
Pradeep Kumar.P
HI
please share your testmethod what you have written and also share the code which are not under coverage.. please make
this code to red color so that I can easily identify
Thanks
asish
Hi asish,
This is my trigger class and test case
//Trigger class
global class FBR_InquiryMonthly{
public static void ValAdd()
{
List<Inquiry__c> Inquiry= [SELECT KPIInquiryContent__c,CommodityName__c,ModifyDate__c,Status__c from Inquiry__c ];
{
Inquiry__c m1=Inquiry[0];
Date startDate=m1.ModifyDate__c.toStartOfMonth();
Date endDate=m1.ModifyDate__c.toStartOfMonth().addMonths(1);
LIST<AggregateResult> countmonth=[SELECT count(KPIInquiryContent__c) coun from Inquiry__c];// where KPIInquiryContent__r.Account__c=:FBR.KPIInquiryContent__r.Account__c and ModifyDate__c >= :startDate AND ModifyDate__c < :endDate and PayoffObject__c=:'○' and Status__c IN('対応中' ,'対応完了')];
//LIST<AggregateResult> calmonth=[Select CALENDAR_MONTH(WorkDate__c), count(ImportCount__c) icount from WorkResultsMonth__c where Account_Name__c=:acname GROUP BY CALENDAR_MONTH(WorkDate__c)];
LIST<AggregateResult> calmonth=[Select count(ImportCount__c) icount from WorkResultsMonth__c where WorkDate__c >= :startDate and WorkDate__c < :endDate];// and Account_Name__c=:FBR.KPIInquiryContent__r.Account__c];
List<KPIAccident__c> acc= [SELECT Date__c,MonthlyGeneratedCount__c,MonthlyPPM__c from KPIAccident__c ];//where Account_Name__c=:FBR.KPIInquiryContent__r.Account__c and Date__c >= :startDate and Date__c < :endDate]; //
if(!acc.isEmpty() )
{
KPIAccident__c accupdate = acc[0];
Integer totalcountmonth=Integer.Valueof(countmonth[0].get('coun'));
accupdate.MonthlyGeneratedCount__c=totalcountmonth;
Integer totalmonthwork= Integer.Valueof(calmonth[0].get('icount'));
accupdate.MonthlyPPM__c=((totalmonthwork/totalcountmonth)*1000000);
update accupdate;
}else
{
KPIAccident__c kpi = new KPIAccident__c();
List<KPIAccident__c> ToUpdate = new List<KPIAccident__c>();
kpi.Date__c=FBR.ModifyDate__c;
// kpi.Account__c=FBR.KPIInquiryContent__r.Account__c;
kpi.itemname__c= FBR.CommodityName__c;
Integer totalcountmonths=Integer.Valueof(countmonth[0].get('coun'));
kpi.MonthlyGeneratedCount__c=totalcountmonths;
Integer totalmonthworks= Integer.Valueof(calmonth[0].get('icount'));
kpi.MonthlyPPM__c=((totalmonthworks/totalcountmonths)*1000000);
ToUpdate.add(kpi);
insert ToUpdate;
}
}
}
//Test case
static testMethod void testAMethod1()
{
Test.startTest();
try
{
Account a2 = new Account();
a2.name='rakuten12';
insert a2;
double dTotal=124;
double Total=1563;
KPIAccident__c kpi=new KPIAccident__c();
kpi.MonthlyGeneratedCount__c=dTotal;
kpi.MonthlyPPM__c=Total;
insert kpi;
Inquiry__c f1=new Inquiry__c(CommodityName__c='pradeep',ModifyDate__c=date.parse('03/09/2012'),Status__c ='good');
insert f1;
KPIAccident__c kpiTest = new KPIAccident__c(Name='rakuten',Date__c=date.parse('03/09/2012'));
insert kpiTest ;
kpiTest.Date__c = f1.ModifyDate__c ;
kpiTest.itemname__c= f1.CommodityName__c;
kpiTest.MonthlyGeneratedCount__c=156;
kpiTest.MonthlyPPM__c=5632;
Update kpiTest ;
}
catch(Exception e){}
Test.stopTest();
}
}
I shared test method and code of trigger class.The red mark is not covered .Pls help me test case of my trigger class.It should be covered in 53%.pls covered the red mark of code.
cheers
Pradeep p
Hi
please tell more about FBR.. delete all the commented code and share it..coz commented code confusing..
TO cover else block you need to make acc size is zero .
acc is a list of KPIAccident__c record . so to make blank dont creat record in this KPIAccident__c for first update....
then create record for 2nd update...
Thanks
asish
Hi asish,
This is my code,
global class FBR_InquiryMonthly{
public static void ValAdd()
{
List<Inquiry__c> Inquiry= [SELECT KPIInquiryContent__c,CommodityName__c,ModifyDate__c,Status__c from Inquiry__c ];
{
Inquiry__c m1=Inquiry[0];
Date startDate=m1.ModifyDate__c.toStartOfMonth();
Date endDate=m1.ModifyDate__c.toStartOfMonth().addMonths(1);
LIST<AggregateResult> countmonth=[SELECT count(KPIInquiryContent__c) coun from Inquiry__c];
//LIST<AggregateResult> calmonth=[Select CALENDAR_MONTH(WorkDate__c), count(ImportCount__c) icount from WorkResultsMonth__c where Account_Name__c=:acname GROUP BY CALENDAR_MONTH(WorkDate__c)];
LIST<AggregateResult> calmonth=[Select count(ImportCount__c) icount from WorkResultsMonth__c where WorkDate__c >= :startDate and WorkDate__c < :endDate];
List<KPIAccident__c> acc= [SELECT Date__c,MonthlyGeneratedCount__c,MonthlyPPM__c from KPIAccident__c ];
if(!acc.isEmpty() )
{
KPIAccident__c accupdate = acc[0];
Integer totalcountmonth=Integer.Valueof(countmonth[0].get('coun'));
accupdate.MonthlyGeneratedCount__c=totalcountmonth;
Integer totalmonthwork= Integer.Valueof(calmonth[0].get('icount'));
accupdate.MonthlyPPM__c=((totalmonthwork/totalcountmonth)*1000000);
update accupdate;
}else
{
KPIAccident__c kpi = new KPIAccident__c();
List<KPIAccident__c> ToUpdate = new List<KPIAccident__c>();
kpi.Date__c=m1.ModifyDate__c;
kpi.itemname__c= m1.CommodityName__c;
Integer totalcountmonths=Integer.Valueof(countmonth[0].get('coun'));
kpi.MonthlyGeneratedCount__c=totalcountmonths;
Integer totalmonthworks= Integer.Valueof(calmonth[0].get('icount'));
kpi.MonthlyPPM__c=((totalmonthworks/totalcountmonths)*1000000);
ToUpdate.add(kpi);
insert ToUpdate;
}
}
}
static testMethod void testAMethod1()
{
Test.startTest();
try
{
Account a2 = new Account();
a2.name='rakuten12';
insert a2;
double dTotal=124;
double Total=1563;
KPIAccident__c kpi=new KPIAccident__c();
kpi.MonthlyGeneratedCount__c=dTotal;
kpi.MonthlyPPM__c=Total;
insert kpi;
Inquiry__c f1=new Inquiry__c(CommodityName__c='pradeep',ModifyDate__c=date.parse('03/09/2012'),Status__c ='good');
insert f1;
KPIAccident__c kpiTest = new KPIAccident__c(Name='rakuten',Date__c=date.parse('03/09/2012'));
insert kpiTest ;
kpiTest.Date__c = f1.ModifyDate__c ;
kpiTest.itemname__c= f1.CommodityName__c;
kpiTest.MonthlyGeneratedCount__c=156;
kpiTest.MonthlyPPM__c=5632;
Update kpiTest ;
}
catch(Exception e){}
Test.stopTest();
}
}
Pls help me the above test case and trigger write test case of trigger solve the red marks
cheers
Pradeep kumar.p
Hi asish,
This is my trigger and test class built in trigger class
global class InquiryMonthly{
public static void ValAdd()
{
List<Inquiry__c> Inquiry= [SELECT KPIInquiryContent__c,CommodityName__c,ModifyDate__c,Status__c from Inquiry__c ];
{
nquiry__c fr= Inquiry[0];
Date startDate=fr.ModifyDate__c.toStartOfMonth();
Date endDate=fr.ModifyDate__c.toStartOfMonth().addMonths(1);
LIST<AggregateResult> countmonth=[SELECT count(KPIInquiryContent__c) coun from Inquiry__c];// where KPIInquiryContent__r.Account__c=:FBR.KPIInquiryContent__r.Account__c and ModifyDate__c >= :startDate AND ModifyDate__c < :endDate and PayoffObject__c=:'○' and Status__c IN('対応中' ,'対応完了')];
//LIST<AggregateResult> calmonth=[Select CALENDAR_MONTH(WorkDate__c), count(ImportCount__c) icount from WorkResultsMonth__c where Account_Name__c=:acname GROUP BY CALENDAR_MONTH(WorkDate__c)];
LIST<AggregateResult> calmonth=[Select count(ImportCount__c) icount from WorkResultsMonth__c where WorkDate__c >= :startDate and WorkDate__c < :endDate];// and Account_Name__c=:FBR.KPIInquiryContent__r.Account__c];
List<KPIAccident__c> acc=[SELECT Date__c,MonthlyGeneratedCount__c,MonthlyPPM__c from KPIAccident__c];//where Account_Name__c=:FBR.KPIInquiryContent__r.Account__c and Date__c >= :startDate and Date__c < :endDate]; //
if(!acc.isEmpty() )
{
KPIAccident__c accupdate =acc[0];
Integer totalcountmonth=Integer.Valueof(countmonth[0].get('coun'));
accupdate.MonthlyGeneratedCount__c=totalcountmonth;
Integer totalmonthwork= Integer.Valueof(calmonth[0].get('icount'));
accupdate.MonthlyPPM__c=((totalmonthwork/totalcountmonth)*1000000);
update accupdate;
}else
{
KPIAccident__c kpi = new KPIAccident__c();
List<KPIAccident__c> ToUpdate = new List<KPIAccident__c>();
kpi.Date__c=FBR.ModifyDate__c;
// kpi.Account__cfr.KPIInquiryContent__r.Account__c;
kpi.itemname__c=frCommodityName__c;
Integer totalcountmonths=Integer.Valueof(countmonth[0].get('coun'));
kpi.MonthlyGeneratedCount__c=totalcountmonths;
Integer totalmonthworks= Integer.Valueof(calmonth[0].get('icount'));
kpi.MonthlyPPM__c=((totalmonthworks/totalcountmonths)*1000000);
ToUpdate.add(kpi);
insert ToUpdate;
}
}
}
static testMethod void testAMethod1()
{
Test.startTest();
try
{
Account a2 = new Account();
a2.name='rakuten12';
insert a2;
double dTotal=124;
double Total=1563;
Inquiry__c f1=new Inquiry__c(CommodityName__c='pradeep',ModifyDate__c=date.parse('03/09/2012'),Status__c ='good');
insert f1;
//List<KPIAccident__c> acc1=[SELECT Date__c,MonthlyGeneratedCount__c,MonthlyPPM__c from KPIAccident__c];
//KPIAccident__c kp=acc1[0];
// kp.Date__c=f1.ModifyDate__c;
// kp.itemname__c=f1.CommodityName__c;
//kp.MonthlyGeneratedCount__c=145;
//kp.MonthlyPPM__c=1456;
//update kp;
KPIAccident__c kc1=new KPIAccident__c();
List<KPIAccident__c> ToUpdate1=new List<KPIAccident__c>();
kc1.Date__c=f1.ModifyDate__c;
kc1.MonthlyGeneratedCount__c=126;
kc1.MonthlyPPM__c =12563;
ToUpdate1.add(kc1);
insert ToUpdate1;
}
catch(Exception e){}
Test.stopTest();
}
}
The above code the red mark is not working and covered the test case. Present 65% covered in my test case.Pls rectify and try the my test case covered 75% above in my trigger class
cheers
Pradeep P