#GPPT Making Class ID on Vendor Maintenance Required

This is the ninth article in the series on making window fields required. If you have not yet read the introduction article to the series, please read that first:
This example uses GP Power Tools to make the Class ID field on the Vendor Maintenance window required and change the dialog to add a specific message.
This article comes after a support case that was raised with us after the code which worked for the Customer Maintenance window failed to work with the Vendor Maintenance window.
The Vendor Maintenance window is using the required() function so making the Class ID field is as simple as changing the Required field property for the field to true. You can do this with GP Power Tools’ Form Control module or using Developer Tools with a single trigger on the Window Pre event using the Field_SetBooleanProperty() command to change the FIELD_PROP_REQUIRED to true. The additional twist is that we also want to change the dialog displayed to have the wording “Please select a Class ID before Saving.” and then leave the cursor on the Class ID field.
We recently make this exact customization using GP Power Tools for the Customer Maintenance window and the instructions and the project can be found in the following article:
You would expect that we can use the same techniques for the Vendor Maintenance window, so let’s try that.
Create The Project
Let’s start by creating the project:
- Open the Project Setup window. Click clear if an existing current project is displayed.
- Enter the Project ID: VEND_CLASS_ID
- Enter the Project Description: Make the Vendor Class ID required with changed dialog wording
- Enter the Open Form (optional) for testing, in the 0: Microsoft Dynamics GP product, PM_Vendor_Maintenance
- Mark the project as your Current Project.
- Press Ctrl-S to save and continue (or Options Menu >> Save and continue).
Make the Field Required
To make the field required on the window, use the following steps to create a trigger after the window pre event:
- From the Project, Click Add >> Trigger Setup.
- Enter the Trigger ID: VEND_CLASS_ID_01 and Trigger Description: Make the Vendor Class ID Field required.
- Check Start Trigger Automatically checkbox.
- Set Trigger Type: Focus Event, Trigger Event, Window Pre, Trigger Attach: After Original.
- On the Resource Tab, select Product: 0: Microsoft Dynamics GP, Form Name: PM_Vendor_Maintenance, Window Name: PM_Vendor_Maintenance.
- On the Script Tab, add the Field_SetBooleanProperty() command as shown below to set the Required Property to True.
- Click Save.
out boolean OUT_Condition;
OUT_Condition = false;
if isopen(form PM_Vendor_Maintenance) then
Field_SetBooleanProperty('Vendor Class ID' of window 'PM_Vendor_Maintenance' of form 'PM_Vendor_Maintenance', FIELD_PROP_REQUIRED, true);
OUT_Condition = true;
end if;
Change the Dialog Message
We could finish there, but we also want to change the message displayed in the dialog and set the focus to the Class ID field. To make this happen we can add a second trigger before the ‘Save Record’ field script as this is the script we expect to be used to save the customer to the PM_Vendor_MSTR table. Use the following steps to create the second trigger:
- From the Project, Click Add >> Trigger Setup.
- Enter the Trigger ID: VEND_CLASS_ID_02 and Trigger Description: Change Warning Dialog to say the Vendor Class ID is required.
- Check Start Trigger Automatically checkbox.
- Set Trigger Type: Focus Event, Trigger Event, Field Change, Trigger Attach: Before Original.
- On the Resource Tab, select Product: 0: Microsoft Dynamics GP, Form Name: PM_Vendor_Maintenance, Window Name: PM_Vendor_Maintenance, Field Name ‘Save Record’.
- On the Actions Tab, select Display Message to screen using system dialog, Display Message: Please select a Class ID before Saving., select Display Message to screen using simple dialog instead of text dialog, Dialog Type: Warning. Then select Issue Reject Script. This will display the desired warning and prevent the original code running when our script returns OUT_Condition as true.
- On the Script Tab, add a check for the required fields (manually as we don’t want to include the Class ID field) and then check if Class ID is empty. If the conditions are met we can set focus to the Class ID field and set the OUT_Condition to true.
- Click Save.
in string IN_OldValue;
in string IN_NewValue;
out boolean OUT_Condition;
OUT_Condition = false;
if isopen(form PM_Vendor_Maintenance) then
{ Can't use Required as Customer Class ID is now part of required() }
//if required(window 'PM_Vendor_Maintenance' of form 'PM_Vendor_Maintenance') then
if not empty('Vendor ID' of window 'PM_Vendor_Maintenance' of form 'PM_Vendor_Maintenance') and
not empty('Vendor Name' of window 'PM_Vendor_Maintenance' of form 'PM_Vendor_Maintenance') then
if empty('Vendor Class ID' of window 'PM_Vendor_Maintenance' of form 'PM_Vendor_Maintenance') then
focus 'Vendor Class ID' of window PM_Vendor_Maintenance of form PM_Vendor_Maintenance;
OUT_Condition = true;
end if;
end if;
end if;
Conclusion
You would expect that this is all that is required to make the field required and change the message displayed when the Class ID field has not been entered. However, the message has not been changed and the default message is displayed and the focus has not been moved to the Class ID field.
So, what went wrong….
Adding a warning statement into the handler script for the ‘Save Record’ trigger confirmed that the trigger never actually fires. Capturing logs of clicking the save button with GP Power Tools (from the Tools menu of the window) shows that the Save Button does not call the ‘Save Record’ field change script, but in fact calls the form level function Save() of form PM_Vendor_Maintenance.
19:01:00 'PM_Vendor_Maintenance_Save_Button_CHG on form PM_Vendor_Maintenance' 19:01:00 'CloseAdditionalAccounts of form PM_Vendor_Maintenance' 19:01:00 'Save() of form PM_Vendor_Maintenance', 0, table 'PM_Vendor_MSTR', table 'PM_SETP', 0, 0, 0
The interesting thing about Functions and Procedures is that they cannot be stopped in the same way a trigger before a focus event can use reject script to prevent the original application code from executing. This means that we can display a dialog with the different wording as desired, but we cannot prevent the original dialog from showing afterwards.
As mentioned in the article #GPPT Making Window Fields Required and Why It Does Not Always Work, the required fields warning is displayed using the command call Warning_Dialog,2351,””,””,””;. The Warning_Dialog global procedure displays messages from the SY_Error_Messages_MSTR (SY01700) using the SY_Error_Message form.
GP Power Tools supports triggers before and after the Warning_Dialog procedure, so we can alter the VEND_CLASS_ID_02 trigger to fire afterwards and change the wording displayed on the dialog.
To change the focus to the Class ID field, we are going to need a Runtime Execute that sets the focus which we can run delayed.
- From the Project, Click Add >> Runtime Execute Setup.
- Enter the Script ID: VEND_CLASS_ID_02 and Script Name: Set focus to Vendor Class ID field.
- Add the script as shown below to set the focus.
- Click Save
if isopen(form 'PM_Vendor_Maintenance') then focus 'Vendor Class ID' of window 'PM_Vendor_Maintenance' of form 'PM_Vendor_Maintenance'; end if;
- Then from the Project, double click on VEND_CLASS_ID_02 to open the trigger in Trigger Setup.
- Set Trigger Type: Warning Dialog, Trigger Event, Warning Dialog, Trigger Attach: After Original.
- On the Script Tab, in the second section (else) of the if statement we can add the manual check for the ‘Vendor ID’ and ‘Vendor Name’ fields and that the Class ID field has not been entered. We can then use this to change the messages displayed on the SY_Error_Message window and then run a Runtime Execute script delayed to set the focus to the Class ID field.
- Click Save.
in 'Error MSG Number' IN_error_number;
in string IN_variable_1, IN_variable_2, IN_variable_3;
in integer nErrorCode;
in string sErrorText;
out boolean OUT_Condition;
local long l_SQL_Error_State_Value, l_SQL_Return_Status_Value;
local text MBS_Text_Field;
local integer MBS_Dictionary;
local integer MBS_Status;
OUT_Condition = false;
if 'SQL Server' of globals > 0 and nErrorCode = WARNINGDIALOGGET then
l_SQL_Return_Status_Value = value(IN_variable_2);
l_SQL_Error_State_Value = value(IN_variable_3);
if not(l_SQL_Error_State_Value < 0 and l_SQL_Return_Status_Value = 0) then
case IN_error_number
in [0] { Insert Error Number Here }
if IN_variable_1 = "SprocName" then { Insert Stored Procedure Name Here }
OUT_Condition = true;
end if;
else
end case;
end if;
else
case IN_error_number
in [2351] { Insert Error Number Here }
if isopen(form PM_Vendor_Maintenance) then
{ Can't use Required as Vendor Class ID is now part of required() }
//if required(window 'PM_Vendor_Maintenance' of form 'PM_Vendor_Maintenance') then
if not empty('Vendor ID' of window 'PM_Vendor_Maintenance' of form 'PM_Vendor_Maintenance') and
not empty('Vendor Name' of window 'PM_Vendor_Maintenance' of form 'PM_Vendor_Maintenance') then
if empty('Vendor Class ID' of window 'PM_Vendor_Maintenance' of form 'PM_Vendor_Maintenance') then
if isopen(form 'SY_Error_Message') then
'(L) Message String'[1] of window 'SY_Error_Message' of form 'SY_Error_Message' = "Please select a Class ID before Saving.";
'(L) Message String'[2] of window 'SY_Error_Message' of form 'SY_Error_Message' = "";
end if;
{ Set Focus using Delayed Script }
call with name "MBS_Script_Load_Dex" in dictionary 5261, "VEND_CLASS_ID_02",
MBS_Text_Field, MBS_Dictionary;
call with name "MBS_Runtime_Execute_Delayed" in dictionary 5261,
MBS_Text_Field, MBS_Dictionary, MBS_Status;
if MBS_Status <> OKAY then
warning MBS_Text_Field;
end if;
OUT_Condition = true;
end if;
end if;
end if;
OUT_Condition = true;
else
end case;
end if;
Conclusion again
Testing the code again and it works as desired. The message is now changed, and the focus is set to the Class ID field.
But, we have another problem.
If the Vendor Maintenance window is open and the Class ID field is empty and the Vendor ID and Vendor Names have been entered, we meet all the conditions to change the Warning Dialog message when the Warning_Dialog command is called with the ID 2351….. from any window. This means that any other window can make this trigger fire and change the message and set the focus to the Class ID on the Vendor Maintenance window.
To solve this issue and get to the final solution, we need to use the Three Trigger Technique. This technique uses two additional triggers to ensure that the main trigger is only enabled while it is needed and disabled at all other times. This will ensure that the changing of the warning dialog and setting the focus will only happen when it is triggered by calling the Warning_Dialog procedure from inside the Save() of form PM_Vendor_Maintenance form level function.
- From the Project, double click on VEND_CLASS_ID_02 to open the trigger in Trigger Setup.
- On the Options tab, select Start Trigger Temporarily Disabled.
- Click Save.
This will register the trigger but leave it disabled until it is needed.
- From the Project, Click Add >> Trigger Setup.
- Enter the Trigger ID: VEND_CLASS_ID_03 and Trigger Description: Start Warning Dialog Trigger.
- Check Start Trigger Automatically checkbox.
- Set Trigger Type: Function, Trigger Event, Form Level Trigger Attach: Before Original.
- On the Resource Tab, select Product: 0: Microsoft Dynamics GP, Form Name: PM_Vendor_Maintenance, Function Name: Save.
- On the Script Tab, use the Helper Functions window with Working with Logging, Triggers and Projects >> Enable a Temporarily Disabled Trigger ID to enable the VEND_CLASS_ID_02 trigger.
- Click Save.
out boolean OUT_Condition; OUT_Condition = false; if isopen(form PM_Vendor_Maintenance) then call with name "MBS_Trigger_EnableSingle" in dictionary 5261, "VEND_CLASS_ID_02"; OUT_Condition = true; end if;
- Then from the Project, Click Add >> Trigger Setup.
- Enter the Trigger ID: VEND_CLASS_ID_04 and Trigger Description: Stop Warning Dialog Trigger.
- Check Start Trigger Automatically checkbox.
- Set Trigger Type: Function, Trigger Event, Form Level Trigger Attach: After Original.
- On the Resource Tab, select Product: 0: Microsoft Dynamics GP, Form Name: PM_Vendor_Maintenance, Function Name: Save.
- On the Script Tab, use the Helper Functions window with Working with Logging, Triggers and Projects >> Temporarily Disable a Trigger ID to disable the VEND_CLASS_ID_02 trigger.
- Click Save.
out boolean OUT_Condition; OUT_Condition = false; if isopen(form PM_Vendor_Maintenance) then call with name "MBS_Trigger_DisableSingle" in dictionary 5261, "VEND_CLASS_ID_02"; OUT_Condition = true; end if;
Final Conclusion
Now we have the code working correctly AND only working when used with the Vendor Maintenance window.
Downloading and Installing
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 triggers (if not disabled) 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:
- #GPPT Making Window Fields Required and Why It Does Not Always Work
This article contains the links to the other sample projects in the series.
- GP Power Tools Portal: http://winthropdc.com/GPPT
- GP Power Tools Samples: http://winthropdc.com/GPPT/Samples
- GP Power Tools Videos: http://winthropdc.com/GPPT/Videos
Hope you find this example useful.
David
This article was originally posted on https://www.winthropdc.com/blog.


