How to Get Salesforce Session ID

So, you want to know how to get Salesforce session ID information? Most of Salesforce is ridiculously easy to use, unless you want to get into advanced programming and automation. It usually involves just going through some UI steps with wizards and forms, and keeping a clear head while doing so.

Almost never, with a few exceptions, is getting simple information involved or highly technical, which is what makes Salesforce truly great. Not only is it powerful and flexible, but it’s seldom any more complicated than you choose to make it be, at any given time.

So, it is with apprehension that I tell you that getting a session ID with apex is both the simplest task in Saleforce, but also more technical than anything else you’re apt to have to do unless you yourself are an advanced developer for this platform.

It involves using some apex code, and understanding what a couple elements of programming are. It’s not difficult, but if you’re not a programmer, prepare to learn about variables, strings, classes, members and assignment, as well as line termination.

Before I explain these, let’s look at the piece of code itself:

String MySessionID = UserInfo.getSessionID();

Nearly anyone who’s worked with nearly any programming or scripting language will instantly understand this line of code, and realize that from here, it’s just a matter of setting a text label, text field or other text-recipient control to the value of MySessionID through similar code.

For those unfamiliar, here we go! First, a string is a line of text. This line of text can be empty, contain multiple of only one character, or a whole array of characters, even the contents of this article. By saying “String MySessionID”, we create a non-rigid name, “MySessionID” which represents an unspecified text value. By joining this with “=”, we are assigning the otherwise unspecified text value this name represents, to the right side of the operator.

So, “UserInfo” is a generic class, or a named item containing its own code and variables. By attaching “.getSessionID()”, we’re asking this named piece of code to execute a function by this name, which requires no information given (which would appear in the parentheses). This line of code returns data as a variable, which then becomes the text value of our string, “MySessionID”. The semicolon simply means this is the end of that line of code.

This is all there is to how to get Salesforce session ID information. From here, it’s just assigning something to display it, if displaying is your intention, or logging it, etc.

Amanda McDonald
Amanda is the Lead Author & Editor of Rainforce Blog. Amanda established the Rainforce blog to create a source for news and discussion about some of the issues, challenges, news, and ideas relating to Salesforce usage.
Amanda McDonald on sabtwitterAmanda McDonald on sabgoogleAmanda McDonald on sabfacebook