While working as a Salesforce developer at Texas Customer Management Company. I was tasked with retrieving the user’s session ID using the Salesforce tool.
And as a solution to this, I found that Salesforce provides us with the function named “GETSESSIONID”.
In this tutorial, we will learn how to use the GETSESSIONID() Function in Salesforce. We will also learn the function’s syntax, examples, and some real-world business scenarios.
If you want to learn how to find a user’s session ID using the Salesforce GETSESSIONID function, with examples, go through this complete article.
What is the GETSESSIONID() Function in Salesforce?
The Salesforce GETSESSIONID() Function is one of the available TEXT() Functions, used for various text operations on fields. In general, the GETSESSIONID() function returns the user’s Salesforce session ID.
In addition, this may be useful for hyperlinks to other applications that use Salesforce.com credentials for authentication. And the following is the syntax of the Salesforce GETSESSIONID function:
GETSESSIONID()This function does not take any parameters. If you pass any parameter to the function, you will get an error “Error: Incorrect number of parameters for function ‘GETSESSIONID()’. Expected 0, received”.
Moreover, the GETSESSIONID function always returns the output in text format. If you set any other output data type, you will get the “Error: Formula result is data type (Text), incompatible with expected data type”.
With this, we have learned the syntax of the GETSESSIONID function. Now, we will learn how to use the GETSESSIONID function to retrieve the session ID in Salesforce.
How to Get Session ID in Salesforce Lightning
Here, you will learn how to use the GETSESSION function in Salesforce Lightning to get the session ID of the user.
- Go to the Gear Icon. -> Click on the Setup. -> Select the Object Manager tab. Select the Object on which you want to create the Formula Field. Here, I’m going to create an Opportunity Object.
- In the Opportunity setup, click on Fields and Relationships to create a new field, then click the New button.

- In this step, we will use a formula to calculate and display the date after adding days to the given date, and display it on the product object. To do this, select the Formula option from the data type and click Next.

- The choose output type screen appears after clicking the Next button. Enter the information now by filling out the “Field Label” and the “Field Name”, which is automatically filled in when you put the cursor on the field name. In this instance, I typed “User Session ID” into the field label.
- Choose the “Output Data Type“ for the field in which we want to store the result. In this case, I check the radio button next to the “Text” data type to return the formula result as text.
- Move to the next step by clicking the “Next” button.

- Go to the advanced formula editor and enter the GETSESSIONID () function formula there. In my case, I only need to get the user session ID; therefore, the formula is as follows:
GETSESSIONID()- Here, we use the GETSESSIONID() function of Salesforce to get the user’s ID.
- After entering the formula, you can also check whether the formula is correct or not by clicking on the “Check Syntax” button.
- And you can also add the “Description“, “Help Text“, and “Handle the empty field”. Now click the “Next” button to proceed to the next step.

- In this step, establish “Field-Level Security“. Choose the profiles to which we wish to grant field-level security edit access to this field. If field-level security is not added, the field will be hidden from all profiles.
- As I want it to be visible to all profiles, I have checked the “Visible” checkbox in this instance. After that, click the “Next” button located at the top.

- Add the custom field to the “Page Layout“ after setting up the field-level security. Pick the page layout that has this field in it. If we do not select a layout, the field will not appear on any pages.
- To save the formula field, click the “Save” button.

Once the formula field is created, you can use the formula field. Let’s see an example:
- Create a new contact and save the contact details.
- After saving it, move to the Details section; there, you will find the User Session ID field, which shows the result of the GETSESSIONID function.

With this, we have learned how to use the GETSESSIONID function to get the session id of the user in Salesforce Lightning.
Conclusion
In a nutshell, we have learned that the GETSESSIONID function in Salesforce is a great way to retrieve a user’s session ID. In addition, we have learned the syntax and various real-world examples of the GETSESSIONID function.
Furthermore, we have covered the steps for using the GETSESSIONID function in Salesforce to retrieve the user’s session ID.
You may like to read the following articles:
- FIND() Function in Salesforce
- CONTAINS() Function in Salesforce
- TRUNC() Function in Salesforce
- ATAN2() Function in Salesforce
I am Bijay Kumar, the founder of SalesforceFAQs.com. Having over 10 years of experience working in salesforce technologies for clients across the world (Canada, Australia, United States, United Kingdom, New Zealand, etc.). I am a certified salesforce administrator and expert with experience in developing salesforce applications and projects. My goal is to make it easy for people to learn and use salesforce technologies by providing simple and easy-to-understand solutions. Check out the complete profile on About us.