+ Start a Discussion
kiran k 12kiran k 12 

inside for loop not covered

I have wriiten a test class for trigger.i got 42% code coverage.the coverage is not entering in for loop.i have instantiate the fields based on for loop,but not covered.how to cover FOR LOOP in test class?please can you anybody help me.
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.');
                    }
                }
            }
     }
 
 }
 }
 
test class:
@isTest
public class Rfleet_DisableAttachements_Test {
static testMethod void  Rfleet_DisableAttachements(){

Account acc = new Account(Name='vfvh',montant__c=0.3);
insert acc;
acc.name='hjghg';
update acc;
  
Opportunity opp=new Opportunity();
RecordType rt = [select Id from RecordType where Name = 'RFLEET-OPP-DCVF-RT' and SobjectType = 'opportunity' LIMIT 1];
opp.RecordTypeId = rt.id; 
opp.Rfleet_locked__c = false;
opp.name='fdf';
opp.stagename='Draft';
opp.closedate=System.Today();
insert opp;
opp.name='save';

update opp;


Rfleet_Profitability__c Pro=new Rfleet_Profitability__c();
Pro.name='Testpro';
Pro.Rfleet_opportunity__c=opp.id;


Attachment attach=new Attachment(); 
attach.Name='TestAtt'; 
Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body'); 
attach.body=bodyBlob; 
attach.parentId=opp.Id; 
insert attach; 
       
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];
 delete attach;  
       
     }
    
 }

User-added image
Sumit Kumar Singh 9Sumit Kumar Singh 9
Hello Kiran, 

I would advise you to optimize your trigger code. 
I can see SOQL statements inside for loop, which is not a good practice and most likely you will get "Too many DML statements" at some point of time.

Now come to your test coverage - 
list<Rfleet_Profitability__c> ids=[Select Rfleet_opportunity__c from Rfleet_Profitability__c where id=:att.ParentId];
The above SOQL is not returning any rows. 
Rfleet_Profitability__c Pro=new Rfleet_Profitability__c();
Pro.name='Testpro';
Pro.Rfleet_opportunity__c=opp.id;
Pro.Rfleet_Profitability__c=attach.id;
insert Pro;
Add above lines below the attachment statement. 


Thanks,
Sumit Kumar Singh