19
February

0 Comments | 0 Shares | Urdhva Tech

Plenty of times the forums have hit the same question, since I know SugarCRM.

"How to audit relate fields?" No out of box solution for it!

But I simply love the power of logic hooks.

Lets win the world!

For this blog post, we will audit Account name field under Contacts module.

Step 1: Create a before save definition logic hook under custom/modules/Contacts/logic_hooks.php

<?php
$hook_array['before_save'][] = Array(90, 'Audit account name', 'custom/modules/Contacts/auditAcc.php','auditAccC', 'auditAccF');

Step 2: Create file auditAcc.php under custom/modules/Contacts folder and add following code.

<?php
class auditAccC{
    function auditAccF($bean){
        // check for the change
        if($bean->fetched_rel_row['account_id'] != $bean->account_id){
            // prepare an array to audit the changes in parent module's audit table
            $aChange = array();
            $aChange['field_name'] = 'account_id';
            $aChange['data_type'] = 'relate';
            $aChange['before'] = $bean->fetched_rel_row['account_id'];
            $aChange['after'] = $bean->account_id;
            // save audit entry
            $bean->db->save_audit_records($bean, $aChange);
        }
    }
}

Blink of an eye! Done! Test it out!

Hope this helps and feels like missing piece is just found!

Feel free to drop your comments.

Your valuable feedback means a lot.
Download attachments:
Comments
  • No Comments Found.
Post your comment