function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Anju Alexander 8Anju Alexander 8 

Test Class related to Approval Process

Hi All,

I have Opportunity and Attachment. Now if i am attaching any document with name OPF, the checkbox,OPF_Uploaded__c gets checked. I have written a trigger on Opportunity, for Automated Approval Process with condition that if( OPF_Uploaded__c ==true), then submit for Approval Process. Now this trigger is working.

I have also written a trigger upon Opportunity(after update). If after the uploading of Attachment with name, OPF(now the OPF_Uploaded__c in opportunity gets checked) and after this if I am updating this Opportunity Record and unchecking the OPF_Uploaded__c , the attachment with name,OPF gets deleted using this trigger.

Now the problem is that I have written test class for this trigger. In the test class after insertion, the record is going for Approval Process...So the deletion is not happening...and my trigger is not getting covered?

Below is my trigger code.....
trigger deleteOpportunityDoc on Opportunity(after update) {
deleteOpportunityDocHandler handler=new deleteOpportunityDocHandler();

/*if(Trigger.isDelete)
 {
 handler.AfterDelete(trigger.old);
 }*/
 
 /*if(Trigger.isInsert)
 {
 handler.AfterInsert(trigger.new);
 }*/
 
 if(Trigger.isUpdate)
 {
 handler.AfterUpdate(trigger.new);
 }
 /*if(Trigger.isUndelete)
 {
 handler.AfterUndelete(trigger.new);
 }
 */
 
}
Handler class:

public class deleteOpportunityDocHandler
{
public void AfterUpdate(Opportunity[] newObjects)

{
Set<Id> oppIdOPF=new Set<Id>();
    Set<Id> oppIdPO=new Set<Id>();
    Set<Id> oppIdQuote=new Set<Id>();
    Set<Id> oppIdWS=new Set<Id>();    
    List<Attachment> attachOPF = new List<Attachment>();
    List<Attachment> attachPO = new List<Attachment>();
    List<Attachment> attachQuote = new List<Attachment>();
    List<Attachment> attachWS = new List<Attachment>();
for(Opportunity opp:newObjects)   
{
if(opp.OPF_Uploaded__c==false)
{
  oppIdOPF.add(opp.id);
}
}
attachOPF=[SELECT id,parentid,name FROM attachment where parentId IN:oppIdOPF and name LIKE '%OPF%'];
if(attachOPF.size() > 0){
delete attachOPF;
}

for(Opportunity opp:newObjects)   
{
if(opp.PO_Uploaded__c==false)
{
  oppIdPO.add(opp.id);
}
}
attachPO=[SELECT id,parentid,name FROM attachment where parentId IN:oppIdPO and name LIKE '%PO%'];
if(attachPO.size() > 0){
delete attachPO;
}


for(Opportunity opp:newObjects)   
{
if(opp.Quote_Uploaded__c==false)
{
  oppIdQuote.add(opp.id);
}
}
attachQuote=[SELECT id,parentid,name FROM attachment where parentId IN:oppIdQuote and name LIKE '%Quote%'];
if(attachQuote.size() > 0){
delete attachQuote;
}


for(Opportunity opp:newObjects)   
{
if(opp.Working_Sheet_Uploaded__c==false)
{
  oppIdWS.add(opp.id);
}
}
attachWS=[SELECT id,parentid,name FROM attachment where parentId IN:oppIdWS and name LIKE '%Working%'];
if(attachWS.size() > 0){
delete attachWS;
}


}
}

My test class:

@isTest(SeeAllData=false)
public with sharing class Test_deleteOpportunityDoc {

   static testMethod void myTestMethod() {
    Account ac = new Account();
    ac.name='Test';
    Date myDate = date.newInstance(2012,05,22);
    ac.Date_of_Enquiry__c=myDate;
    ac.E_Mail_ID__c='anju@gmail.com';
       
     insert ac;
       
    Opportunity  oppo=new Opportunity();
    oppo.AccountId=ac.id;
    oppo.name='Test';
    Date myDate1 = date.newInstance(2012,05,22);
    oppo.CloseDate=myDate1;
    oppo.StageName='Prospecting';
    oppo.OPF_Uploaded__c=false; 
    oppo.PO_Uploaded__c=false;
    oppo.Quote_Uploaded__c=false;
    oppo.Working_Sheet_Uploaded__c=false;  
 
       try {
            insert oppo;  
        }
         Catch(DMLException e) {
             system.assertEquals('Process failed. First exception on row 0; first error: ALREADY_IN_PROCESS, Cannot submit object already in process: []', e.getMessage());
         }  
   
       
    
    Attachment att=new Attachment();
    Blob b = Blob.valueOf('Test Data');  
    att.Name='OPF';
    att.parentid=oppo.id;

    att.body=b;
    insert att;  
       
       Attachment att1=new Attachment();
    Blob b1 = Blob.valueOf('Test Data');  
    att1.Name='OPF';
    att1.parentid=oppo.id;
    att1.body=b1;
    insert att1;
    
    Attachment att2=new Attachment();
    Blob b2 = Blob.valueOf('Test Data');  
    att2.Name='OPF';
    att2.parentid=oppo.id;
    att2.body=b2;
    insert att2;
       
    Attachment att3=new Attachment();
    Blob b3 = Blob.valueOf('Test Data');  
    att3.Name='OPF';
    att3.parentid=oppo.id;
    att3.body=b3;
    insert att3;     
      oppo.OPF_Uploaded__c=false;
      oppo.PO_Uploaded__c=false;
      oppo.Quote_Uploaded__c=false;
      oppo.Working_Sheet_Uploaded__c=false; 
    update oppo; 
    
   }
}
Please help....what to do to cover delete statement
 
Abhi_TripathiAbhi_Tripathi
Hi Anju,

You need to modify something in the trigger code then it will cover as per your test class

In you trigger you are checking Working_Sheet_Uploaded__c == true, but you are not checking the old value , that will run the your trigger in loop.
So modify the trigger and check the old value of Working_Sheet_Uploaded__c == true and new value of Working_Sheet_Uploaded__c == false then add id of the oppotunity in list and delete the Attachements.
Something like this
//Compare old value with new value
if(opp.Working_Sheet_Uploaded__c == false && oldmap.get(opp.id).Working_Sheet_Uploaded__c == true) {
       oppIdWS.add(opp.id);
}

//query attachements and delete
attachWS=[SELECT id,parentid,name FROM attachment where parentId IN:oppIdWS and name LIKE'%Working%'];
if(attachWS.size() > 0){
      delete attachWS;
   }
}


Regards,
Abhi Tripathi
Salesforce Developer
http://abhithetechknight.blogspot.in/
Anju Alexander 8Anju Alexander 8
Hi Abhi, Thanks for pointing out the mistake in the trigger. And thanks for explaining clearly how to do it. Regards, Anju