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
Tester GirlTester Girl 

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

Best Answer chosen by Tester Girl
Fatima 1Fatima 1
It is happening because standard validation occurs first.
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

Hargobind_SinghHargobind_Singh
Hello Tester Girl

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. 
Tester GirlTester Girl
This is my visualforce page

<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

 
Fatima 1Fatima 1
Hey Tester,
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
 
Tester GirlTester Girl
Hi,

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.
Tester GirlTester Girl
The profile of the user does have read/edit permissions but owd is set to private with sharing rules providing accesslevel like readonly,read/write etc to different groups of users.
Fatima 1Fatima 1
It is happening because standard validation occurs first.
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.

 
This was selected as the best answer
Tester GirlTester Girl
That should work,thanks