Relate field not showing in audit log after saving through the logic hook in version 10.

Hi everyone,

I have a related field and saving it through the logic hook. After saving it is not showing in the audit log in version 10 and the field is selected with audit in the studio.

It works fine when we save it normally in the module but getting an issue with the logic hook.

Please suggest a solution.

Thanks

Laxmi Chand Saini

Parents
  • Hi,

    Yes, I have faced the same scenario in my project. Audit log record will create only on UI updates.

    So, I would like to suggest, add the logic for audit log record creation for relationship field in your logichook file.

    Hope this would help for you.

  • Hi Dhanalaskhmi

    Can you share your logic?

    laxmichand

  • Hi Laxmichand,

    I persoanlly have not tested this code, but can you just give a try whether it works for you or not.

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

    <?php
    $hook_array['before_save'][] = Array(1, '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);
            }
        }
    }

    Step 3: Please perform the Quick Repair and Rebuild and check the code if it works or  not.

    Let me know how it goes for you :)

  • Hi Poojitha,

    It is not working proper. $bean->fetched_rel_row['account_id'] alway getting empty.

    A new value showing in the audit but the old value is missing.

    laxmichand

Reply Children
  • Hi,

    Can you try with $bean->fetched_row['account_id'].

  • Hi Laxmichand,

    The record which you are trying does it have related account in contact initially, if so pls add global statement once and check once. Or else you can replace the first line in code with

     if($bean->fetched_row['account_id'] != $bean->account_id) 

    and add one global statement and check whether it is entering into if condition or not.

    This Logic Hook should use before_save trigger. Let me know how it goes for you.

     

  • Sorry missed one more thing, please try this code in auditAcc.php. Please check if you are able to get these two statements printed in logs or not.

    <?php
    class auditAccC
    {
    function auditAccF($bean)
    {

    $GLOBALS['log']->fatal("printing fetched row account id" . $bean->fetched_row['account_id']);

    $GLOBALS['log']->fatal("printing current account id" . $bean->['account_id']);

    // check for the change
    if ($bean->fetched_row['account_id'] != $bean->account_id)
    {

    $GLOBALS['log']->fatal("entered into if condition");

    // 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_row['account_id'];
    $aChange['after'] = $bean->account_id;
    // save audit entry
    $bean->db->save_audit_records($bean, $aChange);
    }
    }

    Please try this code and perform QRR and check once. Let me know how it goes for you this time.