I have a valid until field use to deactivate metrics and it is not working, please help.

ChuanYanF
Tera Guru

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);
1 ACCEPTED SOLUTION

Chaitanya ILCR
Mega Patron

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

 

View solution in original post

1 REPLY 1

Chaitanya ILCR
Mega Patron

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