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
Umadevi SUmadevi S 

how to write test class for the attachments ???

Hi Team,

I need to make mandatory attachment Probability is 100 %, also Check box field(Invoiced__c) is true then attchment is mandatory.

I write the below trigger and it working fine. I don't how can i write the test class for that trigger.
 
trigger ClosedOpportunityTrigger on Opportunity (before update) {
    
    
    for(Opportunity o:Trigger.New) {
    if(o.Probability==100 && o.Invoiced__c == true)
    {
        
            
            Attachment a = new Attachment();
            try {
               a = [Select Id, Name from Attachment where ParentId =:o.Id];
            }
            catch(Exception e) {
               a = null;
            }
            
            if (a == null)
               o.addError('Add an attachment before you close the Opportunity');
        }
    }
   }
This my Test Class

@isTest
private class ClosedOpportuntyTriggerTest{
    
    static testmethod void methodOne(){
        
        /*contact objc = new contact();
        objc.Lastname = 'test';
        objc.Email ='umadevi.s@bookmyshow.com';
        insert objc;*/
        Opportunity objOpp = new Opportunity();
       objOpp.Name = 'test opp';
        objOpp.StageName = 'Payment Receiveed';
        objOpp.Probability = 80;
       // objOpp.Contact__c = objc.id;
        objOpp.CloseDate = system.today();
        objOpp.Booking_Ids__c ='12345';
        insert objOpp;
        
        blob attBlob;   
        
        attBlob = EncodingUtil.base64Decode('AA=='); // zero-length, null-terminated string
        
        Attachment objAttach = new Attachment();
        objAttach.OwnerId = UserInfo.getUserId();
        objAttach.Name = 'test.pdf';
        objAttach.ParentId = objOpp.Id; // the record the file is attached to
        objAttach.IsPrivate = true;
        objAttach.Body = attBlob;
        objAttach.ContentType = 'application/pdf';
        insert objAttach;
        
        objOpp.StageName = 'Payment Confirmation';
        objOpp.Probability = 100;
        objOpp.Invoiced__c = true;
        
        objOpp.Probability = 100;
        
        update objOpp;
        
    }
}
And i got Error Add one Product 
 
Rahul KumarRahul Kumar (Salesforce Developers) 
Hi Umadevi, hope it helps.

Please mark it as best answer if the information is informative.

Thanks
Rahul Kumar
Aman PathakAman Pathak
@istest
public class testclassoppTrigger {
    @istest 
    Static void closeopp()
    {
       opportunity opp = new opportunity();
        opp.Name ='test';
        opp.StageName ='Closed Won';
        opp.CloseDate =system.today();
        insert opp;
        
        opp.Probability =100;
        opp.Invoiced__c =true;
        
        update opp;
        
    }

}

this is fairly simple , you don't need to pass the Ids or insert attachment (trigger will do that for you) as you were doing in your test class.
just insert the opp and update it.( as trigger is fired on update).

Please mark it as best answer if this answer helps you.

Thanks
Aman Pathak
Umadevi SUmadevi S
FIELD_CUSTOM_VALIDATION_EXCEPTION, Please add atleast one BMS Event for change status: [] i get error
Aman PathakAman Pathak
Ohhh, this was such a good one, In your case it was due to AddError scenerio.
"just add a try catch block  when you update Opp in test class , that will do.
this FIELD_CUSTOM_VALIDATION_EXCEPTION is due to " o.addError('Add an attachment before you close the Opportunity');" 
 below is the working code 
with 100% coverage and all methods passed.
 
@istest
public class testclassoppTrigger {
    @istest 
    Static void closeopp()
    {
       opportunity opp = new opportunity();
        opp.Name ='test';
        opp.StageName ='Closed Won';
        opp.CloseDate =system.today();
        opp.Probability =100;
        insert opp;
        blob attBlob;   
        
        attBlob = EncodingUtil.base64Decode('AA==');
       
        attachment  at = new attachment();
        at.ParentId = opp.Id;  
        at.Name ='testattach';
        at.Body = attblob;
        insert at;
        opp.Invoiced__c =true;
        //update opp;
        try{
                update opp;}
            catch(exception ex){
           Boolean expectedExceptionThrown =  ex.getMessage().contains('Add an attachment before you close the Opportunity') ? true : false;
                System.assertEquals(expectedExceptionThrown, false);}
        
        
        
        
    }
   @istest 
    Static void closeopp1()
    {   
        test.startTest();
        Profile p = [select id from profile where name='Account'];
        User u = new User(
        alias = 'ddim', 
        email='noemail@noemail.com', 
        timezonesidkey='America/Los_Angeles', 
        localesidkey='en_US', 
        emailencodingkey='UTF-8', 
        ProfileId = p.Id,
        languagelocalekey='en_US', 
        lastname='Testing', 
        Firstname='Testing', 
        CompanyName= 'xyz', 
        username='dim71@noemail.com');
        insert u;
        
        //system.runas(u)
        { 
        opportunity opp = new opportunity();
        opp.Name ='test';
        opp.StageName ='Closed Won';
        opp.CloseDate =system.today();
        opp.Probability =100;
        insert opp;
        opp.Invoiced__c =true;
            try{
                update opp;}
            catch(exception ex){
           Boolean expectedExceptionThrown =  ex.getMessage().contains('Add an attachment before you close the Opportunity') ? true : false;
                System.assertEquals(expectedExceptionThrown, true);}
        test.stopTest();
        }
        
    }

}


Please mark it as best answer if this answer helps you.

Thanks
Aman Pathak
Umadevi SUmadevi S
Hi Aman,

I got the error msg like need add the atleast one Product for the Opportunity.
Aman PathakAman Pathak
strange, as I am not getting any error, plz check if there is any other validation or trigger is there that might casue this ,use the deubug log to find that out.(in dev console you can see that under log section, handy tool).

for product , you have to use opportunitylineItem or may be Product2 intest class ,I am not sure of that(depend on your org)  Please check. 

also I quite didn't understant your code , 
you are setting "a = null "in Catch block , why?
you can add the error in catch block itself