[SalesForce] How to get remaining session time in Apex for current user

I have 2 questions:

  1. I know we can configure session timeout, but how can we get the configured session timeout value in Apex code?

  2. Does Salesforce provide helper function to calculate the remaining session time for current logged in user?

For example, the session timeout is set to be 2 hours, now the remaining session time should be 0.5 hour for a user logged in 1.5 hours ago

To calculate the remaining session time in Apex class, I can query LastLoginDatetime for current user, then calculate expireDate = currentDatetime + (the remaining session time – LastLoginDatetime)

Just want to check if there is more elegant solution.

Best Answer

I know we can configure session timeout, but how can we get the configured session timeout value in Apex code?

String sessionLength = Auth.SessionManagement.getCurrentSession().get('NumSecondsValid');

Does Salesforce provide helper function to calculate the remaining session time for current logged in user?

No.

For example, the session timeout is set to be 2 hours, now the remaining session time should be 0.5 hour for a user logged in 1.5 hours ago

No, that is not correct. Because the code was called after halfway through the session time, the timer is reset back to two hours. As long as you're using your session, it does not time out.

To calculate the remaining session time in Apex class, I can query LastLoginDatetime for current user, then calculate expireDate = currentDatetime + (the remaining session time - LastLoginDatetime)

That's not correct (as per above). The session may have been created days ago, so long as it was in use the whole time, it's still active.

As it says in the documentation:

LastModifiedDate ... A session expires when the current date and time equals LastModifiedDate + NumSecondsValid. This field is a standard system field.

Conceptually, you could get the LastModifiedDate and add the NumSecondsValid from the getCurrentSession() map from above. Note that the date format isn't a typical format you'd see in Salesforce, so you'll have to "manually" parse it.