How do I change the logic for due date for metric data task form?

ChuanYanF
Tera Guru

Dear experts,

 

I would like to change the logic for the due date generation for the sn_grc_metric_data_task table, to a custom due date period generation of my choice, how should I do that? 

ChuanYanF_0-1747288649345.png

 

 

1 ACCEPTED SOLUTION

This is my working script:

(function executeRule(current, previous /*null when async*/) {
    // Validate that current.metric is not empty
    if (!current.metric) {
        gs.warn('Metric field is empty. Setting fallback due date.');
        setFallbackDueDate(current);
        return;
    }

    // Query the sn_grc_metric_metric table
    var metricGR = new GlideRecord('sn_grc_metric_metric');
    if (metricGR.get(current.metric)) {
        // Check if next_run_time is valid
        if (metricGR.next_run_time) {
            var gdt = new GlideDateTime(metricGR.next_run_time);
            gdt.addDaysUTC(4); // Add 4 days
            // Set time to 23:59:00
            gdt.setValue(gdt.getDate() + ' 15:59:00');
            current.due_date = gdt;
        } else {
            gs.warn('Next run time is empty for metric: ' + current.metric.getDisplayValue());
            setFallbackDueDate(current);
        }
    } else {
        gs.warn('Metric record not found for: ' + current.metric.getDisplayValue());
        setFallbackDueDate(current);
    }

    // Helper function to set fallback due date
    function setFallbackDueDate(current) {
        var fallback = new GlideDateTime();
        fallback.addDaysUTC(4); // Add 4 days
        // Set time to 23:59:00
        fallback.setValue(fallback.getDate() + ' 15:59:00');
        current.due_date = fallback;
    }
})(current, previous);

View solution in original post

6 REPLIES 6

hi 

try once with following modified script :

var metricGR = new GlideRecord('sn_grc_metric_metric');
    if (current.metric && metricGR.get(current.metric)) {
        // Check if next_run_time exists and is valid
        if (metricGR.next_run_time) {
            var gdt = new GlideDateTime(metricGR.next_run_time);
            gs.info('Original next_run_time: ' + gdt.getDisplayValue());

            // Add 4 days
            gdt.addDaysUTC(4);
            gs.info('After adding 4 days: ' + gdt.getDisplayValue());

            // Set the time to 23:59:00 in the user's timezone
            var datePart = gdt.getLocalDate(); // Get the date in user's timezone
            gdt.setValue(datePart + ' 23:59:00');
            gs.info('Final due_date before setting: ' + gdt.getDisplayValue());

            // Set the due_date
            current.due_date = gdt;
            gs.info('Due date set to: ' + current.due_date.getDisplayValue());
        } else {
            gs.warn('No valid next_run_time found for metric: ' + current.metric);
            setFallbackDueDate(current);
        }
    } else {
        gs.warn('No valid metric found for metric_data_task: ' + current.sys_id);
        setFallbackDueDate(current);
    }

    // Function to set fallback due date
    function setFallbackDueDate(current) {
        var fallback = new GlideDateTime();
        fallback.addDaysUTC(4); // Add 4 days from current date
        var datePart = fallback.getLocalDate(); // Get date in user's timezone
        fallback.setValue(datePart + ' 23:59:00'); // Set time to 23:59:00
        current.due_date = fallback;
        gs.info('Fallback due date set to: ' + current.due_date.getDisplayValue());
    }

This is my working script:

(function executeRule(current, previous /*null when async*/) {
    // Validate that current.metric is not empty
    if (!current.metric) {
        gs.warn('Metric field is empty. Setting fallback due date.');
        setFallbackDueDate(current);
        return;
    }

    // Query the sn_grc_metric_metric table
    var metricGR = new GlideRecord('sn_grc_metric_metric');
    if (metricGR.get(current.metric)) {
        // Check if next_run_time is valid
        if (metricGR.next_run_time) {
            var gdt = new GlideDateTime(metricGR.next_run_time);
            gdt.addDaysUTC(4); // Add 4 days
            // Set time to 23:59:00
            gdt.setValue(gdt.getDate() + ' 15:59:00');
            current.due_date = gdt;
        } else {
            gs.warn('Next run time is empty for metric: ' + current.metric.getDisplayValue());
            setFallbackDueDate(current);
        }
    } else {
        gs.warn('Metric record not found for: ' + current.metric.getDisplayValue());
        setFallbackDueDate(current);
    }

    // Helper function to set fallback due date
    function setFallbackDueDate(current) {
        var fallback = new GlideDateTime();
        fallback.addDaysUTC(4); // Add 4 days
        // Set time to 23:59:00
        fallback.setValue(fallback.getDate() + ' 15:59:00');
        current.due_date = fallback;
    }
})(current, previous);