Press ESC to close

How to output Salesforce session Id in Apex debug log?

You all know the method UserInfo.getSessionId() in Apex. You most probably have used it to make Webservice call. However, I find it difficult to output it so that I can use is externally.

If you would debug it, the debugger log will output:System.debug(UserInfo.getSessionId());
USER_DEBUG [1]|DEBUG|SESSION_ID_REMOVEDSystem.debug(‘——‘ + UserInfo.getSessionId() + ‘======’);
USER_DEBUG [1]|DEBUG|——SESSION_ID_REMOVED======

Even if you concatenate a string to it, you will always get the SESSION_ID_REMOVED.

However, after searching the internet, I found a solution which works:System.debug(UserInfo.getOrganizationId().substring(0, 15) + ‘ ‘ + UserInfo.getSessionId().substring(15));

Notice there is a space concatenating the organization id and the substring of the session Id. You need to remove it to make the Session Id valid.

Leave a Reply

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