• ashish gupta 61
  • NEWBIE
  • 0 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 2
    Replies
Hi All,
I have to avoid User Soql inside for loop. I have a custom object filldata__c. I need each user record for every filldata ID and want to avoid this user SOql query in for loop

 List<fill_Data__c> fillDatalist = [Select id,CreatedById,Action_Type__c from fill_Data__c Where Action_Type__c='BulkParent'];
for(fill_Data__c obj:fillDatalist){
    fillIDWithCreatedBYIDMap.put(obj.ID,obj.CreatedById); 
}
for(ID fillID :fillIDWithCreatedBYIDMap.keyset()){
    if(fillIDWithCreatedBYIDMap.containsKey(fillID)){
        
       User user=[select ID,FirstName,LastName,email from user where Id =:fillIDWithCreatedBYIDMap.get(fillID) limit 1];  // I  want to avoid this user SOql query in for loop
    }
}
Please check above code and provide your solution It would be great help.
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(); 
}
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(); 
}