• hhkwong
  • NEWBIE
  • 0 Points
  • Member since 2012

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 3
    Replies

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 {}
}

 

Is it possible to determine whether the current user has delete permissions (aka Full Access) for a particular record (custom object), without actually attempting to delete the record?

I'm creating some custom actions using VF/apex, and for some of them I want to limit who can perform the action to the same users who can delete the record.  The action does not involve deleting the record, but I want it to be more restrictive than who can perform an edit.

I looked into the sharing table (object_share) but that only lists the owner as Full Access.  Since anyone above the owner in the role hierarchy also has full access, and the hierarchy might be several levels tall, it seems like a tall order for my code to walk the entire hierarchy and determine if that applies.  Is there an easier way?

Thanks much!
  • August 04, 2008
  • Like
  • 0