0
trigger AvailabilityStatusUpdate on User (before Update) {    
    DateTime lastModifiedDate;
    Integer noOfSecondsValid;

    for( User userObj : Trigger.New) {           
        LIST<AuthSession> session = [ SELECT LastModifiedDate, NumSecondsValid
                                     FROM AuthSession WHERE UsersId = : userObj.Id] ;    

        for(AuthSession sessionObj : session) {
            noOfSecondsValid = sessionObj.NumSecondsValid;
            lastModifiedDate = sessionObj.LastModifiedDate;
        }
        userObj.SessionExpiryTime__c = lastModifiedDate.addSeconds( noOfSecondsValid ); 

        if( userObj.SessionExpiryTime__c > System.now())
            userObj.AvailabilityStatus__c = 'Available';

        List<AggregateResult> loginHistoryObj = [SELECT MAX(LoginTime) FROM LoginHistory 
                                                 WHERE UserId = : userObj.Id GROUP BY UserId];

        DateTime loginDateTime = (DateTime)loginHistoryObj[0].get('expr0');
        Date loginDate = loginDatetime.date();
        if( logindate != (userObj.TimeNow__c).date())
            userObj.AvailabilityStatus__c = 'Absent';
    } 
}

I have used the above given piece of code to write a trigger. However, I want it to fire and update the USer Obj as and when the Session expiry Time changes. I am unable to fire it.

Firing the given query I get AvailabilityStatus and SessionExpiryTime blank.

SELECT username, id, AvailabilityStatus__c,CaseCounter__c, TimeNow__c, SessionExpiryTime__c from user

Please suggest.

Alap Mistry
  • 167
  • 1
  • 7
user2703132
  • 961
  • 3
  • 22
  • 42
  • 3
    A trigger only runs when a change to an object is made: what change to the User object are you expecting to cause your trigger to run? Also the AuthSession object seems to contain only live sessions which may break your logic. I recommend you add debug statements to see what is going on - e.g. to see if the trigger ever fires and if it does whether AuthSession ever has any rows. See e.g. How do I start to debug my own Apex code?. – Keith C Jun 26 '14 at 08:21
  • I have added a Formula field as well as a normal DateTime field with default value of now() so that an update of time fires up every second. But I have come to know that a Formula field cannot fire a trigger( Probably, because these are run time calculations and not the SML operations). – user2703132 Jun 26 '14 at 08:25
  • 1
    possible duplicate of http://salesforce.stackexchange.com/questions/10995/is-a-user-logged-in – AtulRajguru9 Jun 26 '14 at 08:38
  • @AtulRajguru9 Please check the content before marking a duplicate. Heading same may not mean the same question. – user2703132 Jun 26 '14 at 09:02
  • @user2703132 I agree with you, I have updated the subject to reflect the question which is in turn different to the marked duplicate. – Bartley Jun 26 '14 at 09:38

0 Answers0