AX7: Delete a User Session in the WHS Mobile Device Portal
One and a half year ago I posted an article which showed how to reset a user session in the Mobile Device Portal inside of the AX WHS module. Today I want to post an update on this issue and show you how to do that in AX 7 / Dynamics 365 for Finance and Operations.
Good news up front – there are only two rows to change!
TLDR: Delete the line realated to your WHSUserId in the WHSWorkUserSession table, to completly reset your user session.
Like last time, we create a new class which extends RunBase.
Declaration:
class WHSUserSession extends RunBase | |
{ | |
DialogField fieldWHSUserId; | |
} |
Methods:
Add a main method and get your dialog being prompted.
static void main(Args args) | |
{ | |
WHSUserSession userSession = new WHSUserSession(); | |
if (userSession.prompt()) | |
userSession.resetUserSession(); | |
} |
As we still want to select the WHSUserId we will override the lookup method, like we did it in AX 2012.
Here you can see the two changes that I did, to run the AX 2012 code in AX 7. First we have to align the access modifier, I just set it to public.
Next we want to reference a FormBuildStringControl instead of a FormStringControl, else we get a cast error.
public Object dialog() | |
{ | |
Dialog dialog; | |
FormBuildStringControl control; | |
dialog = super(); | |
dialog.caption("WHS: Reset user session"); | |
fieldWHSUserId = dialog.addField(extendedTypeStr(WHSUserId), "Select User"); | |
control = fieldWHSUserId.control(); | |
control.registerOverrideMethod(methodStr(FormStringControl, lookup), | |
methodStr(WHSUserSession, lookup), this); | |
return dialog; | |
} |
Now the lookup override, exactly the same like it was in AX 2012.
public void lookup(FormStringControl _control) | |
{ | |
Query query = new Query(); | |
QueryBuildDataSource queryBuildDataSource; | |
SysTableLookup sysTableLookup = SysTableLookup::newParameters(tableNum(WHSWorkUserSession), | |
_control); | |
sysTableLookup.addLookupField(fieldNum(WHSWorkUserSession, UserId)); | |
sysTableLookup.addLookupField(fieldNum(WHSWorkUserSession, InventLocationId)); | |
sysTableLookup.addLookupField(fieldNum(WHSWorkUserSession, LastActionDateTime)); | |
queryBuildDataSource = query.addDataSource(tableNum(WHSWorkUserSession)); | |
sysTableLookup.parmQuery(query); | |
sysTableLookup.performFormLookup(); | |
} |
And the actual reset, also unchanged.
private void resetUserSession() | |
{ | |
WHSWorkUserSession workUserSession; | |
if (fieldWHSUserId.value()) | |
{ | |
workUserSession = WHSWorkUserSession::find(fieldWHSUserId.value(), true); | |
if (workUserSession) | |
{ | |
try | |
{ | |
ttsBegin; | |
if (workUserSession.validateDelete()) | |
workUserSession.delete(); | |
else | |
throw Exception::Error; | |
ttsCommit; | |
} | |
catch (Exception::Error) | |
{ | |
error(strFmt("Could not delete user session data for userId %1", | |
workUserSession.UserId)); | |
} | |
} | |
else | |
error(strFmt("UserId %1 not found!", | |
fieldWHSUserId.value())); | |
} | |
} |
Delete the WHS User Session in AX 7:
Like you will see, the dialog window changed slightly but the function is still the same.