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.
Userobject are you expecting to cause your trigger to run? Also theAuthSessionobject 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 whetherAuthSessionever has any rows. See e.g. How do I start to debug my own Apex code?. – Keith C Jun 26 '14 at 08:21