Saturday, January 10, 2015

Limitation with Dynamics GP VS Tools Event Handlers?

By Steve Endow

Visual Studio Tools Event Handlers are a great feature that allows you to respond to events and actions in Dynamics GP.  Let's say that you want to validate data after a user selects a value from a GP drop down list, or you want to run some code when the user selects a customer ID in a window.

Or, as in my current project, let's say that you want to suppress or click past some Dynamics GP dialog boxes!

The Void Historical Payables Transaction window was modified in Dynamics GP 2013 to add a Vendor ID filter.  This is a huge improvement for customers that actually need to use this window--you can now filter on Vendor ID and Document Number to quickly find the document to be voided.

But another change made in the Void Historical Payables Window in GP 2013 is that if you use the "Mark All" button, two very annoying dialog boxes appear.

I understand that these dialogs have a purpose, but for my current project of automating a PM void, they are getting in the way.

So, using a handy-dandy VS Tools event handler, I'm able to suppress the dialog boxes.

Here I am registering the event handler:

pmVoidPaymentsWindow.BeforeModalDialog += new EventHandlerBeforeModalDialogEventArgs>(PmVoidPayments_BeforeModalDialog);

And here is the code that will fire when the event occurs:

void PmVoidPayments_BeforeModalDialog(object sender, Microsoft.Dexterity.Bridge.BeforeModalDialogEventArgs e)
    if (e.Message.StartsWith("Your selection may include payments that have been reconciled"))
        e.Response = Microsoft.Dexterity.Bridge.DialogResponse.Button2;
    else if (e.Message.StartsWith("Your selection may include credit card payments with related invoices"))
        e.Response = Microsoft.Dexterity.Bridge.DialogResponse.Button2;


This Event Handler code works great.  If I click on the Mark All button on the GP window, I don't even see the dialog boxes--they are suppressed by the VS Tools code.  And if I have some code in the GpAddIn.cs class that clicks the Mark All button programmatically, it also works.

But.......if I have code outside of the GpAddIn.cs file that clicks the Mark All button, the event handler doesn't fire.

In the screen shot above, I have a separate VS Tools form (FrmVoidTest) with a Mark All button.

The button simply calls pmVoidPaymentsWindow.MarkAll.RunValidate(), just like my test code in GpAddIn.cs.  But for some reason, when that method is called from the .NET form, the BeforeModalDialog event handler does not fire, and the two dialog boxes are displayed.

So based on my testing, there appears to be some limitation of VS Tools that requires any code that interacts with the GP form be called from within the GpAddIn.cs file in order to trigger event handlers.  Even if my test form calls the exact same method in the GpAddIn.cs file that clicks the Mark All button, the event handler is not triggered.

This seems really odd, but I've tried 3 or 4 different approaches and the event handler code just won't fire if I click the Mark All button outside of GpAddIn.cs.  I'm still researching it and asking a colleague to review my code to see if I'm missing something.

If anyone has any ideas or clever suggestions, I'm all ears!

Steve Endow is a Microsoft MVP for Dynamics GP and a Dynamics GP Certified IT Professional in Los Angeles.  He is the owner of Precipio Services, which provides Dynamics GP integrations, customizations, and automation solutions.

You can also find him on Google+ and Twitter

No comments: