+ Start a Discussion
HayaHaya 

How do I use =!$User

How do I use the $User field in an if statement?

Something like:

case1=[Select Priority,CaseNumber,Accountid,Subject,Contactid,CreatedDate,Ownerid from case where Ownerid=!$User];  
     return case1;

 

which of course is not how to do it.

 

Thanks,

Haya

Best Answer chosen by Admin (Salesforce Developers) 
swatKatswatKat

The query should be like this :

 

case1=[Select Priority,CaseNumber,Accountid,Subject,Contactid,CreatedDate,Ownerid from case where Ownerid=:UserInfo.getUserId()]; 

 In your apex controller, You have to use UserInfo methods like UserInfo.getUserId() to get the user details. For reference look at this : http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_userinfo.htm

All Answers

swatKatswatKat

The query should be like this :

 

case1=[Select Priority,CaseNumber,Accountid,Subject,Contactid,CreatedDate,Ownerid from case where Ownerid=:UserInfo.getUserId()]; 

 In your apex controller, You have to use UserInfo methods like UserInfo.getUserId() to get the user details. For reference look at this : http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_userinfo.htm

This was selected as the best answer
Avidev9Avidev9
Will Suggest you to read the documents carefully.

The one you are referring is Global variable for VF pages and Formulas http://www.salesforce.com/us/developer/docs/pages/Content/pages_variables_global.htm

Also keep in mind Merge fields are only for VF pages, formulas, buttons etc not for Apex

What you actually need is access through apex, Have a look at userInfo class http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_userinfo.htm
HayaHaya

Thank you very much, Avi.

I will read the documents.

HayaHaya

This worked.

Thank you so much, swatKat