Set Value to list collector using background script

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-03-2017 12:18 PM
Hello all,
We recently changed a dictionary field, called Impacted Product (u_project), (List Collector) to point to a new table.
We noticed after the move, there is a sys_id in the place of the value(s) in the List Collector field. I understand a List Collector can have 0 or more values. For this example, let's pretend all records have 0 or 1 value in the field.
I wrote a background script to get the sys_id in Impacted Product, go to the original table, get the name of the product, then go to the new product table, get that sys_id, and update Impacted product w/ the new sys_id.
When I try to update the record, it just wipes out the original sys_id value.
Here is my code:
var req = new GlideRecord('u_triage');
req.addNotNullQuery('u_project'); //gets me the records I want
req.query();
gs.log('number of records: ' + req.getRowCount()); //this is good
while(req.next()){
gs.log('sys id of u_project: ' + req.u_project); //gets me the sys_id of the value in the list collector
var prodname = new GlideRecord('cmdb_ci'); //old location of products
prodname.addQuery('sys_id',req.u_project); //match sys_id of original record in cmdb_ci table
prodname.query();
while(prodname.next()) {
var name = prodname.name.toString(); //declare variable name to equal name of record in cmdb_ci table
var newprod = new GlideRecord('u_product');
newprod.addQuery('u_name',name); //query for name found in cmdb_ci table in the new product table
newprod.query();
while(newprod.next()){
gs.log('sys id of product name: ' + newprod.sys_id); //this works great
req.autoSysFields(false);
req.setWorkflow(false);
req.setValue('u_project',newprod.sys_id);
req.update();
}
}
}
HOwever, when I run the script above, it just wipes out the original value of the impacted product and never updates the field w/ the true product from the u_product table
Any suggestions?
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-03-2017 12:24 PM
Try below
- var req = new GlideRecord('u_triage');
- req.addNotNullQuery('u_project'); //gets me the records I want
- req.query();
- gs.log('number of records: ' + req.getRowCount()); //this is good
- while(req.next()){
- gs.log('sys id of u_project: ' + req.u_project); //gets me the sys_id of the value in the list collector
- var prodname = new GlideRecord('cmdb_ci'); //old location of products
- prodname.addQuery('sys_id',req.u_project); //match sys_id of original record in cmdb_ci table
- prodname.query();
- while(prodname.next()) {
- var name = prodname.name.toString(); //declare variable name to equal name of record in cmdb_ci table
- var newprod = new GlideRecord('u_product');
- newprod.addQuery('u_name',name); //query for name found in cmdb_ci table in the new product table
- newprod.query();
- while(newprod.next()){
- gs.log('sys id of product name: ' + newprod.sys_id); //this works great
- req.autoSysFields(false);
- req.setWorkflow(false);
- req.setValue('u_project',newprod.getValue('sys_id')); /// change it to getValue as when using setValue data types should match ----https://developer.servicenow.com/app.do#!/api_doc?v=jakarta&id=r_GlideRecord-setValue_String_Object
- req.update();
- }
- }
- }

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-07-2017 07:13 AM
HI Abdul,
This works great for a single reference field. Still can't get the list collector to update with the updated script above. perhaps list collectors cannot be updated in this fashion?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-07-2017 07:31 AM
I did stumble across this. Looks like I won't be able to update a list collector with setValue until I upgrade from Geneva:
ServiceNow KB: Unable to set the value of a list collector using setValue() (KB0622779)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-07-2017 07:57 AM
If you are using list field type. You cannot use setValue, I think just setting the value to comma seperated sys_id's will do the trick. So you should generate an array with sys_id's and then assign to the custom field.