Thursday, February 8, 2024

Log off user

Auto log-off users who have been logged in for more than 8 hours in Dynamics 365 FO


Auto log off users


public void autoUserLogOff()

{

SecurityUserRole securityUserRole;

SecurityRole securityRole;

container usersList;

SysClientSessions sysUserSession;

utcDateTime dateTime, dateTimeLocal;

TimeOfDay totalHours;

SysUsersTerminate usersTerminate;

SysUserInfo userInfo;

str timeValue;


#define.AOTName(‘-SYSADMIN-‘)

#define.Admin(‘Admin’)


dateTime = DateTimeUtil::newDateTime(systemDateGet(), timeNow());


while select Id from userInfo

where userInfo.Id != #Admin

{

select firstOnly RecId from securityUserRole where securityUserRole.User == userInfo.Id

exists join securityRole where securityRole.RecId == securityUserRole.SecurityRole

&& securityRole.AotName == #AOTName;


if (securityUserRole.RecId)

continue;


while select * from sysUserSession

where sysUserSession.userId == userInfo.Id

&& sysUserSession.sessionType == SessionType::GUI

{

dateTimeLocal = DateTimeUtil::applyTimeZoneOffset(sysUserSession.LoginDateTime, DateTimeUtil::getUserPreferredTimeZone());


totalHours = int642int(DateTimeUtil::getDifference(dateTime, dateTimeLocal));


timeValue = conPeek(str2con(time2StrHM(totalHours), “:”),1);


if( str2int(timeValue) >= 8)

{

usersList += [[sysUserSession.userId, sysUserSession.SessionId, sysUserSession.LoginDateTime]];

}

}

}


if (conLen(usersList) > 1)

{

usersTerminate = SysUsersTerminate::newUsersList(usersList);

usersTerminate.run();

}

}


Autouser log off user x++


I hope this blog of DynamicsCommunity101 helped you learn Auto log-off users who have been logged in for more than 8 hours in Dynamics 365 FO