Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Convert Business rule to Fix Script to update the old records in a table.

Munna1
Tera Contributor

Hello everyone,

 

I want to convert the below Business rule to Fixscript.

After Insert and Update BR

Written on  Key_values Table

(function executeRule(current, previous /*null when async*/) {

    var data = new GlideRecord('cmdb_ci_cloud');
data.addQuery('sys_id', current.configuration_item);
data.query();
if (data.next()) {
    if (current.value == 'dev')
        data.instance = 'Development';
    else if (current.value == 'acc')
        data.instance = 'Acceptance';
        else if(current.value == 'prod')
        data.instance = 'Production';
    data.update();
}
})(current, previous);
1 REPLY 1

Brad Bowman
Kilo Patron
Kilo Patron

You would first add a GlideRecord on the Key Value table, to return records with Value = dev, acc, or prod, and configuration_item.instance not Development, Acceptance, or Production - or whatever your criteria for updating these records is.  In the while loop of this first GR will be your existing script replacing current.configuration_item and value with the keyvalueGR.configuration_item and value.  So something like this:

var keyvalues = new GlideRecord('cmdb_key_value');
keyvalues.addQuery('value', 'IN', 'dev, acc, prod');
keyvalues.addQuery('configuration_item.instance', 'NOTIN', 'Development, Acceptance, Production' );
keyvalues.query();
while (keyvalues.next()){
    var data = new GlideRecord('cmdb_ci_cloud');
    data.addQuery('sys_id', keyvalues.configuration_item);
    data.query();
    if (data.next()) {
        if (current.value == 'dev')
            data.instance = 'Development';
        else if (current.value == 'acc')
            data.instance = 'Acceptance';
        else if (current.value == 'prod')
            data.instance = 'Production';
        data.update();
    }
}