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
Sandip458Sandip458 

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,

Sandip

Best Answer chosen by Admin (Salesforce Developers) 
Srinivas_V2Srinivas_V2
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

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

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;
        }
       
            </script>

<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; 
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      <b>Accounts</b> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      <apex:selectOptions value="{!Acc}"/>
     </apex:selectList>

</apex:pageBlockSection>

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

</apex:pageBlock>

<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>
                        {!a.Accounts__c}
         </apex:column>
        <apex:column >
                <apex:facet name="header">Action</apex:facet>
                <apex:commandLink value="Delete" action="{!deleteAccount}" >
                <apex:param name="id" value="{!a.id}"/>
                 </apex:commandLink>
         </apex:column>
  
   </apex:pageBlockTable>

</apex:pageBlock>
</apex:form>
</apex:page>

 

 

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()];
           if(i==0)
                   {
                     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.

 

 

Srinivas_V2Srinivas_V2
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
Sandip458Sandip458

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

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