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
Jeyabharathi Rajendran 2Jeyabharathi Rajendran 2 

Apex Test Class for opportunity

Hi Guys
   I am new to coding and i need your help. My task is to create a trigger which will allow to move the opportunity stage from qualification to estimation only if the record has an attachment. I found a trigger for this but i need a test class.

Apex Trigger
trigger AttachmentTrigger on Opportunity (before update) {
    
    
    for(Opportunity o:Trigger.New) {
        if(o.stagename == 'Estimating') {
            
            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 change the Opportunity stage to Estimating');
        }
    }
}

Apex Class:
@isTest 
public class AttachmentTriggerTest {
    static testMethod void testMethod1(){
        Opportunity opp = new Opportunity();
          
            opp.Name = 'Test Opportunity';
            opp.CloseDate = system.today();
            opp.StageName = 'Qualification';
            opp.Type = 'New Customer';
        insert opp;
        
        Attachment attach=new Attachment();       
         attach.Name='Unit Test Attachment';
         Blob bodyBlob = Blob.valueOf('Unit Test Attachment Body');
          attach.body = bodyBlob;
          attach.parentId = Opp.id;
        insert attach;
        
        }
}


i don't know why the apex class is not working. Iried deploy it in my production but it says 0% code coverage.
Best Answer chosen by Jeyabharathi Rajendran 2
AmarpreetAmarpreet
Hi Jeyabharathi , 

Please try below code:
@isTest
public class AttachmentTriggerTest {
    static testMethod void testMethod1(){
        Opportunity opp = new Opportunity();
        
        opp.Name = 'Test Opportunity';
        opp.CloseDate = system.today();
        opp.StageName = 'Qualification';
        opp.Type = 'New Customer';
        insert opp;
        
        Attachment attach=new Attachment();       
        attach.Name='Unit Test Attachment';
        Blob bodyBlob = Blob.valueOf('Unit Test Attachment Body');
        attach.body = bodyBlob;
        attach.parentId = Opp.id;
        insert attach;
        
        opp.StageName = 'Estimating'; 
        update opp;
        
        try{
            delete attach;
            update opp;
        }
        catch(Exception e){
            
        }
    }
    
}

As your trigger will execute on "before update", you need to update the record once in Test Class. Please try and let me know in case of any isuee.

All Answers

AmarpreetAmarpreet
Hi Jeyabharathi , 

Please try below code:
@isTest
public class AttachmentTriggerTest {
    static testMethod void testMethod1(){
        Opportunity opp = new Opportunity();
        
        opp.Name = 'Test Opportunity';
        opp.CloseDate = system.today();
        opp.StageName = 'Qualification';
        opp.Type = 'New Customer';
        insert opp;
        
        Attachment attach=new Attachment();       
        attach.Name='Unit Test Attachment';
        Blob bodyBlob = Blob.valueOf('Unit Test Attachment Body');
        attach.body = bodyBlob;
        attach.parentId = Opp.id;
        insert attach;
        
        opp.StageName = 'Estimating'; 
        update opp;
        
        try{
            delete attach;
            update opp;
        }
        catch(Exception e){
            
        }
    }
    
}

As your trigger will execute on "before update", you need to update the record once in Test Class. Please try and let me know in case of any isuee.
This was selected as the best answer
Jeyabharathi Rajendran 2Jeyabharathi Rajendran 2
Thank you so much amarpreet. It works.