Salesforce | How to get the User Informations like ORG_ID, User_ID etc in VisualForce code

Author - Webner

To get the active user info or org info in Visualforce code one of the best programming practice is to get the values from the controller class of the Component.

Here is sample controller class:

public with sharing class GetUserInfo
{
    // getters and setters for the variables
       Public string orgID {get;set;}
       Public string userID {get;set;}
       Public string s3path {get;set;}

    public getUserInfo()
    {
        orgID = UserInfo.getOrganizationId() ;    // getting the current org_id
        userID =UserInfo.getUserId();                  // getting the current user_id
        s3path ='salesforcedata/'+orgID+'/'+userID;  // A custom variable to be passed to external Api
        //system.debug('orgID ==== ???  '+s3path);
    }
}

Our VisualForce Component:

<apex:component Controller="GetUserInfo" allowDML="true" rendered="true">

// We need to access the s3path  here within our script
// we need to use the JSENCODE function to access the variable from the controller class
<script>
var orgId = '{!JSENCODE(s3path)}';
alert("orgid is == " + orgId);
</script>
</apex:component>

Webner Solutions is a Software Development company focused on developing CRM apps (Salesforce, Zoho), LMS Apps (Moodle/Totara), Websites and Mobile apps. If you need Salesforce customization, App development or any other software development assistance please contact us at salesforce@webners.com

Webner Solutions is a Software Development company focused on developing Insurance Agency Management Systems, Learning Management Systems and Salesforce apps. Contact us at dev@webners.com for your Insurance, eLearning and Salesforce applications.

Leave a Reply

Your email address will not be published. Required fields are marked *