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

Rajesh Chopade1
Mega Sage

Hi @ChuanYanF 

you can use a Business Rule or Flow Designer to set the due_date field based on your custom logic when a record is inserted or updated.
If you prefer a low-code approach use flow designer: Create flow and set Trigger to Record Created or Record Updated on the sn_grc_metric_data_task table.

Add an Update Record action to set the due_date field.

Use a Script Step to calculate the custom due date:

var dueDate = new GlideDateTime();
dueDate.addDays(7); // Your custom period
return dueDate.getDisplayValue();

Map the script output to the due_date field in the Update Record action.

Activate and test the flow.

 

I hope my answer helps you to resolve your issue, if yes please mark my answer helpful and correct.

thank you

Rajesh

I tried to create a before insert business rule with a script like this but it does not work, can you let me know what is wrong with my scrip?

var metricGR = new GlideRecord('sn_grc_metric_metric');
if (metricGR.get(current.metric)) {
    var gdt = new GlideDateTime(metricGR.next_run_time);
    gdt.addDaysUTC(4);  // Add 4 days (May 1 + 4 = May 5)
    gdt.addDaysLocalTime(23, 59, 0);  // Set time to 23:59:00
    current.due_date = gdt;
} else {
    var fallback = new GlideDateTime();
    fallback.addDaysUTC(4);
    fallback.addDaysLocalTime(23, 59, 0);
    current.due_date = fallback;
}

hi @ChuanYanF 

Try bellow code once :

(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() + ' 23: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() + ' 23:59:00');
        current.due_date = fallback;
    }
})(current, previous);

Hi Rajesh, it does not work and the expected output should be 5-June-2025 23:59 it shows 6-June-2025 7:59