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

Need some help with a record producer script

mitzaka
Mega Guru

Hey SNC,

I need some help with a record producer script. My case is the following - I have a record producer which is on the cmdb_ci table. I am asking a few questions:

1. what action you want to take (options here are a):new , b):remove as

a): new - it will update multiple CI records's field called 'u_desired_version' with a string which will be entered in the record producer variable 'dvm_desired_version'

b): remove - it will remove the so called field 'u_desired_version' in multiple CI records to null (nothing)

2. second question in my record producer is a reference field where you can choose the class (sys_class_name) of the CIs you want to interact with

3. third question in my record producer is a list collector which will allow you to pick the CIs which you want to set the desired version field for , from the CIs available in the selected class in step 2

So first how do I make question #3 (the list - slashbucket) dependent on the choice from #2 - so when you choose a class, only the CIs from that class will be present in the list selector?

And then in the script, how do I update those selected CIs, I guess I need to pass the variable (which would be an array) to the glide record query where I would set the value of the desired version field for the selected CIs?

My draft script is something like this:

if(producer.dvm_action == 'new'){ //this is my variable for the action selection

var arr = producer.dvm_ciselection.split(','); //here is where supposedly I am trying to capture the list of CIs from the CI selector

var gr = new GlideRecord('cmdb_ci'); //here I am trying to find all of the CIs

gr.addQuery('sys_class_name',producer.dvm_ciclass); //then I want to filter only the ones which are selected

gr.addQuery('name','IN',arr); //here is where I want to filter only the CIs which are selected from the selector - I am not sure I am doing this right, feels like not...

gr.query();

while(gr.next()){

gr.setValue('u_desired_version',producer.dvm_desired_version); //here I set the desired version field to the queried filtered CIs

}

}

else if(producer.dvm_action == 'remove'){

var arr = producer.dvm_ciselection.split(',');

var gr = new GlideRecord('cmdb_ci');

gr.addQuery('sys_class_name',producer.dvm_ciclass);

gr.addQuery('name','IN',arr);

gr.query();

while(gr.next()){

gr.setValue('u_desired_version','');

}

}

1 ACCEPTED SOLUTION

Okay, I think I solved this myself. The problem was in the fetching of the correct CI record from the glide record query.


Below is the correct workflow script code:



//get the desired version from the catalog item variable


var desv = current.variables.dvm_desired_version;


gs.addInfoMessage("You have successfully set a desired version of: " + desv);


var list = current.variables.dvm_cilist.toString().split(','); //build the array of cis from the catalog item list collector


var array = list; //use this variable as the array


for (var i=0; i < array.length; i++){


      var ciitem = array[i]; //remember the current ci


      //find the ci item in the database


      var gr = new GlideRecord('cmdb_ci');


      gr.addQuery('sys_id',ciitem);


      gr.query();


      while(gr.next()){


              gr.setValue("u_desired_version",desv); //update the desired verison of the found ci


              gr.update();


      }


}


View solution in original post

4 REPLIES 4

Michael Fry1
Kilo Patron

You're going to have to do #3 in a client script, not the script field on the record producer. Take a look here on options: https://www.servicenowguru.com/scripting/client-scripts-scripting/changing-filter-list-collector-var...


I decided in the end that I would go with an order guide which calls catalog items upon action selection. Each catalog item is triggering a workflow which I want to update the configuration items with the chosen desired version (which is chosen by the requestor in the catalog item variable).


So I went ahead and did my workflow and catalog item but I am still unable to figure our why the configuration items are not updated by the script. Below is my script from the workflow:



var desv = current.variables.dvm_desired_version; // here I am getting the desired version string set by the requestor


gs.addInfoMessage("desired version is: " + desv); //verification of the desired version


var list = current.variables.dvm_cilist.toString().split(','); //here I am getting the list of sysids for the selected configuration items


var array = list;


for (var i=0; i < array.length; i++){


gs.addInfoMessage("ci list is:" + array[i]); //verification of the selected cis



var gr = new GlideRecord('cmdb_ci');


      gr.addQuery('sys_id',array[i].sys_id); //here I am getting the ci which has been selected, one at a time from the list


gr.query();


gs.addInfoMessage("found: " + array[i]); //verification


while(gr.next()){


gr.setValue('u_desired_version',desv); //here I am trying to set the desired field field on the CI - this part fails and I am unable to understand why


gr.update();


}


}


Okay, I think I solved this myself. The problem was in the fetching of the correct CI record from the glide record query.


Below is the correct workflow script code:



//get the desired version from the catalog item variable


var desv = current.variables.dvm_desired_version;


gs.addInfoMessage("You have successfully set a desired version of: " + desv);


var list = current.variables.dvm_cilist.toString().split(','); //build the array of cis from the catalog item list collector


var array = list; //use this variable as the array


for (var i=0; i < array.length; i++){


      var ciitem = array[i]; //remember the current ci


      //find the ci item in the database


      var gr = new GlideRecord('cmdb_ci');


      gr.addQuery('sys_id',ciitem);


      gr.query();


      while(gr.next()){


              gr.setValue("u_desired_version",desv); //update the desired verison of the found ci


              gr.update();


      }


}


kevclark
Tera Contributor

I know this is an old thread, but I thought I'd pop a note in here to let anyone who finds it know that ServiceNow has introduced a table called sc_item_produced_record - it looks like it came in in Istanbul, and it captures the relationship between Produced Records and their originating Record Producer.  This might help save others some pain in future-  it's not well documented afaik.