Dynamics SL VBA Debugging in Production

Posted by Joe Jacob, Senior Developer on November 21, 2016

Have you ever tested Microsoft Dynamics SL VBA code in a production environment?   Users don’t always like developers “messing” with their systems, but sometimes it’s necessary.

It’s best practice to debug on a test environment, limiting any unwanted contamination of production data.  If you don’t have the ability to do that, here’s something to try.

Create an SL user group in your production environment.  Then you can sprinkle messages throughout your source code to fire off only to that group.  This way you can test various conditions in your live environment without affecting the other users.

Here’s How: 

  1. Create an SL User group 'VBADEBUG' and assign only your own user ID to the group.  You can call it whatever you'd like but be consistent within your code.
  2. Next add the USERGRP DH code to your project.
  3. Under your form load add:
    Sub Form1_Load()
    	     Call SqlCursorEx(CSR_USERGRP, NOLEVEL + SqlSystemDb, 
                 "CSR_USERGRP", "USERGRP", "USERGRP")
    	End sub
  4. Create a new module with the following code:
    ' ***************************** debug module ********************************
    	Global CSR_USERGRP As Integer 
            Global isDebugMode As Boolean 
            Public Sub GetDebugMode()     
              Dim sUserId     
           Call GetBufferValue("bpes.UserId", sUserId)     
           serr1 = SqlFetch1(CSR_USERGRP, "select * from 
           UserGrp  where groupid = 'VBADEBUG' and userid = 
           " + SParm(sUserId), bUserGrp, LenB(bUserGrp))     
           If serr1 <> NOTFOUND Then         
              isDebugMode = True     
              isDebugMode = False     
           End If              
           End Sub 
           Public Sub DebugMessage(sMsg As String)     
           If isDebugMode Then
             Call MessBox(sMsg, MB_OK, "Debug")
           End If     
          End Sub 
    '**************************** debug module *******************************
  5. On your Form Display method add:
    Sub Form1_Display() GetDebugMode End Sub
  6. Finally add as many messages as you'd like to capture values, or isolate functionality (until testing is complete). For example:
    Sub Spread1_LineChk(action%, maintflg%, retval%)
    	DebugMessage ("Action = " + CStr(action)) End sub
    	' or
    	If isDebugMode then RunNewMethod

After testing is completed you can remove the appropriate debug messages or ISDEBUGMODE conditions, but still leave the other code behind for future use.  The other beauty of this method is that you could leave all the DEBUGMESSAGE() calls in the code but remove yourself from the VBADEBUG group.  Either way, it’s a great trick because the overhead is minimal.

Need assistance with your Dynamics SL VBA solutions?  Email our team of experts at Support@Crestwood.com and we can help you out.

Share this:


Add your comment: