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
CosCCosC 

how to get logged in user id in visualforce page ?

 
I need to show list of open cases for logged in  user so i used below query & it does not work.It does not show any cases. I am using Java Script & HTML5 .

 var sreas = UserInfo.getUserID();

SELECT Id, AccountId, CaseNumber, OwnerId, Reason, Status, Subject, Description, ContactId, Priority, Origin from Case WHERE OwnerId = \''+sreas+'\' Order by CaseNumber asc

Best Answer chosen by CosC
Nilesh Jagtap (NJ)Nilesh Jagtap (NJ)

You can make use of global variable to access user id in vf page as {!$User.Id}
For more information please go through https://www.salesforce.com/us/developer/docs/pages/Content/pages_variables_global_user.htm

If you are trying to access it in your JS function you can do somthing like
<apex:page controller="myController">
<script>
function myJSFunction(){
  alert('{!userid}');
  //your js code
 }


 /** Controller **/
 public with sharing class myController{
    public String userid{get;set;}
    public myController(){
         userid = Userinfo.getUserId();
    }
 
 //your code

 }



Thanks,
N.J

All Answers

ShashForceShashForce
You can perhaps try the merge field $user.Id in your visualforce page.
Nilesh Jagtap (NJ)Nilesh Jagtap (NJ)

You can make use of global variable to access user id in vf page as {!$User.Id}
For more information please go through https://www.salesforce.com/us/developer/docs/pages/Content/pages_variables_global_user.htm

If you are trying to access it in your JS function you can do somthing like
<apex:page controller="myController">
<script>
function myJSFunction(){
  alert('{!userid}');
  //your js code
 }


 /** Controller **/
 public with sharing class myController{
    public String userid{get;set;}
    public myController(){
         userid = Userinfo.getUserId();
    }
 
 //your code

 }



Thanks,
N.J
This was selected as the best answer
Arunkumar RArunkumar R
you can try the below query that might be helpful,

List<case> accList= [SELECT Id, AccountId, CaseNumber, OwnerId, Reason, Status, Subject, Description, ContactId, Priority, Origin from Case WHERE OwnerId =: Userinfo.getUserId()];
CosCCosC

Arun &  N.J I tried both the option it did not work. Below is my code after the change. Can you please let me know what is wrong in my query

Controller:
 public class CaseExtension
{
     public Boolean refreshPage { get; set; }
    public ApexPages.StandardController stdCtrl { get; set; }
 // added to get user id
  public String userid{get;set;}   

    public CaseExtension(ApexPages.StandardController std)     {
       stdCtrl=std;
        // initialize the refresh value as false
        refreshPage = false;
        userid = Userinfo.getUserId();
    }

    public PageReference save()

    {
        PageReference pRef = stdCtrl.save();
       
        if (pRef != null) {
            // set this value last, after successful saving to cause the VF page to show the javascript block
            refreshPage = true;
        }
          //refreshPage = true;
      
        return null;
   
    }

}

Visualforcepage & JavaScript code:

function getAllCase() {
              
                var sreas = {!$User.ID};
               alert('{!userid}');
                
Case.fetch('soql','SELECT Id, AccountId, CaseNumber, OwnerId, Reason, Status, Subject, Description, ContactId, Priority, Origin from Case WHERE OwnerId = \''+userid+'\'  Order by CaseNumber asc ',function() {
                    showCase(Case.data());
                });
            }

CosCCosC
I changed JS code to below & it works:

             var Case = new SObjectData();
            Case.errorHandler = displayError;
              $j(document).ready(function() {
                regBtnClickHandlers();
                getAllCase('{!userid}');
            });
           
            function getAllCase(sreas) {
              
                //var sreas = {!$User.ID};
                //alert();
                
                Case.fetch('soql','SELECT Id, AccountId, CaseNumber, OwnerId, Reason, Status, Subject, Description, ContactId, Priority, Origin from Case WHERE OwnerId = \''+sreas+'\'  Order by CaseNumber asc ',function() {
                    showCase(Case.data());
                });
            }