+ Start a Discussion

Override Edit button with Visualforce - How to manage record sharing

I've overridden the edit button with a redirect link that directs users to a visualforce edit page.  Using this link below:


<apex:page standardController="Guest_Card__c" showheader="false" sidebar="false">

<meta http-equiv="REFRESH" content="0;url=../apex/editguestcard?id={!Guest_Card__c.Id}&mode=edit&returl=%2F{!Guest_Card__c.Id}" />



The custom edit page has a standard controller with sharing.  So, when a user tries to edit a record they do not have access too, it will not let them save.  How can I make it so the user doesn't even get redirected to the edit page as it would be confusing for them to allow them to fill the page out, but not let them save.  


Is there a way to detect the permissions of the user on the redirect page, and if they have permission to edit the record to direct them to the edit page, but if not, direct them to another page that displays an error?



Thanks for any suggestions!


Try this...

public PageRegerence validatePermission() {
	PageReference pr;
	SavePoint sp = DataBase.setSavepoint();
	try {
		// assuming selectedGuestCardId contains the ID of the record the user is trying to edit
		update [select Id from Guest_Card__c where Id = :selectedGuestCardId]; 
	catch(Exception e) {
		// Exception means that update to the record failed
		pr = Page.NoAccess;
	// Rollback, b/c the update was only to validate if the user can update the record
	return pr;


Call the validatePermission() method from the action attribute of the <apex:page /> tag.  If the user does not have the permission to update the record, they will be directed to NoAccess page.