
This is the fifth 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 code to make the Salesperson ID field on the Sales Transaction Entry – Sales Customer Detail Entry sub window (separate form) required.
As the Sales Transaction Entry (SOP_Entry) form uses the required() function against the form’s main window only and the required field is on a completely separate form anyway, making the field required with code or Modifier will not work to prevent saving.
So, we will add a check and display a specific warning and then open the sub window and place the focus on the required field.
To make this example work we need the following to happen:
- Change the field’s Object Property: Required to true so its prompt is highlighted.
- Just before Saving, check if the field has a value, and display a warning message if it hasn’t.
- Prevent the Save to table from occurring if the field is empty.
- Open the sub window and place the focus on the required field.
The following SOP_SALESPERSON project achieves these points with 3 triggers and a script.
Trigger SOP_SALESPERSON_01
This trigger runs before the Save event. For this window, saving to the table happens in the ‘Save Record’ field’s change script. You can capture logs and look at the Script Log to identify if the save happens from a field change script or from a form level procedure or function.
The trigger script will check if all the required fields on the main window have already been entered. It is not worth us checking for our particular field if the other required fields have not yet been entered.
Note: Even though the editing of the ‘Salesperson ID’ field is on a separate form, the field is copied to a local hidden copy of the field on the Sales Transaction Entry window. This makes it simple to check if a value has been entered by checking this hidden field.
Then we check if our ‘Salesperson ID’ field is empty and, if it is, we load and execute delayed the SOP_SALESPERSON_01 script to open the window and set the focus.
Then we set OUT_Condition = true and use the Actions tab to display the following message as a simple system dialog.
The final step is to prevent the original Save event from executing, which we can do by using the Issue Reject Script option on the Actions tab. This works as the Save event code is contained in the ‘Save Record’ field’s change script and so can be stopped with a reject script command.
Trigger SOP_SALESPERSON_02
This trigger runs before the Window Pre event of the main window of the SOP_Customer_Detail_Entry form. The Window Pre event on a form’s main window is triggered when the window is opened and each time the form is restarted (eg. after save, clear or delete). The code is simply a call to the Field_SetBooleanProperty() function to change the field’s Required property to true. No additional actions are needed.
Note: The Sales Customer Detail Entry window checks required fields when OK is clicked, so if this window is opened, it will not be possible to save changes without entering the Salesperson ID field. However, if the window is never opened and the Salesperson ID is not defaulted in from the Customer master record, this project will prevent it being saved as empty.
Trigger SOP_SALESPERSON_03
This optional trigger can be disabled if desired. It runs before moving into the Scrolling Window to enter lines of the Sales Transaction Entry window and checks if the Salesperson ID has been entered. As the Salesperson ID on the transaction header is copied to the transaction lines, it is important that the Salesperson ID is entered before any lines are created.
The trigger uses the same logic as the SOP_SALESPERSON_01 trigger, but sets the focus back to the Customer Name field and stops the Scrolling Window Pre script from running.
Runtime Execute Script SOP_SALESPERSON_01
This script will open the Sales Customer Detail Entry (SOP_Customer_Detail_Entry) window if it is not already open and then set the focus to the ‘Salesperson ID’ field. It is executed by the SOP_SALESPERSON_01 trigger to run delayed, so that it happens after the dialog is closed.
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 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:
- #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 http://www.winthropdc.com/blog.


