You need to sign in to do that
Don't have an account?
Abhishek AS 3
write a testclass for apex trigger
trigger EUCOBBrochureCount on Opportunity_Brochure__c (after insert, after delete, after undelete) {
Set<Id> quoteIDs = new Set<Id>();
if(Trigger.isInsert || Trigger.isUndelete){
for(Opportunity_Brochure__c OB : Trigger.New){
quoteIDs.add(OB.Quote__c);
}
EUCOBBrochureCountHandler.updateQuote(quoteIDs);
}
else if(Trigger.isDelete){
for(Opportunity_Brochure__c opp : Trigger.old){
quoteIDs.add(opp.Quote__c);
}
EUCOBBrochureCountHandler.updateQuote(quoteIDs);
}
}
public with sharing class EUCOBBrochureCountHandler {
public static List<Quote> updateQuote(Set<Id> quoteIds){
Integer TotalCount = [select count() from Opportunity_Brochure_c where Quote_c in :quoteIds];
List<Quote> quoteList = [select id, Brochure_Count__c from Quote where id in :quoteIds];
for(Quote qt : quoteList){
qt.Brochure_Count__c= TotalCount;
}
try{
update quoteList;
}
catch(Exception e){
ApexException.LogException(e);
}
return quoteList;
}
}
Set<Id> quoteIDs = new Set<Id>();
if(Trigger.isInsert || Trigger.isUndelete){
for(Opportunity_Brochure__c OB : Trigger.New){
quoteIDs.add(OB.Quote__c);
}
EUCOBBrochureCountHandler.updateQuote(quoteIDs);
}
else if(Trigger.isDelete){
for(Opportunity_Brochure__c opp : Trigger.old){
quoteIDs.add(opp.Quote__c);
}
EUCOBBrochureCountHandler.updateQuote(quoteIDs);
}
}
public with sharing class EUCOBBrochureCountHandler {
public static List<Quote> updateQuote(Set<Id> quoteIds){
Integer TotalCount = [select count() from Opportunity_Brochure_c where Quote_c in :quoteIds];
List<Quote> quoteList = [select id, Brochure_Count__c from Quote where id in :quoteIds];
for(Quote qt : quoteList){
qt.Brochure_Count__c= TotalCount;
}
try{
update quoteList;
}
catch(Exception e){
ApexException.LogException(e);
}
return quoteList;
}
}
Can you use the below test class. Make sure to replace the API names of the objects and fields where ever necessary.
It is giving 100% coverage for trigger and 87% for Class.
Let me know if you face any issues.
If this solution helps, please mark it as best answer.
Thanks,
All Answers
Can you list down what are the mandatory fields for creation of Quote and Opportunity_Brochure__c records and what is the relation with opportunity?
The simple test class would be creation of Quote and 2 Opportunity_Brochure__c records and deletion of 1 Opportunity_Brochure__c record. This should cover 100%.
Thanks,
K. Sai Praveen Kumar.
Thanks,
Can you use the below test class. Make sure to replace the API names of the objects and fields where ever necessary.
It is giving 100% coverage for trigger and 87% for Class.
Let me know if you face any issues.
If this solution helps, please mark it as best answer.
Thanks,
Can u help to cover the exception in this code. This test class is not covering it.