A database trigger responds to a successful record delete if you specify TRIGGER_ON_DB_DELETE in the table_operations parameter of the Trigger_RegisterDatabase() function. If successful, the database operation passes the removed record’s buffer to your processing procedure as an inout parameter.
There are two ways in which you can use delete database triggers; responding to a record deleted from a Microsoft Dynamics GP form, and responding to a record deleted from a Microsoft Dynamics GP procedure.
Database triggers are useful for deleting a corresponding third-party record in response to a delete operation completed using a Microsoft Dynamics GP form (when the user clicks the Delete button). Prior to deleting the third-party record, use a focus trigger to verify that the table operation can occur for your record as well.
This database trigger is activated by a successful delete of a customer record from the RM_Customer_Maintenance form. In this case, it runs only when the user clicks the Delete button in the RM_Customer_Maintenance form. A focus trigger (not shown here) on the Delete button first checks to be sure the it can read the corresponding third-party record before it deletes the record. If the trigger can’t read the record, it issues a reject script statement and all processing stops. If it can read the record, the Microsoft Dynamics GP delete occurs, and this delete trigger is activated.
{Name: Startup} local integer l_result; l_result = Trigger_RegisterDatabase(anonymous(table RM_Customer_MSTR), form RM_Customer_Maintenance, TRIGGER_ON_DB_DELETE, script IG_Delete_Contact_History); if l_result <> SY_NOERR then warning "Database trigger registration failed."; end if;
This trigger processing procedure deletes the third-party contact history record that corresponds to the deleted RM_Customer_MSTR record.
{Name: IG_Delete_Contact_History} inout table RM_Customer_MSTR; 'Customer Number' of table IG_Contact_History_MSTR = 'Customer Number' of table RM_Customer_MSTR; release table IG_Contact_History_MSTR; change table IG_Contact_History_MSTR by number 1; if err() = OKAY then remove table IG_Contact_History_MSTR; if isopen(form IG_Contact_History) then clear form IG_Contact_History; unlock 'Customer Number' of window IG_Contact_History of form IG_Contact_History; end if; end if;
Database triggers are also useful for responding to a delete operation completed by a Microsoft Dynamics GP procedure. The difficulty here is that you cannot verify the delete operation for a related third-party record using a focus trigger. A Microsoft Dynamics GP record deleted by a procedure will simply run the delete trigger on the third-party table.
To handle this situation, delete a related third-party record conditionally within the processing procedure that responds to the database delete trigger. This will also work in cases where a Microsoft Dynamics GP procedure removes a range of records. The trigger then runs for each successful record delete in the range.
This trigger processing procedure responds to a record deleted in the RM_Customer_MSTR table by a Microsoft Dynamics GP procedure. It sets key values, then attempts to delete the third-party record. If the delete operation isn’t successful (because another user has locked the record) the trigger writes the record to a log file.
{Name: IG_Delete_Contact_History} inout table RM_Customer_MSTR; 'Customer Number' of table IG_Contact_History_MSTR = 'Customer Number' of table RM_Customer_MSTR; change table IG_Contact_History_MSTR by number 1; remove table IG_Contact_History_MSTR; if err() = LOCKED then {Write the records to a log file.} 'Customer Number' of table IG_Contact_Error_Log = 'Customer Number' of IG_Contact_History_MSTR; 'Customer Name' of table IG_Contact_Error_Log = 'Customer Name' of IG_Contact_History_MSTR; save table IG_Contact_Error_Log; end if;