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.

Set Value to list collector using background script

matthew_magee1
Giga Guru

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?

4 REPLIES 4

Mateen
Tera Guru

Try below


  1. var req = new GlideRecord('u_triage');  
  2. req.addNotNullQuery('u_project'); //gets me the records I want  
  3. req.query();  
  4. gs.log('number of records: ' + req.getRowCount()); //this is good  
  5. while(req.next()){  
  6. gs.log('sys id of u_project: ' + req.u_project); //gets me the sys_id of the value in the list collector  
  7. var prodname = new   GlideRecord('cmdb_ci'); //old location of products  
  8. prodname.addQuery('sys_id',req.u_project); //match sys_id of original record in cmdb_ci table  
  9. prodname.query();  
  10. while(prodname.next()) {  
  11. var name = prodname.name.toString(); //declare variable name to equal name of record in cmdb_ci table  
  12. var newprod = new GlideRecord('u_product');  
  13. newprod.addQuery('u_name',name); //query for name found in cmdb_ci table in the new product table  
  14. newprod.query();  
  15. while(newprod.next()){  
  16. gs.log('sys id of product name: ' + newprod.sys_id); //this works great  
  17. req.autoSysFields(false);  
  18. req.setWorkflow(false);  
  19. 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
  20. req.update();  
  21. }  
  22. }  
  23. }  

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?


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)


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.