#GPPT Using .Net Math Functions from Microsoft Dynamics GP

David Meego - Click for blog homepageA recent customization project I was working on using GP Power Tools required the calculation of a square root as part of some number crunching needed to work out material quantities on a Sales Order Processing transaction.

While Dexterity (the language behind Microsoft Dynamics GP) is great at accounting style mathematics (as per a standard calculator), it does not have advanced functions for powers, logs, and trigonometry (as per a scientific calculator). This means that there is no “native” method to calculate a square root.

However, Dexterity (from v14.0 onwards) has a .Net Interop which allows you to import and call .Net libraries directly from your Dexterity scripts. This example shows how to call the System.Math library as well as other advanced techniques, such as Custom Forms and Virtual Fields.

The magic of the .Net Interop works with the import and using commands. The import command allows a .Net Library or Assembly to be loaded and creates a reference to it. The using command declares the namespace and makes it available for use in the script.

Using GP Power Tools with Runtime Execute Setup is the easiest way to work with the .Net Interop. While it is possible to use the .Net Interop from a Trigger Setup handler script, using Runtime Execute has the advantage that it can be executed in a different dictionary context than the calling script. This is important because the .Net Interop only works properly when it is called in the context of the core Microsoft Dynamics GP (DYNAMICS or 0) dictionary. Using any other dictionary context generates an “Invalid import directive” error.

Note: Once the import command has been used in a script, any syntax error in the script returns the “Invalid import directive” error, even though that is not the issue. The best method of debugging in this situation is to comment out code and slowly bring it back to identify the line which is causing the error. Then you can work out what the real error is and fix it.

To use the .Net System.Math Library we need the import the System (version 4.0) library. In GP Power Tools, you can do this quickly using the Insert >> Import .Net Assembly Button Drop List.

Selecting the desired library and clicking OK will insert the required commands at the top of the script. As System.Math is part of the System library, we just added the extra line “using System.Math;” to get to the Math Library.

[code]
import "System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
using System;
using System.Math;

[/code]

Now we can use any of the Methods in the Math Class:

We have created a sample project which has two examples using the System.Math Library that you can look at.

The first example, execute Runtime Execute script MATH_FUNCTIONS_01, asks you for a number to obtain the Square Root of:

And then displays a dialog showing the result:

The second example is much more complex and demonstrates the usage of many advanced techniques. When the project triggers are running, you can select Inquiry >> System >> Math Functions Demo from the application menus or from the Administration Area Page. It will open a window to display a Sine wave graph:

The advanced techniques for this second sample include:

  • Registering a reserved Custom Blank Form using Visual Studio Integration Toolkit.
  • Adding an Application Level Menu using Visual Studio Integration Toolkit.
  • Opening the Custom Blank Form using Visual Studio Integration Toolkit.
  • Resizing the Custom Blank Form using Visual Studio Integration Toolkit.
  • Adding 44 Virtual Fields to the Custom Blank Form, being the title, two boxes for axis and 41 data points.
  • Using Memory Parameters to pass variables to the scripts calling .Net Math functions.
  • Using .Net Interop to perform scientific calculations not normally available in Dexterity.
  • Use of exception handling for when the .Net calls fail.

Note: The Visual Studio Integration Toolkit (build 19 or later) needs to be installed for the Application menu to work, and the additional Custom Forms module needs to be registered to be able to use the Custom Blank Form.

The following MATH_FUNCTIONS project achieves these points with 3 triggers and a script.

Trigger MATH_FUNCTIONS_01

This trigger using Visual Studio Integration Toolkit to add our Custom Blank form to the System >> Inquiry menu as “Math Functions Demo”. When selected it executes to Helper Functions to open the custom form.

Trigger MATH_FUNCTIONS_02

This trigger runs before the FORM PRE event on the Custom Blank Form 09 (VSTM_Forms_Blank_09) and calls MATH_FUNCTIONS_03 to add the Virtual Fields to window.

Runtime Execute Script MATH_FUNCTIONS_01

This script contains the first example and will use the getstring() system dialog to request a number and warning system dialog to display the Square Root of that number. The code includes tryend try exception handling for when the Math.Sqrt() .Net functions fail, for example requesting the Square Root of a negative number. It also uses the substitute command rather than concatenating strings.

Runtime Execute Script MATH_FUNCTIONS_02

This script is executed automatically on login as it is using the EventRegisterForm Custom Script Purpose to register Visual Studio Integration Toolkit Custom Forms. This script is using the FormRegisterReserve Helper Function added in VSIT Build 19 to specifically request form number 09.

Runtime Execute Script MATH_FUNCTIONS_03

This script is called by Trigger MATH_FUNCTIONS_02 when the Custom Blank Form 09 is opened before the FORM PRE event. The script resizes the custom form and then adds Virtual Fields for the Title and the axis. Then it loops for 41 times calculating the sine values to plot the graph by placing small string fields with red bold asterisk characters.

As we only have 10 of each type of Virtual Field in the GP Power Tools dictionary, we are using string fields of 5 different lengths with the name of the field calculated with a case statement inside the for loop.

Runtime Execute Script MATH_FUNCTIONS_04

This script performs the call to Math.Sin() to calculate the sine value. As the Math.Sin() function wants the input value in radians, the script also converts the degrees value into radians by multiplying by (Math.PI / 180). The values are passed in and out of the script using Memory Parameters. It also includes exception handling in case of calculation errors.

Downloading and Installing

The Visual Studio Integration Toolkit (build 19 or later) needs to be installed for the Application menu to work, and the additional Custom Forms module needs to be registered to be able to use the Custom Blank Form.

Also, GP Power Tools (build 31 or later) is required for Virtual Fields to work.

Download the example code, import using the Project Setup window (without any project showing, select the path to the xml file and click Import):

The code will be active on next login or after switching companies, or you can start the triggers manually from the Project Setup window.

More Information

For more information see:

Hope you find this example and the advanced techniques it demonstrates useful.

David

08-Oct-2025: Updated to use Blank Form 09 rather than Blank Form 10, so that it does not clash with other demo projects.

This article was originally posted on http://www.winthropdc.com/blog.

Leave a Reply