1

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.

2 Answers 2

4

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.

0
5

If you happen across the documentation on the AuthSession object, you'll get a helpful hint (emphasis mine):

LastModifiedDate

Type
dateTime

Properties
Defaulted on create, Filter, Sort

Description
The date and time this session was last updated. A session expires when the current date and time equals LastModifiedDate + NumSecondsValid. This field is a standard system field.

So, here is a sample script you could run in Execute Anonymous and adapt to your needs:

static Long getRemainingSecondsInSession()
{
    return getRemainingSecondsInSession(UserInfo.getUserId());
}
static Long getRemainingSecondsInSession(Id userId)
{
    AuthSession session = [
        SELECT LastModifiedDate, NumSecondsValid
        FROM AuthSession
        WHERE UsersId = :userId
        ORDER BY LastModifiedDate DESC LIMIT 1
    ];

    // make sure both Datetime values are in same time zone
    Datetime now = Datetime.now();
    now = Datetime.newInstance(now.date(), now.time());

    Long elapsed = (now.getTime() - session.LastModifiedDate.getTime()) / 1000;
    return session.NumSecondsValid - elapsed;
}

system.debug(getRemainingSecondsInSession());
1
  • Of course calling the method updates the AuthSession, as mentioned in the other answer. Still interesting to find out how far along you were in the session, perhaps.
    – Adrian Larson
    Commented Nov 27, 2018 at 4:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .