You need to sign in to do that
Don't have an account?
Ashish Kumar Yadav
How to code Coverage event Approval Custom object apex trigger?
Here is my Code
Apex Trigger
============
trigger EventUpdate on EventApproval__c (after update,before update) {
List<String> ListAuto=new List<String>();
//List<Event>lstEvent=new List<Event>();
map<id,Event> mapevntupdate= new map<id,Event>();
List<Id> EventToBeUpdatedId = new List<Id>();
System.debug('trigger.new---'+trigger.new);
for(EventApproval__c EvAppr:trigger.new){
ListAuto.add(EvAppr.EventAuto__c);
EventToBeUpdatedId.add(EvAppr.Id);
}
System.debug('ListAuto---'+ListAuto);
if(trigger.isAfter && Onetimeinsertupdate.EventUpdatetrigger){
//lstEvent=[Select id,Ownerid,Description,Subject,Tavelled_From__c,Travelled_To__c,Kms_Travelled__c,Public_Transport_Fare__c,EventAuto__c,Reimbursement_Amount__c from Event where (EventAuto__c !=Null and EventAuto__c in :ListAuto) ];
mapevntupdate=new map<id,Event>([Select id,Ownerid,Telephone_call_type__c,EventSubject__c,Description,Subject,Tavelled_From__c,
Travelled_To__c,Kms_Travelled__c,Public_Transport_Fare__c,
EventAuto__c,Reimbursement_Amount__c,
Sales_Manager_Assistance__c
from Event
where (EventAuto__c !=Null and EventAuto__c in :ListAuto) ]);
system.debug('-----------map---'+ mapevntupdate.values());
for(EventApproval__c EvAppr:trigger.new){
for(Event ev: mapevntupdate.values()){
if(EvAppr.EventAuto__c==ev.EventAuto__c){
ev.Tavelled_From__c=EvAppr.Tavelled_From__c;
ev.Travelled_To__c=Evappr.Travelled_To__c;
ev.Kms_Travelled__c=EvAppr.Kms_Travelled__c;
ev.Public_Transport_Fare__c=EvAppr.Public_Transport_Others_Fare__c;
//ev.Reimbursement_Amount__c=Integer.valueof(EvAppr.Reimbursement_Amount__c);
ev.Description=EvAppr.Description__c;
ev.Public_Transport_Mode__c=Evappr.Public_Transport_Mode__c;
ev.Conveyance_Mode__c=EvAppr.Conveyance_Mode__c;
ev.Status__c=EvAppr.Status1__c;
ev.Sales_Manager_Assistance__c=EvAppr.Sales_Manager_Assistance__c;
// new code added by Ashish
ev.Telephone_call_type__c=EvAppr.Telephone_call_type__c;
ev.EventSubject__c=EvAppr.EventSubject__c;
// ev.Reason_for_winning__c=EvAppr.Reason_for_winning__c;
// ev.Reason_for_losing__c=EvAppr.Reason_for_losing__c;
// ev.Reason_for_dropping__c=EvAppr.Reason_for_dropping__c;
// ev.OtherDetails__c=EvAppr.OtherDetails__c;
// ev.OtherDetails1__c=EvAppr.OtherDetails1__c;
// ev.OtherDetails2__c=EvAppr.OtherDetails2__c;//Code End Here
}
}
}
// Logic for updating the Event Status and Comment
List<ProcessInstance>lstprocess = [Select p.TargetObjectId, p.Status, (Select StepStatus,Comments From Steps where StepStatus!='Started') From ProcessInstance p where TargetObjectId in :trigger.newMap.keyset()];
if(lstprocess .size()>0){
for(Event ev:mapevntupdate.values()){
for(ProcessInstance step :lstprocess ){
for( ProcessInstanceStep st:step.Steps){
ev.Comments__c=st.Comments;
system.debug('--------------------Steps Ststus----- '+st.StepStatus);
system.debug('##########+comments are----'+st.Comments);
}
/*if(step.Status!='Started'){
ev.Status__c=step.Status;
}else{
ev.Status__c='Pending';
}*/
ev.Approved_Date__c=system.today();
system.debug('@@@@@@Approval Status---'+step.Status);
}
}
}
system.debug('&&&&&&&&&&&&&&&&&&'+mapevntupdate.size());
if(mapevntupdate.size()>0){
system.debug('&&&&&&&&&&&&&&&&&&');
Onetimeinsertupdate.eventupdateflag=false;
Onetimeinsertupdate.EventUpdatetrigger = false;
//update lstEvent;
update mapevntupdate.values();
system.debug('************'+mapevntupdate.size());
}
//MyFutureClass.myMethod(EventToBeUpdatedId,ListAuto);
Onetimeinsertupdate.EventUpdatetrigger=false;
}
if(Trigger.isBefore){
List<ProcessInstance>lstprocess = [Select p.TargetObjectId, p.Status, (Select StepStatus, Comments From
Steps where StepStatus!='Started') From ProcessInstance p where TargetObjectId in :trigger.newMap.keyset()];
if(lstprocess .size()>0){
for(EventApproval__c Ev:trigger.new){
for(ProcessInstance step :lstprocess ){
for( ProcessInstanceStep st:step.Steps){
ev.Comments__c=st.Comments;
system.debug('--------------------Steps Ststus----- '+st.StepStatus);
system.debug('##########+comments are----'+st.Comments);
}
/* if(step.Status!='Started'){
ev.Status1__c=step.Status;
}else{
ev.Status1__c='Pending';
}*/
//ev.Status1__c=step.Status;
// ev.Approved_Date__c=system.today();
system.debug('@@@@@@Approval Status---'+step.Status);
}
}
}
}
}
Test Class 27 percent code coverage
============================
@isTest
public class TestEventUpdateTest {
public static testMethod void testcontroller(){
test.startTest();
Account acc=new Account();
acc.Name='test Account';
Opportunity opp = new Opportunity();
opp.Accountid = acc.id;
opp.StageName = '00-Pending';
opp.CloseDate = system.today();
opp.name = 'Test Opportunity';
opp.Hardware__c=true;
opp.No_Of_Units_Of_Hardware__c=5;
opp.INR_Hardware_Contract_Value_In_Lacs__c=5;
opp.Product_Type__c='Hardware';
insert opp;
update opp;
Event objevent=new Event(WhatID = opp.id,Subject='visit');
objevent.DurationInMinutes = 1;
objevent.Subject='Test subject';
objevent.StartDateTime=System.today()-1;
objevent.Tavelled_From__c='kanpur';
objevent.Travelled_To__c='delhi';
objevent.Kms_Travelled__c=10;
objevent.Status__c='New';
objevent.Public_Transport_Fare__c=100;
insert objEvent;
objevent.Tavelled_From__c='lucknow';
objevent.Travelled_To__c='mumbai';
objevent.Kms_Travelled__c=14;
objevent.Public_Transport_Fare__c=200;
update objevent;
map<id,EventApproval__c> mapevntappupdate= new map<id,EventApproval__c>();
EventApproval__c objevent1=new EventApproval__c();
objevent1.Tavelled_From__c='kanpur';
objevent1.Travelled_To__c='delhi';
objevent1.Kms_Travelled__c=10;
objevent1.Kms_Travelled__c=10;
objevent1.Conveyance_Mode__c='test';
objevent1.Telephone_call_type__c='Service call';
objevent1.Reason_for_winning__c='Product quality';
objevent1.Reason_for_dropping__c='Insufficient budget';
objevent1.Reason_for_losing__c='Mapping';
objevent1.OtherDetails__c='Test';
objevent1.OtherDetails1__c='Test1';
objevent1.OtherDetails2__c='Test2';
insert objevent1;
objevent1.Tavelled_From__c='lucknow';
objevent1.Travelled_To__c='mumbai';
objevent1.Kms_Travelled__c=14;
objevent1.Public_Transport_Mode__c='test';
objevent1.Conveyance_Mode__c='test';
objevent1.Sales_Manager_Assistance__c='test';
objevent1.Telephone_call_type__c='Service call';
objevent1.Reason_for_winning__c='Product quality';
objevent1.Reason_for_dropping__c='Insufficient budget';
objevent1.Reason_for_losing__c='Mapping';
objevent1.OtherDetails__c='Test';
objevent1.OtherDetails1__c='Test1';
objevent1.OtherDetails2__c='Test2';
update objevent1;
/*opp.StageName='Prospect';
update opp;
opp.StageName='Proposal in Play';
update opp;
opp.StageName='Demo';
update opp;
opp.StageName='Negotiation';
update opp;
opp.StageName='Closed Won';
update opp;*/
List<Opportunity> oppmapid = [SELECT Id, Name, StageName FROM Opportunity];
system.debug('oppmapID@@@@@@@@@@@@@@@@@@@'+oppmapid);
test.stopTest();
}
}
Apex Trigger
============
trigger EventUpdate on EventApproval__c (after update,before update) {
List<String> ListAuto=new List<String>();
//List<Event>lstEvent=new List<Event>();
map<id,Event> mapevntupdate= new map<id,Event>();
List<Id> EventToBeUpdatedId = new List<Id>();
System.debug('trigger.new---'+trigger.new);
for(EventApproval__c EvAppr:trigger.new){
ListAuto.add(EvAppr.EventAuto__c);
EventToBeUpdatedId.add(EvAppr.Id);
}
System.debug('ListAuto---'+ListAuto);
if(trigger.isAfter && Onetimeinsertupdate.EventUpdatetrigger){
//lstEvent=[Select id,Ownerid,Description,Subject,Tavelled_From__c,Travelled_To__c,Kms_Travelled__c,Public_Transport_Fare__c,EventAuto__c,Reimbursement_Amount__c from Event where (EventAuto__c !=Null and EventAuto__c in :ListAuto) ];
mapevntupdate=new map<id,Event>([Select id,Ownerid,Telephone_call_type__c,EventSubject__c,Description,Subject,Tavelled_From__c,
Travelled_To__c,Kms_Travelled__c,Public_Transport_Fare__c,
EventAuto__c,Reimbursement_Amount__c,
Sales_Manager_Assistance__c
from Event
where (EventAuto__c !=Null and EventAuto__c in :ListAuto) ]);
system.debug('-----------map---'+ mapevntupdate.values());
for(EventApproval__c EvAppr:trigger.new){
for(Event ev: mapevntupdate.values()){
if(EvAppr.EventAuto__c==ev.EventAuto__c){
ev.Tavelled_From__c=EvAppr.Tavelled_From__c;
ev.Travelled_To__c=Evappr.Travelled_To__c;
ev.Kms_Travelled__c=EvAppr.Kms_Travelled__c;
ev.Public_Transport_Fare__c=EvAppr.Public_Transport_Others_Fare__c;
//ev.Reimbursement_Amount__c=Integer.valueof(EvAppr.Reimbursement_Amount__c);
ev.Description=EvAppr.Description__c;
ev.Public_Transport_Mode__c=Evappr.Public_Transport_Mode__c;
ev.Conveyance_Mode__c=EvAppr.Conveyance_Mode__c;
ev.Status__c=EvAppr.Status1__c;
ev.Sales_Manager_Assistance__c=EvAppr.Sales_Manager_Assistance__c;
// new code added by Ashish
ev.Telephone_call_type__c=EvAppr.Telephone_call_type__c;
ev.EventSubject__c=EvAppr.EventSubject__c;
// ev.Reason_for_winning__c=EvAppr.Reason_for_winning__c;
// ev.Reason_for_losing__c=EvAppr.Reason_for_losing__c;
// ev.Reason_for_dropping__c=EvAppr.Reason_for_dropping__c;
// ev.OtherDetails__c=EvAppr.OtherDetails__c;
// ev.OtherDetails1__c=EvAppr.OtherDetails1__c;
// ev.OtherDetails2__c=EvAppr.OtherDetails2__c;//Code End Here
}
}
}
// Logic for updating the Event Status and Comment
List<ProcessInstance>lstprocess = [Select p.TargetObjectId, p.Status, (Select StepStatus,Comments From Steps where StepStatus!='Started') From ProcessInstance p where TargetObjectId in :trigger.newMap.keyset()];
if(lstprocess .size()>0){
for(Event ev:mapevntupdate.values()){
for(ProcessInstance step :lstprocess ){
for( ProcessInstanceStep st:step.Steps){
ev.Comments__c=st.Comments;
system.debug('--------------------Steps Ststus----- '+st.StepStatus);
system.debug('##########+comments are----'+st.Comments);
}
/*if(step.Status!='Started'){
ev.Status__c=step.Status;
}else{
ev.Status__c='Pending';
}*/
ev.Approved_Date__c=system.today();
system.debug('@@@@@@Approval Status---'+step.Status);
}
}
}
system.debug('&&&&&&&&&&&&&&&&&&'+mapevntupdate.size());
if(mapevntupdate.size()>0){
system.debug('&&&&&&&&&&&&&&&&&&');
Onetimeinsertupdate.eventupdateflag=false;
Onetimeinsertupdate.EventUpdatetrigger = false;
//update lstEvent;
update mapevntupdate.values();
system.debug('************'+mapevntupdate.size());
}
//MyFutureClass.myMethod(EventToBeUpdatedId,ListAuto);
Onetimeinsertupdate.EventUpdatetrigger=false;
}
if(Trigger.isBefore){
List<ProcessInstance>lstprocess = [Select p.TargetObjectId, p.Status, (Select StepStatus, Comments From
Steps where StepStatus!='Started') From ProcessInstance p where TargetObjectId in :trigger.newMap.keyset()];
if(lstprocess .size()>0){
for(EventApproval__c Ev:trigger.new){
for(ProcessInstance step :lstprocess ){
for( ProcessInstanceStep st:step.Steps){
ev.Comments__c=st.Comments;
system.debug('--------------------Steps Ststus----- '+st.StepStatus);
system.debug('##########+comments are----'+st.Comments);
}
/* if(step.Status!='Started'){
ev.Status1__c=step.Status;
}else{
ev.Status1__c='Pending';
}*/
//ev.Status1__c=step.Status;
// ev.Approved_Date__c=system.today();
system.debug('@@@@@@Approval Status---'+step.Status);
}
}
}
}
}
Test Class 27 percent code coverage
============================
@isTest
public class TestEventUpdateTest {
public static testMethod void testcontroller(){
test.startTest();
Account acc=new Account();
acc.Name='test Account';
Opportunity opp = new Opportunity();
opp.Accountid = acc.id;
opp.StageName = '00-Pending';
opp.CloseDate = system.today();
opp.name = 'Test Opportunity';
opp.Hardware__c=true;
opp.No_Of_Units_Of_Hardware__c=5;
opp.INR_Hardware_Contract_Value_In_Lacs__c=5;
opp.Product_Type__c='Hardware';
insert opp;
update opp;
Event objevent=new Event(WhatID = opp.id,Subject='visit');
objevent.DurationInMinutes = 1;
objevent.Subject='Test subject';
objevent.StartDateTime=System.today()-1;
objevent.Tavelled_From__c='kanpur';
objevent.Travelled_To__c='delhi';
objevent.Kms_Travelled__c=10;
objevent.Status__c='New';
objevent.Public_Transport_Fare__c=100;
insert objEvent;
objevent.Tavelled_From__c='lucknow';
objevent.Travelled_To__c='mumbai';
objevent.Kms_Travelled__c=14;
objevent.Public_Transport_Fare__c=200;
update objevent;
map<id,EventApproval__c> mapevntappupdate= new map<id,EventApproval__c>();
EventApproval__c objevent1=new EventApproval__c();
objevent1.Tavelled_From__c='kanpur';
objevent1.Travelled_To__c='delhi';
objevent1.Kms_Travelled__c=10;
objevent1.Kms_Travelled__c=10;
objevent1.Conveyance_Mode__c='test';
objevent1.Telephone_call_type__c='Service call';
objevent1.Reason_for_winning__c='Product quality';
objevent1.Reason_for_dropping__c='Insufficient budget';
objevent1.Reason_for_losing__c='Mapping';
objevent1.OtherDetails__c='Test';
objevent1.OtherDetails1__c='Test1';
objevent1.OtherDetails2__c='Test2';
insert objevent1;
objevent1.Tavelled_From__c='lucknow';
objevent1.Travelled_To__c='mumbai';
objevent1.Kms_Travelled__c=14;
objevent1.Public_Transport_Mode__c='test';
objevent1.Conveyance_Mode__c='test';
objevent1.Sales_Manager_Assistance__c='test';
objevent1.Telephone_call_type__c='Service call';
objevent1.Reason_for_winning__c='Product quality';
objevent1.Reason_for_dropping__c='Insufficient budget';
objevent1.Reason_for_losing__c='Mapping';
objevent1.OtherDetails__c='Test';
objevent1.OtherDetails1__c='Test1';
objevent1.OtherDetails2__c='Test2';
update objevent1;
/*opp.StageName='Prospect';
update opp;
opp.StageName='Proposal in Play';
update opp;
opp.StageName='Demo';
update opp;
opp.StageName='Negotiation';
update opp;
opp.StageName='Closed Won';
update opp;*/
List<Opportunity> oppmapid = [SELECT Id, Name, StageName FROM Opportunity];
system.debug('oppmapID@@@@@@@@@@@@@@@@@@@'+oppmapid);
test.stopTest();
}
}
Firstly, could you kindly re-post all of that code by doing the following...
In your reply, click the <> button (highlighted below) at the top of your reply box.
Then paste your code inside the popup text box.
That will make it much easier to read and follow, and it will give your code line numbers that anyone helping can use for reference.
You posted way more code than I care to try to read through when it is not formatted well.
Secondly, in general with code coverage: after you have thought of every way to test your code with positive and negative tests, and have included that in your test class; you sometimes just have to open the class you are trying to cover (in this case your trigger) in Developer Console, then click the Code Coverage button...
...scroll through the code, and for every red line you see, just ask yourself, "What code can I add to the test class that will specifically cause this red line of code to fire?", and then go back into the test class and modify a method or write a new one.
For example, I have to go back into the test class and make sure that I add some code to insert a BillingCity value that has more characters than whatever the CITYFIELDLIMIT is:
In this case I could probably add that as part of an appropriate existing method; but in some cases where the code is more complex or not related to existing methods, I should probably add a new test method. Whatever I do, I make sure I properly catch that error and add an assertion making sure the error is what was expected.
Very tedious and monotonous, I know, but such is the life of testing.
You addressed my first point by re-posting the code. Thank you for that.
However, you did not address my second point....
---
I see that you added a couple of sections of code to your test class.
1) What percentage coverage are you getting now?
2) Which lines are red in your code coverage (or I guess more accurately, lack of code coverage)?
3) May I ask why you have some update calls immediately following an insert call (without first making any additional changes to the record[s]) - does that do something that I am not aware of?