- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-18-2025 06:53 PM
Dear experts,
I have created a valid until field to run a scheduled job to deactivate certain metrics but somehow my script is not working, please advise.
(function executeRule(current, gsr) {
var today = new GlideDate();
today.setDisplayValue(gs.nowDate()); // 'yyyy-MM-dd' format
var mdGR = new GlideRecord('sn_grc_metric_definition'); // Replace if using a different table
mdGR.addQuery('u_valid_until', '<=', today); // Compare u_valid_until <= today
mdGR.addQuery('active', true); // Only process active records
mdGR.query();
while (mdGR.next()) {
mdGR.setValue('active', false);
mdGR.update();
gs.info('Deactivated Metric Definition: ' + mdGR.getDisplayValue('name') + ' due to expired valid_until date.');
}
})(current, gsr);
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-18-2025 07:09 PM
Hi @ChuanYanF ,
(function executeRule() {
try {
var today = new GlideDate();
// today.setDisplayValue(gs.nowDate()); // 'yyyy-MM-dd' format
var mdGR = new GlideRecord('sn_grc_metric_definition'); // Replace if using a different table
mdGR.addQuery('u_valid_until', '<=', today.getValue()); // Compare u_valid_until <= today
mdGR.addQuery('active', true); // Only process active records
mdGR.query();
while (mdGR.next()) {
mdGR.setValue('active', false);
mdGR.update();
gs.info('Deactivated Metric Definition: ' + mdGR.getDisplayValue() + ' due to expired valid_until date.');
}
} catch (err) {
gs.error(' Deactivated Metric Definition: Error ' + err);
}
})();
Try this
I have added Error handling if in case of any error check the logs and share it here
Please mark my answer as helpful/correct if it resolves your query.
Regards,
Chaitanya
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-18-2025 07:09 PM
Hi @ChuanYanF ,
(function executeRule() {
try {
var today = new GlideDate();
// today.setDisplayValue(gs.nowDate()); // 'yyyy-MM-dd' format
var mdGR = new GlideRecord('sn_grc_metric_definition'); // Replace if using a different table
mdGR.addQuery('u_valid_until', '<=', today.getValue()); // Compare u_valid_until <= today
mdGR.addQuery('active', true); // Only process active records
mdGR.query();
while (mdGR.next()) {
mdGR.setValue('active', false);
mdGR.update();
gs.info('Deactivated Metric Definition: ' + mdGR.getDisplayValue() + ' due to expired valid_until date.');
}
} catch (err) {
gs.error(' Deactivated Metric Definition: Error ' + err);
}
})();
Try this
I have added Error handling if in case of any error check the logs and share it here
Please mark my answer as helpful/correct if it resolves your query.
Regards,
Chaitanya