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

Displaying only records owned by the current user

I need assistence on how to select only records owned by the current user on a visualforce page.  I have tried  $User and $User.Id to no avail.  Please assist.


Here is my controller.

public class retrieveASNAccounts { public List<Account> getAccounts() { return [SELECT Name, First_ASN_Date__c FROM Account WHERE Account.OwnerId != null AND First_ASN_Date__c != null order by First_ASN_Date__c DESC limit 10]; } }


Here is my page.

<apex:page controller="retrieveASNAccounts"> <apex:pageBlock > <apex:pageBlockTable value="{!accounts}" var="a"> <apex:column value="{!}"/> <apex:column dir="" value="{!a.First_ASN_Date__c}"/> </apex:pageBlockTable> </apex:pageBlock> </apex:page>



Thank you!
Search the apex language reference for "Userinfo methods".
Edwin VijayEdwin Vijay

Use WITH SHARING keyword...


public class with sharing classname{


your query;





Now your query returns only records owned by the current user...




Add condition with Query  Account.ownerId =: userInfo.getUserId() ;





That worked perfectly!  Thanks for all the responses:smileyhappy: