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
salesforce_hoonigansalesforce_hoonigan 

Help with error on Test Class: System.DmlException: Update failed. First exception on row 0 with id a1gJ0000001GZsCIAW; first error: TRANSFER_REQUIRES_READ, The new owner must have read permission: []

Hi All,

I'm newb on Test Classes. Any assistance is gladly appreciated. I am having errors when trying to Run the Test Class I've created, it is showing 100% code coverage but has an error on Test Class. It is showing "System.DmlException: Update failed. First exception on row 0 with id a1gJ0000001GZsCIAW; first error: TRANSFER_REQUIRES_READ, The new owner must have read permission: []

Apex Class:
 
trigger SyncOwner on Project_and_Task__c (before insert, before update) {

  for(Project_and_Task__c pt:Trigger.new)                    // For each record
    if(Trigger.isInsert)                                     // If we're inserting
      pt.OwnerId = pt.Assigned_To_Owner__c;                  // Assign ownership from Assigned__c
    else                                                     // Otherwise (an update)
      if(pt.OwnerId != Trigger.oldMap.get(pt.id).OwnerId)       // If ownership has changed
        pt.Assigned_To_Owner__c = pt.OwnerId;                // Place the new owner is assigned
      else                                                   // Otherwise (owner not changed, is an update)
        if(pt.Assigned_To_Owner__c != Trigger.oldMap.get(pt.id).Assigned_To_Owner__c)     // If the Assigned__c field changed
          pt.OwnerId = pt.Assigned_To_Owner__c;             // Assigned ownership from Assigned__c
}
Test Class
@isTest
private class TestSyncOwner {
  static testMethod void test() {
    List<User> testUsers = [select id from user where isactive = true limit 2];
        
    List<Project_and_Task__c> pt= new List<Project_and_Task__c>();
    pt.add(new Project_and_Task__c(Name='test',Assigned_To_Owner__c=testUsers[0].Id));
    pt.add(new Project_and_Task__c(Name='test',Assigned_To_Owner__c=testUsers[1].Id));
    insert pt;

    Map<Id, Project_and_Task__c> refMap = new Map<Id, Project_and_Task__c>([SELECT Id, Assigned_To_Owner__c, OwnerId FROM Project_and_Task__c WHERE Id IN :pt]);

    system.assertEquals(refMap.get(pt[0].Id).OwnerId,testUsers[0].Id); // OwnerId should equal Assigned_To_Owner__c;
    system.assertEquals(refMap.get(pt[1].Id).OwnerId,testUsers[1].Id); // OwnerId should equal Assigned_To_Owner__c;
    pt[0].OwnerId = testUsers[1].Id;
    pt[1].Assigned_To_Owner__c = testUsers[0].Id;
    update pt;
    
    Map<Id, Project_and_Task__c> refMap2 = new Map<Id, Project_and_Task__c>([SELECT Id, Assigned_To_Owner__c, OwnerId FROM Project_and_Task__c WHERE Id IN :pt]);
    
    system.assertEquals(refMap2.get(pt[0].Id).Assigned_To_Owner__c,testUsers[1].Id); // Assigned_To_Owner__c should equal OwnerId now
    system.assertEquals(refMap2.get(pt[1].Id).OwnerId,testUsers[0].Id); // OwnerId should equal Assigned_To_Owner__c now
  }  
}
I will appreciate any help

 
Best Answer chosen by salesforce_hoonigan
Abhishek BansalAbhishek Bansal
Hi,

Please update your code as below :
 
@isTest
private class TestSyncOwner {
  static testMethod void test() {
    List<User> testUsers = [select id from user where isactive = true AND Profile.Name like '%System Admin%' limit 2];
        
    List<Project_and_Task__c> pt= new List<Project_and_Task__c>();
    pt.add(new Project_and_Task__c(Name='test',Assigned_To_Owner__c=testUsers[0].Id));
    pt.add(new Project_and_Task__c(Name='test',Assigned_To_Owner__c=testUsers[1].Id));
    insert pt;

    Map<Id, Project_and_Task__c> refMap = new Map<Id, Project_and_Task__c>([SELECT Id, Assigned_To_Owner__c, OwnerId FROM Project_and_Task__c WHERE Id IN :pt]);

    system.assertEquals(refMap.get(pt[0].Id).OwnerId,testUsers[0].Id); // OwnerId should equal Assigned_To_Owner__c;
    system.assertEquals(refMap.get(pt[1].Id).OwnerId,testUsers[1].Id); // OwnerId should equal Assigned_To_Owner__c;
    pt[0].OwnerId = testUsers[1].Id;
    pt[1].Assigned_To_Owner__c = testUsers[0].Id;
    update pt;
    
    Map<Id, Project_and_Task__c> refMap2 = new Map<Id, Project_and_Task__c>([SELECT Id, Assigned_To_Owner__c, OwnerId FROM Project_and_Task__c WHERE Id IN :pt]);
    
    system.assertEquals(refMap2.get(pt[0].Id).Assigned_To_Owner__c,testUsers[1].Id); // Assigned_To_Owner__c should equal OwnerId now
    system.assertEquals(refMap2.get(pt[1].Id).OwnerId,testUsers[0].Id); // OwnerId should equal Assigned_To_Owner__c now
  }  
}
As you are randomly fetching the user from database so it might be possible that the User returned by query may not have the Read permission on Project_and_Task__c objects.
So you should query the User that have system admin profile so that you will not face any issues related to permissions.

Note : User data is not visible unless you use seeAllData = true in your test class. So please make sure that this annotation is set to true while firing query on USER data.

Please let me know if you need more help on this.

Thanks,
Abhishek.

All Answers

Abhishek BansalAbhishek Bansal
Hi,

Please update your code as below :
 
@isTest
private class TestSyncOwner {
  static testMethod void test() {
    List<User> testUsers = [select id from user where isactive = true AND Profile.Name like '%System Admin%' limit 2];
        
    List<Project_and_Task__c> pt= new List<Project_and_Task__c>();
    pt.add(new Project_and_Task__c(Name='test',Assigned_To_Owner__c=testUsers[0].Id));
    pt.add(new Project_and_Task__c(Name='test',Assigned_To_Owner__c=testUsers[1].Id));
    insert pt;

    Map<Id, Project_and_Task__c> refMap = new Map<Id, Project_and_Task__c>([SELECT Id, Assigned_To_Owner__c, OwnerId FROM Project_and_Task__c WHERE Id IN :pt]);

    system.assertEquals(refMap.get(pt[0].Id).OwnerId,testUsers[0].Id); // OwnerId should equal Assigned_To_Owner__c;
    system.assertEquals(refMap.get(pt[1].Id).OwnerId,testUsers[1].Id); // OwnerId should equal Assigned_To_Owner__c;
    pt[0].OwnerId = testUsers[1].Id;
    pt[1].Assigned_To_Owner__c = testUsers[0].Id;
    update pt;
    
    Map<Id, Project_and_Task__c> refMap2 = new Map<Id, Project_and_Task__c>([SELECT Id, Assigned_To_Owner__c, OwnerId FROM Project_and_Task__c WHERE Id IN :pt]);
    
    system.assertEquals(refMap2.get(pt[0].Id).Assigned_To_Owner__c,testUsers[1].Id); // Assigned_To_Owner__c should equal OwnerId now
    system.assertEquals(refMap2.get(pt[1].Id).OwnerId,testUsers[0].Id); // OwnerId should equal Assigned_To_Owner__c now
  }  
}
As you are randomly fetching the user from database so it might be possible that the User returned by query may not have the Read permission on Project_and_Task__c objects.
So you should query the User that have system admin profile so that you will not face any issues related to permissions.

Note : User data is not visible unless you use seeAllData = true in your test class. So please make sure that this annotation is set to true while firing query on USER data.

Please let me know if you need more help on this.

Thanks,
Abhishek.
This was selected as the best answer
salesforce_hoonigansalesforce_hoonigan
Thank you Abhishek! It worked.