+ Start a Discussion
ankitsrivastav7771.3911494159052937E12ankitsrivastav7771.3911494159052937E12 

How to cover test class for attachment trigger its getting covered if statement from line 13 to 21

TRIGGER-----

trigger Rfleet_DisableAttachements on Attachment(before insert,before delete,after update) {
Id devRecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('RFLEET-OPP-DCVF-RT').getRecordTypeId();
Id devRecordTypeId1 = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('DC-VF Read only RT').getRecordTypeId();
if(trigger.isinsert && trigger.isbefore){
 for (Attachment att:Trigger.new){
        
            String parentObjId = att.ParentId;     //It will get profitability obj id.  
            
           list<opportunity> pid= [select Rfleet_locked__c,recordtypeid from Opportunity where Rfleet_locked__c=true ];// it get list of opp locked records.
               list<Rfleet_Profitability__c> ids=[Select Rfleet_opportunity__c from Rfleet_Profitability__c where id=:att.ParentId];
            for(Rfleet_Profitability__c pro:ids){
                for(opportunity opp:pid){    
                    if(parentObjId.startsWith('a3r') && opp.Rfleet_locked__c==true && opp.id==pro.Rfleet_opportunity__c && opp.recordtypeid==devRecordTypeId )//a3r is the starting sting in ID for all Profitability
                    {
                    att.addError('Since this opportunity was submitted for approval, you are not allowed to add a new attachment.');
                    }
                    else if(parentObjId.startsWith('a3r') && opp.Rfleet_locked__c==true && opp.id==pro.Rfleet_opportunity__c && opp.recordtypeid==devRecordTypeId1)
                    {
                    att.addError('Since this opportunity was closed, you are not allowed to add a new attachment.');
                    }
                }
            }
    }
 }
 if(trigger.isdelete && trigger.isbefore){
     for (Attachment att:Trigger.old){
      String parentObjId = att.ParentId; 
      list<opportunity> pid= [select Rfleet_locked__c,recordtypeid from Opportunity where Rfleet_locked__c=true ];// it get list of opp locked records.
               list<Rfleet_Profitability__c> ids=[Select Rfleet_opportunity__c from Rfleet_Profitability__c where id=:att.ParentId];
            for(Rfleet_Profitability__c pro:ids){
                for(opportunity opp:pid){    
                    if(parentObjId.startsWith('a3r') && opp.Rfleet_locked__c==true && opp.id==pro.Rfleet_opportunity__c && opp.recordtypeid==devRecordTypeId)//a3r is the starting sting in ID for all Profitability
                    {
                    att.addError('Since this opportunity was submitted for approval, you are not allowed to delete the attachment.');
                    }
                    else if(parentObjId.startsWith('a3r') && opp.Rfleet_locked__c==true && opp.id==pro.Rfleet_opportunity__c && opp.recordtypeid==devRecordTypeId1)
                    {
                    att.addError('Since this opportunity was closed, you are not allowed to delete the attachment.');
                    }
                }
            }
     }
 
 }
 if(trigger.isupdate && trigger.isafter){
     for (Attachment att:Trigger.new){
      String parentObjId = att.ParentId; 
      list<opportunity> pid= [select Rfleet_locked__c,recordtypeid from Opportunity where Rfleet_locked__c=true ];// it get list of opp locked records.
               list<Rfleet_Profitability__c> ids=[Select Rfleet_opportunity__c from Rfleet_Profitability__c where id=:att.ParentId];
            for(Rfleet_Profitability__c pro:ids){
                for(opportunity opp:pid){    
                    if(parentObjId.startsWith('a3r') && opp.Rfleet_locked__c==true && opp.id==pro.Rfleet_opportunity__c && opp.recordtypeid==devRecordTypeId)//a3r is the starting sting in ID for all Profitability
                    {
                    att.addError('Since this opportunity was submitted for approval, you are not allowed to update the attachment.');
                    }
                    else if(parentObjId.startsWith('a3r') && opp.Rfleet_locked__c==true && opp.id==pro.Rfleet_opportunity__c && opp.recordtypeid==devRecordTypeId1)
                    {
                    att.addError('Since this opportunity was closed, you are not allowed to update the attachment.');
                    }
                }
            }
     }
 
 }
 }


@testclass

@isTest(SeeAllData=true)
    public class Rfleet_DisableAttachements_Test {
    static testMethod void  Rfleet_DisableAttachements(){
    

    Id devRecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('RFLEET-OPP-DCVF-RT').getRecordTypeId();
    Id devRecordTypeId1 = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('DC-VF Read only RT').getRecordTypeId();
    //RecordType rt = [select Id from RecordType where Name = 'RFLEET-OPP-DCVF-RT' and SobjectType = 'opportunity' LIMIT 1];
    Opportunity opp=new Opportunity();
    //opp.RecordTypeId = rt.id; 
    opp.RecordTypeId=devRecordTypeId ;
    opp.Rfleet_locked__c = True;
    
    opp.name='fdf';
    //opp.id='a3rm0000000D6Z5';
    //opp.id==pro.Rfleet_opportunity__c
    
    opp.stagename='Internal Approbation';
    opp.closedate=System.Today();
    insert opp;
    opp.stagename='Proposal to Customer';
    update opp;

    Attachment attach=new Attachment();
     //attach.id=opp.Id;
    attach.Name='TestAtt'; 
    Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body'); 
    attach.body=bodyBlob; 
    attach.parentId=opp.Id; 
    insert attach; 
    
    Rfleet_Profitability__c Pro=new Rfleet_Profitability__c();
    pro.Rfleet_opportunity__c=opp.Id;
    //pro.id=attach.id;
    
  // pro.id='a3rm0000000D6Z5';
  
           try{
          insert pro;

              }catch (DMLException e) {} 

    List<Attachment> attachments=[select id, name from Attachment where parent.id=:opp.id ];
    System.assertEquals(1, attachments.size());
     attach = [SELECT Id, name from Attachment where parent.id=:opp.id];
     //System.assertEquals(pro.Id, attach.ParentId);
     update attach;
     delete attach;  

         }
         }
Mathew Andresen 5Mathew Andresen 5
If you paste this in as code it would be much easier to look at line numbers
ankitsrivastav7771.3911494159052937E12ankitsrivastav7771.3911494159052937E12
___thuis particular section is not getting covered---------------


for(opportunity opp:pid){    
                    if(parentObjId.startsWith('a3r') && opp.Rfleet_locked__c==true && opp.id==pro.Rfleet_opportunity__c && opp.recordtypeid==devRecordTypeId )//a3r is the starting sting in ID for all Profitability
                    {
                    att.addError('Since this opportunity was submitted for approval, you are not allowed to add a new attachment.');
                    }
                    else if(parentObjId.startsWith('a3r') && opp.Rfleet_locked__c==true && opp.id==pro.Rfleet_opportunity__c && opp.recordtypeid==devRecordTypeId1)
                    {
                    att.addError('Since this opportunity was closed, you are not allowed to add a new attachment.')