In this Salesforce tutorial, we will learn how to use the GETSESSIONID() Function in Salesforce. We will also learn the syntax and some real-world business scenarios of the function.
While working in the Texas Customer Management Company as a salesforce developer. I got a task to get the session id of the user using the Salesforce tool.
And as a solution to this, I found that Salesforce provides us the function named “GETSESSIONID”. So, if you want to learn how to find the session id of the user using the Salesforce GETSESSIONID function with examples go through this complete article.
What is Salesforce GETSESSIONID Function
Salesforce GETSESSIONID() Function is one of the available TEXT() Functions in Salesforce which is used for various text operations on the fields. In general GETSESSIONID() function, returns the user’s session ID in Salesforce.
In addition to this, this may be useful in hyperlinks to other applications that use salesforce.com credentials for authentication. And the following is the syntax of the Salesforce GETSESSIONID function:
This function does not take any parameter. 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 the 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 GETSESSIONID function with the syntax. Now, we will move and learn how to use the GETSESSIONID function to get session id in Salesforce Lightning.
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.
Step 1: Open the Salesforce account, click on the “Gear” icon located on the top right side of the page next to the bell icon, and from the dropdown click on “Setup” to move to the setup of the current app.
Step 2: After moving to the setup of the current app, click on “Object Manager” to get the list of all the objects in Salesforce.
Step 3: Now it’s time to search for the object from the list in which you want to use the GETSESSIONID function. So, here I searched for the “Contact” object and click on it.
Step 4: You are on the contact object page, move to the “Details Section” on the left side of the page then click on the “Fields & Relationship”. After that click on the “New” option available on the top right side to create a new formula field where you can easily use the GETSESSIONID function.
Step 5: Once you click the new button, you will get the list of the various fields type available in Salesforce select one form then as per need. Here, I choose the “Formula” field type by checking the radio button next to it as my need is to use the function in the field and this is only possible in the formula field type. Then, click on the “Next” button to move to the next page.
Step 6: In this step, you have to choose the output type of the field. Before choosing the output type of the field first enter the information such as “Field Label” and “Field Name”. In this example, I filled the field label as “User Session Id” and the field name is automatically filled up.
On scroll down, you have to choose the data type for the field in which you want to get the result. So, as we have learned above it always returns the output in the text type, so you have to check the radio button next to “Text” and click on the “Next” button.
Step 7: Now, enter the GETSESSIONID function in the advanced formula editor and create a formula using it according to the requirement. In my case I only need to get the user session id, therefore the formula is as follows:
- 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”, and “Help Text” as well as “Handle the empty field”. Now, click on the “Next” button to move to the next step.
Step 8: In this step, you have to set up the “Field-Level Security”. In this, you have to choose the profile to whom you want to grant field-level security edit access to this field.
In my case, I tick the checkbox adjacent to the “Visible” and click on the “Next” button to move to the next step.
Step 9: You have to add the custom field to the “Page Layouts”, for this, choose the page layout names in which you want to add this field. After this, click on 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 and here you will get the User Session Id field that 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. In the next section, we are going to discuss the steps of using the GETSESSSION function in Salesforce Classic.
How to get session id in Salesforce Classic
Here, you will learn how to use the GETSESSION function in Salesforce Classic to get the session id of the user.
Step 1: Log in to the Salesforce account in classic experience, if you are not logged in to the classic experience, click on the “Profile Avatar” at the right top side of the page, and click on the option “Switch to Salesforce Classic”.
Step 2: After successfully logging in to the classic experience, click on the “Setup” option given at the top in between your name and the help option.
Step 3: As you move to the classic experience setup, scroll down to the “Build” section on the left side of the page. Now from the build section, click on the “Customize” dropdown, and here you will get the list of all the objects in the Salesforce classic whether it will be standard or custom.
Choose the object in which you want to use the GETSESSIONID function of Salesforce. Let’s suppose you want to apply this function on the “Product” object, so click on it.
Step 4: As you click on the “Product” object, you will get various options related to the product object, but as you are going to use the GETSESSIONID function on the object, you have to click on the “Fields” option so that you can create a custom field where you use the function.
Once you are on the product field page, move to the section “Product Custom Fields & Relationships” and click on the “New” button to create the new custom field in the product object.
Step 5: Now, you will get the list of the various field types of Salesforce, choose the one from the list that you need. As the main objective is to use the GETSESSIONID function, so you need the formula field type for this. Click on the radio button next to the “Formula” field type and then click on the “Next” button to move to the next step.
Step 6: You are now on the “Choose output type” step. Here, you need to first enter the information related to the field that you are going to create. Enter the “Field Label” and “Field Name” for the field. In this instance, I entered the field label “Product Link with Session Id” and when I click on the field name box the field name will automatically populate.
Scroll down and choose the data type for the field in which you want to get the result of the field. In this example, I choose the text as the data type to get the result in the text format, by checking the radio button next to the “Text”. After that, click on the “Next” button to move to the next step.
Step 7: After clicking on the next button, you will be on the enter formula page. Move to the “Functions” section on the left side of the page and click on the “All Function Categories” dropdown and choose the function category that contains the necessary functions that you need for this formula. Here, I choose the “Text Category” and from the text categories functions list I search for the “GETSESSIONID” function, and “HYPERLINK” function and click on it.
Here, I define the formula that creates a link to an application that is outside of Salesforce which is as follows:
GETSESSIONID() & "?&rowID="&Name & "action=GetCourse","Get Python and Machine Learning Training Course")
- We use the “HYPERLINK” function to create the hyperlink and we pass the base URL “https://pythonguides.com/python-and-machine-learning-training-course/?sId=” of the webpage to it.
- After this, we pass the function “GETSESSIONID” to get the session id of the user. Basically, this session id is used to provide the authentication and maintain the user session on the website.
- Next, we use the “?&rowID=”&Name” parameter to pass the name value to the URL parameter.
- Then, we pass the “action=” parameter to the function and set its value to the “GetCourse”.
- After that, we pass the text “Get Python and Machine Learning Training Course” that we want to display as the text for the hyperlink.
Step 8: Click on the “Check Syntax” button to verify that the above-written formula is error-free. If your formula is error-free, you will get the message “No Syntax errors in merge fields or function”. But if there, is an error you will get an error message on the screen.
You can also enter the “Description” and “Help Text” as well as “Handle the empty field” if you want. Then, click on the “Next” button to move to the next step.
Step 9: You are on the “Establish field-level security” step. Here, you have to choose the profiles to whom you want to grant field-level security editing access. If you have not selected the profile this specific field will be hidden from the profile and the user with this profile is not able to edit this.
Here, I want this field to be visible on all the profiles, so I check the checkbox next to the “Visible” option and then click on the “Next” button to proceed to the next step.
Step 10: In this step, you have to select the page layouts that should include this field. This field will be added as the last field of these page layouts. But if you don’t select the page layouts, the field will not appear on any pages. When you have finished the process, click on the “Save” button.
Once, you have created the formula field, you can use it. Let’s see a demonstration to check how it works:
- Open the Product item, create a new product, and save it.
- Move to the Product Details, you will get the field named “Product Link with Session Id”. Now, click on the hyperlink given in the field, and you will successfully redirect to the external page with the user session id value in the URL.
With this, we have learned how to use the GETSESSIONID function to get the session id of the user in Salesforce Classic.
In a nutshell, we have learned that the Salesforce GETSESSIONID function is a great way to find the session id of the user. In addition, we have learned the syntax and various real-world examples of the GETSESSIONID function.
Furthermore, we have covered the steps of using the GETSESSIONID function of Salesforce to fetch out the session id of the user in Salesforce Lightning and Salesforce Classic.
You may like to read the following articles:
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.