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
Connor Small 5Connor Small 5 

Test Class for Controller that only Queries Record

public class MyController {
    
 public Account account  { get; set; }
        
        public MyController() {
        account = [SELECT Id, Name, Billing_Tier__c, Primary_Contact_Full_Name__C,Secondary_Contact_Full_Name__c,One_Line_Address__c FROM Account
                   WHERE Id = :ApexPages.currentPage().getParameters().get('id')];

	}
}

This controller works for a VF page I have that renders other pages if they meet criteria. 

This Controller also achieves for the same purpose 
public class MyPageController {
 
    Public final Account account;
 
    	public MyPageController() {
        account = [SELECT Id, Name, Billing_Tier__c, Primary_Contact_Full_Name__C,Secondary_Contact_Full_Name__c,One_Line_Address__c FROM Account
                   WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
    }
    	public Account getAccount() {
        return account;
    }
    	public PageReference Save(){
        Update account;
        Return Null;
    }

}

This Test Class (for the second controller) achieves 7/8 coverage in Sandbox but fails deployment because of error below

"System.DmlException: Update failed. First exception on row 0 with id 0012E00002KmF35QAF; first error: INVALID_FIELD_FOR_INSERT_UPDATE, Unable to create/update fields: Name. Please check the security settings of this field and verify that it is read/write for your profile or permission set.: [Name]
Stack Trace: Class.MyPageController.Save: line 13, column 1 Class.MyPageControllerTest.testMethodMyController: line 20, column 1"

Anyone have any insight as to how to test Controller 1 or improve test for controller 2 so as not to fail deployment for that reason?
Best Answer chosen by Connor Small 5
AnudeepAnudeep (Salesforce Developers) 
Hi Connor, 

This appears to be an access issue on the Name field for the user running in the test context. I recommend checking the following

Query on UserFieldAccess

Obtain the Id of the user who is running in the test context (You can find the id from the debug logs) and execute the following queries (Note: These are sample queries)
 
SELECT Durableid FROM UserFieldAccess WHERE FieldDefinition.EntityDefinition.QualifiedApiName='Account' AND FieldDefinition.QualifiedApiName='Name' AND User.Id='<userId for the user you want to check field access for>'

Use the unique DurableId returned above to query UserFieldAccess.
SELECT DurableId,EntityDefinitionId,FieldDefinitionId,Id,IsAccessible,IsCreatable,IsUpdatable,UserId FROM UserFieldAccess WHERE DurableId = '<DurableId returned in query 1>'

Replace Account Name with OwnerId or any other field that you want to check the access for

Let me know if this helps

Anudeep