You need to sign in to do that
Don't have an account?

Visualforce page with extension to check user access to record otherwise redirect
Hi All
I am new to visualforce development and would like your help.
I have to create a visualforce page with extension for a custom object in a managed package.The custom object is called Attendance and all users have access to its records and when users drill down to the related Student link for a record they don't own,an error is displayed:“Data Not Available: The data you were trying to access could not be found. It may be due to another user deleting the data or a system error. If you know the data is not deleted but cannot access it, please look at our support page.”
So I want to create a page with extension that checks if the user has access to record due to security.If yes then redirect to correct record page,otherwise show error'User doesnt own record so cannot access it'.
Thanks for any help
Do one thing, create another custom button, label it as "Edit" and drop this button in place of standard Edit button and remove standard button from layout.
Hope this helps.
All Answers
You can find out whether a user has access to record using Apex. Here is a related article:
http://salesforce.stackexchange.com/questions/175/can-i-find-out-if-the-current-user-has-access-to-a-record-without-querying
=========
ps: If your problem/question is resolved/answered, please mark your post as 'Solved' so that the community can benefit with resolution of issues.
<apex:page standardcontroller="Attendance__c" extensions="checkAcessToRelatedRecords" action="{!redirectToPage}">
</apex:page>
This is my extension class
public class checkAcessToRelatedRecords {
public Attendance__c f{ get; set; }
public id RecordId { get; set; }
public checkAcessToRelatedRecords(ApexPages.StandardController controller) {
RecordId = Controller.getrecord().Id;
f=[SELECT Id, Name, UserRecordAccess.HasReadAccess, UserRecordAccess.HasEditAccess, UserRecordAccess.MaxAccessLevel
FROM attendance__c where id=:recordId];
}
public PageReference redirectTopage() {
// Check if the user has access on application
if (f.UserRecordAccess.HasEditAccess==true){
PageReference p=new pageReference('/apex/AttendanceEdit?Id='+recordid);
p.getParameters().put('nooverride','1');
return p;
}
else{
PageReference pg=new PageReference('/apex/ErrorDisplayForAttendance');
return pg;
}
}
}
VISUALFORCE PAGE ErrorDisplayForAttendance
<apex:page >
<h1>
This record is owned by another User and is not available to view
</h1>
</apex:page>
I have replaced the Edit page button with my Visualforce page. if the user has access,it is redirecting to the original visualforce page attendanceedit. However in case of no-access,still the system-error is being displayed and not my custom visualforce page errorDisplayfor Attendance.Please let me know where I am going wrong.Thanks
I am not following you fully.
Seems like main page is working fine and now you need the same behavior on Edit button. Correct?
If so, I am little confused that if user does not have Edit rights on profile, how does he see Edit button on record?
Can you please elaborate a little more.
Regards,
Fatima
He can see view button and edit button but if he doesnot own or has access to the attendance record,he is redirected to a system error message.I want him to be re-directed to the custom visual force page I made when he clicks View or Edit,but that is not happening.He is still redirecting to the system error message.The edit button is part of a managed package viusalforce page so I don't really know why it is still visible if he has no right on.
Do one thing, create another custom button, label it as "Edit" and drop this button in place of standard Edit button and remove standard button from layout.
Hope this helps.