The CreatorCon Call for Content is officially open! Get started here.

Background Script - Updating Submitted RITM Field

LouisG
Giga Guru

Hello,

I created a background script with the intent to update a field on submitted RITMs, but instead of updating the field value, it is duplicating a variable I'm not intending to touch. In addition to creating the duplicate variable, it logs that it is making the updates to the field, but when I navigate over to the RITM and view the "hardware" field, its value remains unchanged. 

Any insight on this would be greatly appreciated!

find_real_file.png

var count = 0;
var sftware = 0;
var gr = new GlideRecord('sc_req_item');    //set the table for the GlideRecord
gr.setLimit(30);                            //set limit to only 30 records to test
gr.autoSysFields(false);                    //Do not update sys_updated_by, sys_updated_on, sys_mod_count, sys_created_by, and sys_created_on
gr.addQuery('cat_item', '11e915b7dbfbbb0095d5f4b5ae9619fd') //ITD Procurment Request
gr.query(); 
while(gr.next()) {
  var gr2 = new GlideRecord('sc_item_option_mtom');
  gr2.addQuery('request_item', gr.sys_id);
  gr2.orderByDesc('sc_item_option.item_option_new.name');
  gr2.query();
  gs.log(gr.number);
  while(gr2.next()){
    gs.log("Variable: " + gr2.sc_item_option.item_option_new.name);
    gs.log("Value   : " + gr2.sc_item_option.value);
    if(gr2.sc_item_option.item_option_new.name == 'hardware_or_software' && gr2.sc_item_option.value == 'software'){
      sftware = 1;
    }
    if(sftware == 1 && gr2.sc_item_option.item_option_new.name == 'hardware' && gr2.sc_item_option.value != null){
      gs.log(gr.number);
      gr2.sc_item_option.value.setValue(null);
      gs.log('===== UPDATED HARDWARE VARIABLE TO \'\' =====');
      gs.log("Variable: " + gr2.sc_item_option.item_option_new.name);
      gs.log("Value   : " + gr2.sc_item_option.value);
      sftware = 0;
      count++;
    }
  }
  gs.log('///////////////////////////////////////////////////');
  gs.log('///////////////////////////////////////////////////');
  gs.log('');
  sftware = 0;
  gr2.setWorkflow(false);
  gr2.update();
}
gs.log(count + ' updates made.');
gs.log("Done.");
1 ACCEPTED SOLUTION

Michael Jones -
Giga Sage

So...you want to update the value of a variable on a RITM, correct? You don't need to query down to the sc_item_option table to make the change. You can change it directly on sc_req_item.

Try something like this: Change your setLimit to 1 to try it out on a single record. 

var count = 0;
var sftware = 0;
var gr = new GlideRecord('sc_req_item');    //set the table for the GlideRecord
gr.setLimit(30);                            //set limit to only 30 records to test
gr.autoSysFields(false);                    //Do not update sys_updated_by, sys_updated_on, sys_mod_count, sys_created_by, and sys_created_on
gr.addQuery('cat_item', '11e915b7dbfbbb0095d5f4b5ae9619fd'); //ITD Procurment Request
gr.query(); 
while(gr.next()) {
//You can access variables values in a backgorund script using gr.variables.<name>
 
	
    if(gr.variables.hardware_or_software == 'software' && gr.variables.hardware != null){
      gs.log(gr.number);
      gr.variables.hardware.setValue(null);
      gs.log('===== UPDATED HARDWARE VARIABLE TO \'\' =====');
      gs.log("Variable: " + "gr.variables.hardware");
      gs.log("Value   : " + gr.variables.hardware);
      gr.setWorkflow(false);
      gr.update();
      count++;
    }
  
  gs.log('///////////////////////////////////////////////////');
  gs.log('///////////////////////////////////////////////////');
  gs.log('');
}
gs.log(count + ' updates made.');
gs.log("Done.");

If this was helpful or correct, please be kind and remember to click appropriately! Michael Jones - Proud member of the CloudPires team!

I hope this helps!
Michael D. Jones
Proud member of the GlideFast Consulting Team!

View solution in original post

2 REPLIES 2

rajneeshbaranwa
Giga Guru

Looking at your query, You are updating the value of the variable. Note gr2 is mtom table related with variables while gr is sc_req_item table .

If you want to update a field in requested item table, you need to replace.

 

gr2.sc_item_option.value.setValue(null);

gr2.update();

 

to 

 

gr.field_name = '';

gr.update();

This should help you.

Michael Jones -
Giga Sage

So...you want to update the value of a variable on a RITM, correct? You don't need to query down to the sc_item_option table to make the change. You can change it directly on sc_req_item.

Try something like this: Change your setLimit to 1 to try it out on a single record. 

var count = 0;
var sftware = 0;
var gr = new GlideRecord('sc_req_item');    //set the table for the GlideRecord
gr.setLimit(30);                            //set limit to only 30 records to test
gr.autoSysFields(false);                    //Do not update sys_updated_by, sys_updated_on, sys_mod_count, sys_created_by, and sys_created_on
gr.addQuery('cat_item', '11e915b7dbfbbb0095d5f4b5ae9619fd'); //ITD Procurment Request
gr.query(); 
while(gr.next()) {
//You can access variables values in a backgorund script using gr.variables.<name>
 
	
    if(gr.variables.hardware_or_software == 'software' && gr.variables.hardware != null){
      gs.log(gr.number);
      gr.variables.hardware.setValue(null);
      gs.log('===== UPDATED HARDWARE VARIABLE TO \'\' =====');
      gs.log("Variable: " + "gr.variables.hardware");
      gs.log("Value   : " + gr.variables.hardware);
      gr.setWorkflow(false);
      gr.update();
      count++;
    }
  
  gs.log('///////////////////////////////////////////////////');
  gs.log('///////////////////////////////////////////////////');
  gs.log('');
}
gs.log(count + ' updates made.');
gs.log("Done.");

If this was helpful or correct, please be kind and remember to click appropriately! Michael Jones - Proud member of the CloudPires team!

I hope this helps!
Michael D. Jones
Proud member of the GlideFast Consulting Team!