+ Start a Discussion

Record Level security on Custom VF page

Hello All,


I created one custom object with one picklist filed called Account.I have crated page to display this field and aslo created Save method to add the record.When save the record it gets display in page section lower part where I have display the picklist field. now I want apply record leve security to this page.ie whoever will login should see only his record.But whatever record user is adding that is vissible to all user. 


How to apply record leve security please guide mw if anyone have idea....



Thanks In Advance,


Best Answer chosen by Admin (Salesforce Developers) 
Change the query, put a where condition like select field from Account__c where  OwnerId = Current User Id(u will get it like {!$User.Id} something like this . check)

All Answers

I did not get what u r saying. post your code.

Thanks for your reply..


Code is Working fine bt still for your reference Here is my  code..


Page : 


<apex:page standardController="Accounts__c" id="thePage" extensions="AccountsController">
<script type="text/javascript">
        function Test() {
            var account= document.getElementById('{!$Component.thePage.frmAccounts.pb.pbs.account}').value;
          if(account == ''){
                alert('Please select the account');
                return false;   
                return true;

<apex:form id="frmAccounts">
<apex:sectionHeader title="Accounts"/>
<apex:pageBlock id="pb">

<apex:pageBlockSection columns="1" id="pbs">

 <apex:selectList size="1" value="{!Accounts__c.Accounts__c}" id="account">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 
      <b>Accounts</b> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      <apex:selectOptions value="{!Acc}"/>


<apex:pageBlockButtons location="bottom">
     <apex:commandButton value="Save" action="{!Save}" onclick="return Test();">


<apex:pageBlock id="pbD">

<apex:pageBlockTable value="{!Accounts}" var="a" id="pbt" align="center" >
        <apex:column id="c">
                <apex:facet name="header">Accounts</apex:facet>
        <apex:column >
                <apex:facet name="header">Action</apex:facet>
                <apex:commandLink value="Delete" action="{!deleteAccount}" >
                <apex:param name="id" value="{!a.id}"/>




Controller :



public class AccountsController {

private Accounts__c cntr; 
private final ApexPages.StandardController controller;
private  List<Accounts__c> account{ get; set; }
integer i;   

    public AccountsController(ApexPages.StandardController controller) {
    this.controller = controller;

    cntr= (Accounts__c )controller.getRecord();

public List<SelectOption> getAcc()
       List<SelectOption> options = new List<SelectOption>();
       options.add(new SelectOption('','Select'));      
       Schema.DescribeFieldResult fieldResult = Accounts__c.Accounts__c.getDescribe();
       List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
       for( Schema.PicklistEntry f : ple)
          i = [Select count() from Accounts__c where Accounts__c =:f.getValue()];
                     options.add(new SelectOption(f.getLabel(), f.getValue()));
       return options;


 public List<Accounts__c> getAccounts()
       account= [Select id, Name, Accounts__c from Accounts__c  ];
       return account;
    public PageReference deleteAccount()
        Accounts__c acc= [select id from Accounts__c where id=:ApexPages.currentPage().getParameters().get('id')];
        delete  acc;   
        return null;
    public PageReference Save()
       Accounts__c newAccount = new Accounts__c();
       newAccount.Accounts__c = cntr.Accounts__c;
       insert newAccount ;   
       return null;




 Now I have two users A & B.


If A adss any account(record) B sholud not see that record and vice versa.

But in my case When B acess the Account page he is able to see records of his own as well as A's records.

So my question is how to apply record level security so that A cant see B's record n vice versa.



Change the query, put a where condition like select field from Account__c where  OwnerId = Current User Id(u will get it like {!$User.Id} something like this . check)
This was selected as the best answer

Thanks again for your quick reply..


Do we have to insert owner Id while inserting the record or bydefault it will take the current userid

it is auto
Thnk you very much Srinivas it worked ...