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
smagee13smagee13 

How do I populate a VisualFoce page with current system user

I have a VisualFoce page that I've created to perform a quick case add function.  This page is called from a custom object, via button, called Projects__c.  What I would like to do is save my new cases with the userID of the user who is logged in.

 

Can I use the UserInfo.getUserId() method directly in VF or do I need a controller extension to do this?  If so, how do I return the value from the class to the VF page?

 

Thanks

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Ron HessRon Hess

$User

Description A global merge field type to use when referencing information about the current user. User merge fields can reference information about the user such as alias, title, and ID.
Use Use dot notation to access the current user's information. For example:
{!IF (CONTAINS($User.Alias, Smith) True, False)}
Example The following example displays the current user's company name, as well as the status of the current user (which returns a Boolean value.)
<apex:page>
<h1>Congratulations</h1>
This is your new Page
<p>The current company name for this
user is: {!$User.CompanyName}</p>
</apex:page>

All Answers

Ron HessRon Hess

$User

Description A global merge field type to use when referencing information about the current user. User merge fields can reference information about the user such as alias, title, and ID.
Use Use dot notation to access the current user's information. For example:
{!IF (CONTAINS($User.Alias, Smith) True, False)}
Example The following example displays the current user's company name, as well as the status of the current user (which returns a Boolean value.)
<apex:page>
<h1>Congratulations</h1>
This is your new Page
<p>The current company name for this
user is: {!$User.CompanyName}</p>
</apex:page>
This was selected as the best answer
smagee13smagee13

Thanks for the pointer but I still seem to be stuck.  I believe that it is because Im using the Case standard controller in my VisualForce page to create a new case.  All I want to do is grab the current user and use that value in the "Case Owner" field when I save the record.

 

Any ideas?

 

<apex:page standardController="case" showHeader="false" sidebar="false"> <STYLE type="text/css"> .labelCol { text-align: right } .dataCol { text-align: left } </STYLE> <apex:form > <apex:pageBlock title="Quick Add New Project Case/Task"> <table> <tr> <td><apex:outputLabel styleclass="labelCol" value="Status" for="status" /></td> <td><apex:inputField styleclass="dataCol" value="{!case.status}" id="status" /></td> </tr> <tr> <td><apex:outputLabel styleclass="labelCol" value="Case Owner" for="co" /></td> <td><apex:inputField styleclass="dataCol" value="{!$User.ID}" id="co" /></td> </tr> <tr> <td><apex:outputLabel styleclass="labelCol" value="Client Owner" for="clio" /></td> <td><apex:inputField styleclass="dataCol" value="{!case.contactID}" id="clio" /></td> </tr> <!-- add internal owner --> <tr> <td><apex:outputLabel styleclass="labelCol" value="Due Date" for="dd" /></td> <td><apex:inputField styleclass="dataCol" value="{!case.Date_Du__c}" id="dd" /></td> </tr> <tr> <td><apex:outputLabel styleclass="labelCol" value="Internal Due Date" for="idd" /></td> <td><apex:inputField styleclass="dataCol" value="{!case.Internal_Date_Due__c}" id="idd" /></td> </tr> <tr> <td><apex:outputLabel styleclass="labelCol" value="Project" for="proj" /></td> <td><apex:inputField styleclass="dataCol" value="{!case.Project__c}" id="proj" /></td> </tr> <tr> <td><apex:outputLabel styleclass="labelCol" value="Subject" for="subject" /></td> <td><apex:inputField styleclass="dataCol" value="{!case.subject}" id="subject" style="width:400px" /></td> </tr> <tr> <td><apex:outputLabel styleclass="labelCol" value="Description" for="desc" /></td> <td><apex:inputField styleclass="dataCol" value="{!case.description}" id="desc" style="width:800px; height: 100px" /></td> </tr> </table> <apex:commandButton action="{!Save}" value="Save" rerender="AJAXTest" status="AJAXStatus" /> <apex:actionStatus startText="(Saving...)" stopText="" onStop="window.top.close();" ID="AJAXStatus" /> <!-- <apex:commandButton action="{!save}" value="Save Case" />--> </apex:pageBlock> </apex:form> </apex:page>

 

 

 

Edwin VijayEdwin Vijay

Exactly.. You have to use an extension to achieve your bit...

 

When you click on the save button call a method in the extension class... 

 

Use Userinfo().getuserId() and assign it to ownerId...

 

 

 

Hope this helps.......