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

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

    Fullscreen
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    ({
    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'] || {};
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

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

    Fullscreen
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    ({
    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'] || {};
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Children