- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-14-2025 10:57 PM
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?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-15-2025 10:23 PM
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-14-2025 11:53 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-15-2025 12:24 AM
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;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-15-2025 12:34 AM
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-15-2025 12:38 AM
Hi Rajesh, it does not work and the expected output should be 5-June-2025 23:59 it shows 6-June-2025 7:59