Running Dexterity scripts after Login or before Logout

David MeegoA while back I did a post on running Visual Basic for Applications (VBA) scripts when logging into and out of Microsoft Dynamics GP. 

Recently, I was asked about the best method of capturing the same events from Dexterity.

I have seen developers use triggers after the 'OK Button' of window 'Switch Company' of form 'Switch Company' in an attempt to capture the login event.  However, just because the 'OK Button' was pressed does not guarantee that the login was successful.  In the case where the login fails for some reason, a trigger in this location will still fire and will run when the user is not logged into a company and may cause errors.

My recommendation is to use the two activity tracking scripts that are run by the system when there is a successful login or logout event.  They are the global procedures Add_Successful_Login_Record and Add_Successful_Logout_Record. But this alone is not enough....

The issue is that using triggers on these two scripts does not handle when a user returns to Microsoft Dynamics GP from the Report Writer or Modifier. So we need some additional code to handle this situation.

Below is some example code, which uses a global boolean system variable I created called 'MBS Logged In' of globals:

Startup Global Procedure

 pragma(disable warning LiteralStringUsed);

{ Log in and Log out Triggers }
if Trigger_RegisterProcedure(script 'Add_Successful_Login_Record', TRIGGER_AFTER_ORIGINAL, script Set_Environment) <> SY_NOERR then
    warning "Log on procedure trigger registration failed.";
end if;
if Trigger_RegisterFocus(anonymous(form Toolbar), TRIGGER_FOCUS_PRE, TRIGGER_AFTER_ORIGINAL, script MBS_Toolbar_FORM_PRE) <> SY_NOERR then
 warning "Toolbar form focus pre trigger registration failed.";
end if;
if Trigger_RegisterProcedure(script 'Add_Successful_Logout_Record', TRIGGER_AFTER_ORIGINAL, script Set_Environment_POST) <> SY_NOERR then
    warning "Log off procedure trigger registration failed.";
end if;

pragma(enable warning LiteralStringUsed);

Set_Environment Global Procedure

 { Add code here which needs to be executed only on login }
{ for example table creation and updating of security tables }

{ Put your code below here }

{ Put your code above here }

{ Call script for code to run every time, including returning }
{ Report Writer or Modifier }

call Set_Environment_Sub;

'MBS Logged In' of globals = true;

Set_Environment_Sub Global Procedure

 { Code to be run on login and returning from Report Writer/Modifier }

{ Put your code below here }

{ Put your code above here }

MBS_Toolbar_FORM_PRE Global Procedure

 { When returning from Modifier or Report Writer }
if not empty('User ID' of globals) and not empty('Company ID' of globals) then
    if 'MBS Logged In' of globals then
      call Set_Environment_Sub;
   end if;
end if;

Set_Environment_POST Global Procedure

 { Code to be run on logout }

{ Put your code below here }

{ Put your code above here }

'MBS Logged In' of globals = false;

So code in the Set_Environment script will only be executed when actually logging into a company and code in the Set_Environment_Sub script will be executed when logging in and when returning to Microsoft Dynamics GP from the Report Writer or Modifier.  This improves on the method mentioned in the following Knowledge Base (KB) article:

A third-party customization is still triggered even if a user cannot log on to the company in Microsoft Dynamics GP (KB 943959) Secure Link

Please use the link below to look at the original VBA related article:

VBA - Running VBA scripts after Login or before Logout Example

Hope you find this method useful. 

David