How to get salesforce session id in apex

A typical use case for salesforce developers is to find the session id to invoke the salesforce platform api's

In this article, we will cover how to get session id from apex and utilize them in connecting to your salesforce org.

What is session id in apex


The session id serves as a crucial authentication token in Salesforce, granting access to resources and APIs on behalf of a user. The session id is generated by the platform when a user successfully authenticate with the platform.

By design, they are temporary and expire after a set period of inactivity.

How to get session id in apex debug log or from visual studio code


There are various ways you can get the session ID in Apex, but it cannot be directly printed in a debug log. To print the session ID in a debug log, you will need to use a string method. You may use one of the following ways to get the session ID from your Salesforce org.

1. Get session id by using userInfo class

The userInfo class in Apex provides a method getSessionId(), allowing for the retrieval of the session ID in Apex. Due to its sensitive nature, the session ID typically cannot be printed in debug logs.

In order to put the session id in debug logs, apex string methods can be used.

String sessionId = userInfo.getSessionId();

//You will see a message 'SESSION_ID REMOVED'
System.debug('Session id '+sessionId); 

//You will see the session id
System.debug('Session id '+UserInfo.getOrganizationId() + UserInfo.getSessionId().substring(15));
Creating a lwc component in visual studio code video

2. Get access token from visual studio code

Access token is another way to get access to salesforce org and it behaves the same way as session id. The access token can be obtained via connected app.

The visual studio code connects to salesforce org via connected app and generates access token. This accesss token can be used instead of session id when connecting to your org.

To obtain access token from visual studio code via terminal, execute the following command.

//Using sf org command
sf org:display -o <your-org-name> --json

//example
sf org:display -o devorg --json

//Using sfdx command
sfdx org:display -o devorg --json

Session ID Removed Message

The "Session ID Removed" message in Salesforce Apex means that Salesforce automatically removes Session IDs from debug logs to protect sensitive information. This helps prevent accidental exposure of Session IDs, which are authentication tokens.

If you want to temporary get session id, use one of the above methods:

1. Get session id by using userInfo class

2. Get access token from visual studio code

What is Decodeforce?

Decodeforce is a dedicated platform aimed at helping Salesforce developers improve their Apex coding skills by solving real-world programming challenges

Solve a number of practice scenarios in DML, SOQL, trigger and many more on Decodeforce.

String

Learn to use the apex string data type to validate and manipulate string variables in salesforce.

Solve
Date

Play with Apex date methods to gain hands-on experience in manipulating date data type in Salesforce.

Solve
List

Practice working with Apex lists to enhance your Salesforce development skills.

Solve
Map

Practice working with apex Maps to handle large and complex data for business requirements.

Solve
Practice apex collection control flow statements
Collections & Control flow

Discover how to utilize various conditional statements and collection types in Apex..

Solve
Practice apex SOQL and DML statements
SOQL & DML

Explore SOQL queries, database manipulation statements (DML), and how to retrieve and update records in Apex.

Solve
Practice apex triggers
Apex Triggers

Master apex triggers by leveraging trigger events and context variables to address real business scenarios.

Solve
Practice apex governor limits
Governor limits

Resolve apex code that hits the governor limits and discover techniques to prevent such occurrences in your projects.

Solve
Practice apex REST API
Apex REST API

Get hands-on experience with apex REST APIs to connect with external systems to send /receive data and updates records in Salesforce

Coming soon!
Lightning Web Components
Lightning Web Components

Develop and get hands on experience in creating Lightning Web Components to create modern user interface for salesforce apps.

Solve

To know more click the Get started button below