Skip to content

Instantly share code, notes, and snippets.

@dsharrison
Last active December 13, 2015 20:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dsharrison/b0b03c661bf65fc18348 to your computer and use it in GitHub Desktop.
Save dsharrison/b0b03c661bf65fc18348 to your computer and use it in GitHub Desktop.
Controller and visualforce page to demonstrate missing records with UserRecordAccess query.
<apex:page controller="UserRecordAccessDebugCtrl" showHeader="true" sidebar="true">
<!--
Load this page with a record in the 'id parameter as users with varying record access
-->
<apex:form>
<h2>User</h2>
<br />
<apex:selectList value="{!userId}" size="1">
<apex:selectOptions value="{!userOptions}" />
<apex:actionSupport event="onchange" rerender="debug" />
</apex:selectList>
</apex:form>
<br />
<apex:outputPanel id="debug">
<h2>Access</h2>
<p>{!accessDebug}</p>
<h2>Record</h2>
<p>{!recordDebug}</p>
</apex:outputPanel>
</apex:page>
public without sharing class UserRecordAccessDebugCtrl {
private Id recordId {get;set;}
public Id userId {get;set;}
public List<SelectOption> userOptions {get;set;}
public UserRecordAccessDebugCtrl() {
recordId = ApexPages.currentPage().getParameters().get('id');
userOptions = new List<SelectOption>();
for(User u : [SELECT Name, Username, Profile.Name FROM User ORDER BY Name]) {
userOptions.add(new SelectOption(u.Id, u.Name + ' [' + Profile.Name + ']'));
}
userId = userInfo.getUserId();
}
public String getAccessDebug() {
return JSON.serialize(Database.query('SELECT RecordId, MaxAccessLevel FROM UserRecordAccess WHERE RecordId = :recordId AND UserId = :userId'));
}
public String getRecordDebug() {
return JSON.serialize(Database.query('SELECT Name, UserRecordAccess.HasEditAccess FROM Opportunity WHERE Id = :recordId'));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment