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
daniel_hdaniel_h 

UserRecordAccess query not working in test

I'm playing around with my Spring '12 sandbox, trying out the UserRecordAccess object. First my scenario: I want to allow all users who can edit a contact record be able to delete the record, regardless of who owns it. I've created a custom button that executes my code using a web service. This all works fine in the UI, but my tests fail because when it tries to query the UserRecordAccess, no records are returned. I get a "System.QueryException: List has no rows for assignment to SObject"

 

I've also tried running the SOQL in the developer console and I get an internal Salesforce error, so I think something must be up.

 

Here's my code with test class:

 

global without sharing class ContactUtil {
    webService static String deleteContact(Id id) { 
        Contact c = new Contact(id = id);
        try {
            UserRecordAccess ura = [select RecordId, HasEditAccess from UserRecordAccess where UserId = :UserInfo.getUserId() and RecordId = :id];
            if (ura.HasEditAccess) 
                delete c;
            else 
                throw new deleteException('Error deleting');
            return '';
        } catch (Exception e) {
            return e.getMessage();
        }
    }
    
    @isTest
    static void testDeleteContact() {
        Contact c = new Contact(LastName = 'Test');
        insert c;
        User u = [select id from user where Profile.Name = 'PA Customer Sales and Support' and IsActive = true  limit 1];
        System.runAs(u) {
              deleteContact(c.Id);
        }
        
        //Make sure the contact was deleted
        Contact[] testContact = [select Id from Contact where Id = :c.Id];
        System.assert(testContact.isEmpty());
    }
    
    public class deleteException extends Exception {}
}

 

Starz26Starz26

Try adding.

 

@isTest(SeeAllData=true)

 

Starting in Sprin 12, test methods do not have access to existing data in the org without this notation. This is desigend to "help you write better unit testing" by ensuring you create all records during testing and not relying on existing data. Access to the User object etc, it still available without the notation.

 

daniel_hdaniel_h

Thanks for the idea. Unfortunately, I did try that and it didn't make a difference.

hhkwonghhkwong

Hi, I'm on the team at Salesforce responsible for this feature. This is a bug - normally we filter out UserRecordAccess results for records the running user (in this case the user with profile PA Customer Sales and Support) cannot read, so we don't expose too much information, but in this case the running user is querying for his own access on a record, and he should be able to see the result. Thanks for bringing this up, and we will try to have it fixed soon.

(I am not sure what your sharing settings you have for Contacts, but I am guessing that the User u that you are running the UserRecordAccess query as does not have read access to the test Contact.)

daniel_hdaniel_h

Thanks for the answer. I'm really excited about this new feature. Much better than catching a DML exception. Do you have any suggested work arounds?

hhkwonghhkwong

For now you can store the query result in a UserRecordAccess list/array instead of a single SObject, and then check the list size. If the list is empty, then you know that the running user does not have read access, and therefore not edit access either. Otherwise, you can get the first element of the list and check for the HasEditAccess field as before.

This is definitely less than ideal, so we'll try to fix this soon!