I have it all working with one exception: I cannot figure out how to capture and put into the audit table the Sugar UserID.
See below for what I have done, which I am now using and it works fine, with the above caveat.
This is useful when you want the auditing to enable MySQL queries which need dependable information on changes to the custom fields added to a relationship table (like the contact_role in contacts_opportunities). It does NOT allow you to see, from withing Sugar, the list of changes made, since this auditing is handled outside of Sugar and Sugar is therefore blind to the auditing.
If you can figure out either
- how to pass the Sugar UserID to the MySQL trigger
- how to audit a custom field in a relationship table using native SugarCRM capabilities
PLEASE feel free to let us know
To mimic what I have done, see below
===================
5 steps:
1) Create an audit table to capture the changes made to the source table
2) Create a trigger on the audit table to automatically generate a UUID() for each record entry
3) Create 3 triggers on the source table to log to the audit table any changes made to the source table whenever
3a A new relationship is created
3b An existing relationship is edited, and the custom fields are changed
3c An existing relationship is deleted (either by "Removing" the relationship from within SugarCRM or deleting the record from within MySQL)
For this example, I have
CRM MySQL dbase: ayudacrm
relationship table containing custom fields to be audited: evmgr_ev_contacts_c
custom field1: evmgr_ev_contacts_contact_ev_role
custom filed2: evmgr_ev_contacts_contact_ev_hours
auditing table created: evmgr_ev_contacts_c_audit
1) Create the audit table
There are numerous structures you could create with audit tables, but for simplicity and maintenance, I created the same structure SugarCRM uses when it creates an audit table
Run a MySQL command
Code:
DELIMITER $$
USE `ayudacrm` $$
DROP TABLE IF EXISTS `evmgr_ev_contacts_c_audit` $$
CREATE TABLE `evmgr_ev_contacts_c_audit`
(
id CHAR(36),
parent_id CHAR(36) NOT NULL,
date_created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
created_by VARCHAR(36),
field_name VARCHAR(100),
data_type VARCHAR(100),
before_value_string VARCHAR(255),
after_value_string VARCHAR(255),
before_value_text TEXT,
after_value_text TEXT,
PRIMARY KEY(id),
KEY(parent_id)
)
CHARACTER SET utf8 COLLATE utf8_general_ci $$
DELIMITER ;
2) Add the trigger to the audit table to auto-generate a UUID() for each new record created in the audit table
Run a MySQL command
Code:
DELIMITER $$
USE `ayudacrm` $$
DROP TRIGGER IF EXISTS evmgr_ev_contacts_c_audit_bi_id $$
CREATE TRIGGER evmgr_ev_contacts_c_audit_bi_id BEFORE INSERT ON evmgr_ev_contacts_c_audit FOR EACH ROW
BEGIN
SET NEW.id = UUID() ;
END; $$
DELIMITER ; 3a) Add the trigger to the source table to log any new records created in the source/relationship table
Run a MySQL command
Code:
DELIMITER $$
USE `ayudacrm` $$
DROP TRIGGER IF EXISTS `evmgr_ev_contacts_c_ai_audit` $$
CREATE TRIGGER `evmgr_ev_contacts_c_ai_audit` AFTER INSERT ON `ayudacrm`.`evmgr_ev_contacts_c`
FOR EACH ROW
BEGIN
IF (NEW.evmgr_ev_contacts_contact_ev_role <> '') AND (NEW.evmgr_ev_contacts_contact_ev_role IS NOT NULL) THEN
INSERT INTO `ayudacrm`.`evmgr_ev_contacts_c_audit`
(`parent_id`,`created_by`,`field_name`,`data_type`,`before_value_string`,`after_value_string`,`before_value_text`,`after_value_text`)
VALUES
(NEW.id,CONCAT(CURRENT_USER(),USER()),"evmgr_ev_contacts_contact_ev_role","VARCHAR(24)","---new_record---",NEW.evmgr_ev_contacts_contact_ev_role,NULL,NULL);
END IF;
IF (NEW.evmgr_ev_contacts_contact_ev_hours <> 0) AND (NEW.evmgr_ev_contacts_contact_ev_hours IS NOT NULL) THEN
INSERT INTO `ayudacrm`.`evmgr_ev_contacts_c_audit`
(`parent_id`,`created_by`,`field_name`,`data_type`,`before_value_string`,`after_value_string`,`before_value_text`,`after_value_text`)
VALUES
(NEW.id,CONCAT(CURRENT_USER(),USER()),"evmgr_ev_contacts_contact_ev_hours","VARCHAR(24)","---new_record---",NEW.evmgr_ev_contacts_contact_ev_hours,NULL,NULL);
END IF;
END;$$
DELIMITER ; 3b) Add the trigger to the source table to log any changes made to the custom fields in the source/relationship table
Note that you have to treat a relationship "Removede" from within Sugar as a MySQL update not a MySQL delete
since the record is not actually being deleted, it is having one of its fields' (deleted) values set to 1
Run a MySQL command
Code:
DELIMITER $$
USE `ayudacrm` $$
DROP TRIGGER IF EXISTS `evmgr_ev_contacts_c_au_audit` $$
CREATE TRIGGER `evmgr_ev_contacts_c_au_audit` AFTER UPDATE ON `ayudacrm`.`evmgr_ev_contacts_c`
FOR EACH ROW
BEGIN
IF (NEW.deleted = 1 AND OLD.deleted = 0) THEN
INSERT INTO `ayudacrm`.`evmgr_ev_contacts_c_audit`
(`parent_id`,`created_by`,`field_name`,`data_type`,`before_value_string`,`after_value_string`,`before_value_text`,`after_value_text`)
VALUES
(OLD.id,CONCAT(CURRENT_USER(),USER()),"evmgr_ev_contacts_contact_ev_role","VARCHAR(24)",OLD.evmgr_ev_contacts_contact_ev_role,"---deleted_record---",NULL,NULL);
INSERT INTO `ayudacrm`.`evmgr_ev_contacts_c_audit`
(`parent_id`,`created_by`,`field_name`,`data_type`,`before_value_string`,`after_value_string`,`before_value_text`,`after_value_text`)
VALUES
(OLD.id,CONCAT(CURRENT_USER(),USER()),"evmgr_ev_contacts_contact_ev_hours","VARCHAR(24)",OLD.evmgr_ev_contacts_contact_ev_hours,"---deleted_record---",NULL,NULL);
END IF;
IF (NEW.deleted = 0 AND ((NEW.evmgr_ev_contacts_contact_ev_role <> OLD.evmgr_ev_contacts_contact_ev_role) OR (NEW.evmgr_ev_contacts_contact_ev_role IS NOT NULL AND OLD.evmgr_ev_contacts_contact_ev_role IS NULL) OR (NEW.evmgr_ev_contacts_contact_ev_role IS NULL AND OLD.evmgr_ev_contacts_contact_ev_role IS NOT NULL))) THEN
INSERT INTO `ayudacrm`.`evmgr_ev_contacts_c_audit`
(`parent_id`,`created_by`,`field_name`,`data_type`,`before_value_string`,`after_value_string`,`before_value_text`,`after_value_text`)
VALUES
(NEW.id,CONCAT(CURRENT_USER(),USER()),"evmgr_ev_contacts_contact_ev_role","VARCHAR(24)",OLD.evmgr_ev_contacts_contact_ev_role,NEW.evmgr_ev_contacts_contact_ev_role,NULL,NULL);
END IF;
IF (NEW.deleted = 0 AND ((NEW.evmgr_ev_contacts_contact_ev_hours <> OLD.evmgr_ev_contacts_contact_ev_hours) OR (NEW.evmgr_ev_contacts_contact_ev_hours IS NOT NULL AND OLD.evmgr_ev_contacts_contact_ev_hours IS NULL) OR (NEW.evmgr_ev_contacts_contact_ev_hours IS NULL AND OLD.evmgr_ev_contacts_contact_ev_hours IS NOT NULL))) THEN
INSERT INTO `ayudacrm`.`evmgr_ev_contacts_c_audit`
(`parent_id`,`created_by`,`field_name`,`data_type`,`before_value_string`,`after_value_string`,`before_value_text`,`after_value_text`)
VALUES
(NEW.id,CONCAT(CURRENT_USER(),USER()),"evmgr_ev_contacts_contact_ev_hours","VARCHAR(24)",OLD.evmgr_ev_contacts_contact_ev_hours,NEW.evmgr_ev_contacts_contact_ev_hours,NULL,NULL);
END IF;
END;$$
DELIMITER ; 3c) Add the trigger to the source table to log any records that are deleted from the source/relationship table
Note that this trigger would only fire is a record was actually deleted from within MySQL
Run a MySQL command
Code:
DELIMITER $$
USE `ayudacrm` $$
DROP TRIGGER IF EXISTS `evmgr_ev_contacts_c_ad_audit` $$
CREATE TRIGGER `evmgr_ev_contacts_c_ad_audit` AFTER DELETE ON `ayudacrm`.`evmgr_ev_contacts_c`
FOR EACH ROW
BEGIN
INSERT INTO `ayudacrm`.`evmgr_ev_contacts_c_audit`
(`parent_id`,`created_by`,`field_name`,`data_type`,`before_value_string`,`after_value_string`,`before_value_text`,`after_value_text`)
VALUES
(OLD.id,CONCAT(CURRENT_USER(),USER()),"evmgr_ev_contacts_contact_ev_role","VARCHAR(24)",OLD.evmgr_ev_contacts_contact_ev_role,"---deleted_record---",NULL,NULL);
INSERT INTO `ayudacrm`.`evmgr_ev_contacts_c_audit`
(`parent_id`,`created_by`,`field_name`,`data_type`,`before_value_string`,`after_value_string`,`before_value_text`,`after_value_text`)
VALUES
(OLD.id,CONCAT(CURRENT_USER(),USER()),"evmgr_ev_contacts_contact_ev_hours","VARCHAR(24)",OLD.evmgr_ev_contacts_contact_ev_hours,"---deleted_record---",NULL,NULL);
END;$$
DELIMITER ;
That's it!
You have an audit table logging changes, additions or deletions to custom fields in a relationship table
Bookmarks