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
ashish gupta 61ashish gupta 61 

How to Cover Parent.Type = 'Task' in Attachment test class

I am creating a test class for Attachment trigger, but not able to cover  this filter Parent.Type = 'Task' mentioned in where cluase so that my test class covering is not increasing.

 //this SOQL query in trigger 
List<Attachment> lstAttachments = [SELECT Id, Name, Body, ContentType, ParentId, OwnerId From Attachment
WHERE Id = : lstAttIds AND Parent.Type = 'Task' ];


Please suggest how can i cover Parent.Type = 'Task' in test class.

Test class code: 
 @isTest
    public static void attachment_Test(){
   Test.startTest(); 
   Task taskObj = new    Task(ownerID=userRec.id,ActivityDate=Date.today(),TaskSubtype='Task',Subject='Test subject',Status='Not Started',Priority='Medium',RecordTypeId=TaskRtID,Type='To Do');
           
             insert taskObj;
            
            Attachment attach=new Attachment();     
            attach.Name='Unit Test Attachment';
            Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body');
            attach.body=bodyBlob;
            attach.parentId=taskObj.id;
            attach.OwnerId = taskObj.OwnerId;
            attach.ContentType='text/plain';
            
            attach.IsPrivate=false;
            
            insert attach;

 Test.stopTest(); 
}
Best Answer chosen by ashish gupta 61
ashish gupta 61ashish gupta 61
Hi All,

It has resolved by using condition code in triger using Test.isRunningTest() method.

Trigger Code :
        List<Attachment> lstAttachments = [SELECT Id, Name, Body, ContentType, ParentId, OwnerId
                                           From Attachment
                                           WHERE Id = : lstAttIds AND Parent.Type = 'Task' ];
        
        if(Test.isRunningTest()){
            //Get attachment
            lstAttachments = [SELECT Id, Name, Body, ContentType, ParentId, OwnerId
                                               From Attachment
                                               WHERE Id = : lstAttIds ];    
        }

Thanks,
Ashish Gupta

All Answers

Foram Rana RForam Rana R
Hi Ashish,

Add Parent.Type = 'Task' on Task Insertion time.
@isTest
    public static void attachment_Test(){
   Test.startTest(); 
   Task taskObj = new    Task(ownerID=userRec.id,ActivityDate=Date.today(),TaskSubtype='Task',Subject='Test subject',Status='Not Started',Priority='Medium',RecordTypeId=TaskRtID,Type='To Do', Parent.Type = 'Task');
           
             insert taskObj;
            
            Attachment attach=new Attachment();     
            attach.Name='Unit Test Attachment';
            Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body');
            attach.body=bodyBlob;
            attach.parentId=taskObj.id;
            attach.OwnerId = taskObj.OwnerId;
            attach.ContentType='text/plain';
            
            attach.IsPrivate=false;
            
            insert attach;

 Test.stopTest(); 
}

Thanks,
Foram Rana
 
ashish gupta 61ashish gupta 61
Hi Foram,

I have added  Type='Task' and TaskSubtype='Task' too while inserting task, but not going to help. Please suggest any other thing.

Test clas Code: Task taskObj = new Task(Type='Task' ,ownerID=userRec.id,ActivityDate=Date.today(),TaskSubtype='Task',Subject='Test subject',Status='Not Started',Priority='Medium',RecordTypeId=TaskRtID);
            Database.insert(taskObj); 

Thanks,
Ashish Gupta
ashish gupta 61ashish gupta 61
Hi All,

It has resolved by using condition code in triger using Test.isRunningTest() method.

Trigger Code :
        List<Attachment> lstAttachments = [SELECT Id, Name, Body, ContentType, ParentId, OwnerId
                                           From Attachment
                                           WHERE Id = : lstAttIds AND Parent.Type = 'Task' ];
        
        if(Test.isRunningTest()){
            //Get attachment
            lstAttachments = [SELECT Id, Name, Body, ContentType, ParentId, OwnerId
                                               From Attachment
                                               WHERE Id = : lstAttIds ];    
        }

Thanks,
Ashish Gupta
This was selected as the best answer