• Jeyabharathi Rajendran 2
  • NEWBIE
  • 10 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
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.
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.