Focus triggers are useful in cases where you use a database trigger to save or delete a third-party record in response to a save or delete operation in Microsoft Dynamics GP (using the Save or Delete button). This type of focus trigger should run before the original button change script. If the record operation doesn’t meet certain conditions (such as the user didn’t enter all required fields), you can cancel table operations for your application and for Microsoft Dynamics GP using the reject script statement.
You shouldn’t use focus triggers to save or delete a record directly, because there’s no way of knowing whether the Microsoft Dynamics GP table operation was successful or not. Instead, you should use database triggers to save and delete third-party records based on a successful Microsoft Dynamics GP table operation. |
To save a third-party record with a related Microsoft Dynamics GP record, apply a focus trigger to the Save button. This trigger can verify whether conditions required for the save have been met, such as whether the user addressed all required fields in the third-party window. If the save operation doesn’t meet these conditions, the reject script statement stops processing for the Microsoft Dynamics GP save operation.
Once the user addresses the required fields, then clicks Save again, the focus trigger verifies the conditions for the save, and a database trigger can complete the save.
This trigger is activated when the user clicks the Save button in the Customer Maintenance window. Notice that it runs before the original save button change script.
{Name: Startup} local integer l_result; l_result = Trigger_RegisterFocus(anonymous('Save Button' of window RM_Customer_Maintenance of form RM_Customer_Maintenance), TRIGGER_FOCUS_CHANGE, TRIGGER_BEFORE_ORIGINAL, script IG_Verify_Contact_Save); if l_result <> SY_NOERR then warning "Trigger registration failed."; end if;
This trigger processing procedure runs in response to the focus trigger on the Save button. If the user doesn’t enter all required fields in the Contact History window, the reject script statement cancels all processing and the user can address the required fields. Otherwise, the Microsoft Dynamics GP save operation runs to completion.
{Name: IG_Verify_Contact_Save} local integer l_answer; if isopen(form IG_Contact_History) then if not required(form IG_Contact_History) then l_answer = ask("Not all required contact information was entered. Do you want to enter this information?", "Yes", "No", "Cancel"); if l_answer = ASKBUTTON1 then {The user clicked Yes, so stop all processing.} reject script; elseif l_answer = ASKBUTTON2 then {The user doesn't want to save the third-party record, so allow the Microsoft Dynamics GP save to proceed.} clear form IG_Contact_History; else {The user clicked Cancel, so stop all processing.} reject script; end if; end if; end if;
When the user deletes a record from Microsoft Dynamics GP, it may be necessary to delete a corresponding record from a third-party table. A focus trigger on the Delete button can verify whether the third-party record can be deleted. If the third-party record can’t be deleted, the reject script statement cancels all processing. The process is shown in the following illustration.
If the third-party record can be deleted, Microsoft Dynamics GP is allowed to continue and delete its record. The database trigger then runs in response to the Microsoft Dynamics GP record delete, and completes the third-party record delete.
This focus trigger is activated when the user clicks the Delete button in the Customer Maintenance window. Notice that it runs before the original Delete button change script.
{Name: Startup} local integer l_result; l_result = Trigger_RegisterFocus(anonymous('Delete Button' of window RM_Customer_Maintenance of form RM_Customer_Maintenance), TRIGGER_FOCUS_CHANGE, TRIGGER_BEFORE_ORIGINAL, script IG_Verify_Contact_Delete); if l_result <> SY_NOERR then warning "Trigger registration failed."; end if;
This trigger processing procedure attempts to read and actively lock a third-party record. If it can, the Microsoft Dynamics GP delete continues. If it can’t obtain an active lock, it issues a reject script statement and stops all processing.
{Name: IG_Verify_Contact_Delete} release table IG_Contact_History; 'Customer Number' of table IG_Contact_History = 'Customer Number' of window RM_Customer_Maintenance of form RM_Customer_Maintenance; {Read the record and try to actively lock it.} change table IG_Contact_History by IG_Contact_History_Key1, lock; if err() = LOCKED then {The record is actively locked by another user and can't be deleted. Cancel all processing.} warning "This customer record can't be deleted. Other users are editing customer contact information."; reject script; end if;