function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Pratyush ChalasaniPratyush Chalasani 

Calling Auth.SessionManagement.getCurrentSession() throwing "System.UnexpectedException: Salesforce System Error"

Hi All,

I'm trying to get the properties of the current session in a trigger on the Contact object by calling:
Map<String,String> sessionAttributes  = Auth.SessionManagement.getCurrentSession();

This works fine when I'm logged in with the System Administrator profile.
But, when I try to log in and run as a Salesforce Platform user it throws a System.UnexpectedException.
This is the debug log showing the error (the code on line 9 of the trigger is the call to getCurrentSession shown above):
26.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO
11:56:43.306 (306104471)|EXECUTION_STARTED
11:56:43.306 (306155227)|CODE_UNIT_STARTED|[EXTERNAL]|TRIGGERS
11:56:43.306 (306192631)|CODE_UNIT_STARTED|[EXTERNAL]|01q90000000BEmh|OnePlace.trContactBF on Contact trigger event BeforeInsert for [new]
11:56:43.307 (307728017)|SYSTEM_METHOD_ENTRY|[7]|system.Test.isRunningTest()
11:56:43.307 (307815791)|SYSTEM_METHOD_EXIT|[7]|system.Test.isRunningTest()
11:56:43.316 (316249510)|SYSTEM_METHOD_ENTRY|[9]|Auth.SessionManagement.getCurrentSession()
11:56:43.339 (339522821)|SYSTEM_METHOD_EXIT|[9]|Auth.SessionManagement.getCurrentSession()
11:56:43.339 (339710389)|FATAL_ERROR|System.UnexpectedException: Salesforce System Error: 1915389216-15189 (-1293247755) (-1293247755)

(System Code)
********************: line 9, column 1
11:56:43.339 (339734369)|FATAL_ERROR|System.UnexpectedException: Salesforce System Error: 1915389216-15189 (-1293247755) (-1293247755)

(System Code)
********************: line 9, column 1
11:56:43.174 (339797975)|CUMULATIVE_LIMIT_USAGE
11:56:43.174|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 0 out of 100
  Number of query rows: 0 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Maximum CPU time: 0 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 100
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 50
  Number of queueable jobs added to the queue: 0 out of 50
  Number of Mobile Apex push calls: 0 out of 10

11:56:43.174|CUMULATIVE_LIMIT_USAGE_END

11:56:43.339 (339878387)|CODE_UNIT_FINISHED|******************** on Contact trigger event BeforeInsert for [new]

Any ideas as to what's going wrong?
Subramani_SFDCSubramani_SFDC
Problem maybe the cause of API permissions reg platform user
Pratyush ChalasaniPratyush Chalasani

I already checked that the API Enabled permission is set for the user.

Unless I'm mistaken, I'm also fairly sure that it only applies for making calls to the SOAP/REST/Metadata/etc APIs, and not for calling regular Apex code.

I'm wondering if there's some other permission that I'm not aware of (Google isn't helping me much with this error).

Marco Sozzi 5Marco Sozzi 5
Hi,
I have exactly the same problem. When i'm "LOGGED IN AS" i have the error above when i call Auth.SessionManagement.getCurrentSession().
When I try to login normally it works (with different users with different profiles). I tried also to "login as" a user with SysAdmin profile and it doesn't work, so i don't think it is a permission problem.
Did you find a solution?

Thank you.
Marco.
Pratyush ChalasaniPratyush Chalasani
Hi Marco,

Yes, I found a workaround by doing this:
Map<String, String> sessionAttributes;

System.runAs(new User(Id = UserInfo.getUserId())) {
    sessionAttributes = Auth.SessionManagement.getCurrentSession();
}

As you've discovered, this problem only seems to come up when you first login as an admin and then use the "login as" function.
My guess is that there's a bug in their platform where the session variable isn't get set properly when you do that.
Calling System.runAs with the current logged-in user passed as the argument seems to force it to initialise the session data properly.

Give that a try and see if it works for you too.

Marco Sozzi 5Marco Sozzi 5
Hi Pratyush,
I tried this workaround but unfortunately System.runAs() can only be used within a test method.

Regards.
Marco.
Pratyush ChalasaniPratyush Chalasani
Indeed you're right. I hadn't gotten around to testing it manually yet, even trying to catch the exception doesn't work. I suppose I'll have to submit a bug report.