Audit log issue in before save hook

Hello Everyone 

I am facing an issue with the audit log in before save hook. 

I have a field that is enabled for the audit log. In before save logic hook I am throwing an exception on specific conditions in which data is not getting saved but still audit log is getting created so I have to avoid this audit log being created when the data is not saved. 

I have checked that no other code is affecting this in other logic hooks and record.js files.

Thanks in advance

  • Hello

    Thanks for reaching out. 
    Regarding your scenario, is this Save action coming from the user interface? 


    If yes, the best approach would be to have any validation performed at the controller level, therefore, preventing the Save to be triggered at all if the conditions don't fit your requirements.

    https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_12.0/Cookbook/Adding_Field_Validation_to_the_Record_View/index.html

    This will also prevent this validation to be triggered when the record is updated via other sources, Scheduler, API, related record saves (Etc..). That could cause Exceptions to be thrown that could impact non-related processes. 

    Let me know if this helps or if your scenario does not fit my recommendations. 

    Cheers, 

    André

  • Hello Andre,

    To prevent the creation of an audit log entry in logic hooks, a specific exception-based solution is sought to restrict users from changing the status of a lead within a certain time frame. When the status is set to "New lead," an exception is thrown to halt the status change for 4 hours. The exception successfully prevents users from modifying the status, but the problem lies in the creation of audit logs even when the data is not saved.

    To clarify, the desired behavior is to avoid recording any audit log when the exception is triggered and the status change is not allowed. Currently, the exception prevents the status change but does not prevent the audit log from being generated, even though the data remains unchanged.

    The objective is to find a solution that allows the exception to take precedence over the audit log creation when the status change is blocked. This way, users cannot change the status during the restricted period, and no audit log is recorded for the prevented change.

    Thanks

  • Hello
    I hope this detail will make a clear understanding.
    Let me know if you have any confusion here.

    Thanks

  • Hi 


    Thanks for the extra detail. 
    I still believe the best approach is to handle validation in a .js controller and prevent saving if conditions aren't met.

    I did a quick test locally and seems to work fine, do you think something like this would work for you? 

    custom/modules/Leads/clients/base/views/record/record.js

    ({
        extendsFrom: 'LeadsRecordView',
    
        initialize: function (options) {
            this._super('initialize', [options]);
    
            // Add validation task
            this.model.addValidationTask('check_recycled_lead', _.bind(this._doValidateRecycledLead, this));
        },
    
        _doValidateRecycledLead: function(fields, errors, callback) {
            var leadStatus = this.model.get('status');
            var dateModified = new Date(this.model.get('date_modified'));
            var currentDateTime = new Date();
    
            // Calculate time difference in milliseconds
            var timeDifference = currentDateTime - dateModified;
            var hoursDifference = timeDifference / (1000 * 60 * 60);
    
            if (leadStatus === 'Recycled' && hoursDifference < 4) {
                errors['status'] = errors['status'] || {};
    
                    app.alert.show('message-id', {
                    level: 'error',
                    messages: 'Recycled leads must have a date modified older than 4 hours.',
                    autoClose: false
                    });
    
            }
    
            callback(null, fields, errors);
        },
    })
    

  • Hey  

    I'm pleased to share that I've successfully implemented a solution that prevents the creation of audit logs, thanks to the guidance and support I received.