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.

Background script

Manu143
Tera Contributor

Hi Community,

 How to write the fix script  to set the yes/no variable as yes for  the existing RITMS if the doallar amount value is greater  than or equal to 100000 .

 

Thanks,

Manu

7 REPLIES 7

Vishal Birajdar
Giga Sage

Hello @Manu143 

 

As you wanted to update the variable ...Can you try like below 

 

var grRitm = new GlideRecord('sc_req_item');
grRitm.addEncodedQuery('Query where dollor amount is gt or et 10000');   //your query
grRitm.query();

while(grRitm.next()){
//use variables.<variable name>
grRitm.variables.<your_variable_name> = 'Yes';   //use backend value 
grRitm.update();
}

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

Anubhav24
Mega Sage

Hi @Manu143 ,

By variable updation you mean , the questions asked to user while the user is submitting a request if yes then you need to use below script:

 

function setTemplateValueByRitmID(ritmID, templateValue, optionValue) {
var ritmGr = new GlideRecord('sc_req_item');
if (ritmGr.get(ritmID)) {
gs.log('Matched with ' + ritmGr.number + 'Matched from ' + templateValue);
ritmGr.<variable name> = optionValue;
ritmGr.setWorkflow(false);
ritmGr.autoSysFields(false);
ritmGr.update();
}
}

var optGr = new GlideRecord('sc_item_option_mtom');
optGr.orderBy('request_item');
optGr.addEncodedQuery('what ever is the catalog item and its variable value conditions');
optGr.query();

while (optGr.next()) {
setTemplateValueByRitmID(optGr.request_item, optGr.request_item.number, optGr.sc_item_option.value);
}
gs.log('Updation ends');

 

You can customise it the way you want.

 

Please mark helpful/correct if my response helped you.

Devang
Tera Contributor
// Define the threshold
var threshold = 100000;

// GlideRecord to query RITMs
var ritmGR = new GlideRecord('sc_req_item');
ritmGR.addQuery('active', true); // Optional: only active RITMs
ritmGR.query();

while (ritmGR.next()) {
    // Access the variable set via the 'variables' field
    var dollarAmount = parseFloat(ritmGR.variables.dollar_amount); // Replace with actual variable name
    var yesNoVar = ritmGR.variables.yes_no_variable; // Replace with actual variable name

    if (!isNaN(dollarAmount) && dollarAmount >= threshold) {
        // Set Yes/No variable to 'Yes'
        ritmGR.variables.yes_no_variable = 'Yes'; // or true depending on field type
        ritmGR.update();
    }
}