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

  • 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 Laxmi Chand,

    Yes, unfortunately, Audit Log does not log this change.

    However, that may help us to look at the situation from the use case perspective :)

    I think of questions:
    1. What user role requests to log this change? Isn't that other role than the role of the change performer?
    2. How they are going to use that physical data change collected? How are they treat it not physically but logically? Does context matter for consideration of the change happened?

    If they pay attention to a particular field and eager to monitor the change, they probably treat change as a key fact and therefore logically consider it somehow.
    It is possible to help facts-seekers not to go through numerous similar physical changes in the databaseinstead it is possible to help readers more  - use the same logical hook to collect logical key event on the Timeline,  specifying not only the previous and new value but giving a possibility to monitor the named key event within the context and in a form of conclusion  - like this "Tier 1 customer XYZ  has got a Key Person changed from PeterP to BillB. OMG!" 

    Actually, there is an interface to observe key events collected with logic hooks - the Timeline Viewer for Sugar
    It helps to monitor not only for the particular record, but also monitor for the list (e.g. "Tier 1 customers"), dashlet and report.

    The approach of using TimeLine to manage different events regardless of their source and nature and origin is quite common in CRM that collects info rather then data

    Best,
    Dmytro

    Best Regards,
    Dmytro Chupylka

    integroscrm.com
    We make work in Sugar CRM system faster, more convenient and efficient

  • Sorry, due to restrictions I couldn't share my logic.

  • 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

  • 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.