Updating a list type Field that's also a reference field

kemmy1
Tera Guru

I'm working in a Business Rule and need to update a list field.  (which is also a reference field).

Is my syntax correct:

current.listfield = 'something';

or current.listfield = gr.components_impacted;    <-components impacted is another list reference field from another form that using the same table as a reference and usually just brings over the sys_id

or current.listfield = gr.components_impacted.getDisplayValue();

I can't get any of these to work.  Once I get it to work, I know that I'll have to possibly make sure it's empty first and if not just a "," and then my data.

But the first test just trying to get data into this field.  I know I'm missing something really dumb.  

The Business Rule is running, because if have another field that I'm updating (a text field) to make sure it's running.

Lisa

1 ACCEPTED SOLUTION

Daniele Songini
Tera Guru

Hi,

look at this

Updating a List field with a Business Rule

Please mark this as "Correct Answer" if I have given you a sufficient answer to your question.

Best Regards,
Daniele

View solution in original post

4 REPLIES 4

Brian O_Donnell
Tera Guru

It should just take a comma sep list of sys_id's.

Daniele Songini
Tera Guru

Hi,

look at this

Updating a List field with a Business Rule

Please mark this as "Correct Answer" if I have given you a sufficient answer to your question.

Best Regards,
Daniele

Michael Ritchie
ServiceNow Employee
ServiceNow Employee

Kemmy, list fields are basically a long string field that stores a comma separated list of SysIDs of records in the associated reference table.  So if you are updating this field via script, you need to "push" a SysID into that.  Good practice is to first split the current value into an array and check to make sure the value you are pushing into it doesn't already exist.

Example:

var valueToAdd = "123456";

if (current.listfield.toString().indexOf(valueToAdd) == -1) {
	var listfieldArray = current.listfield.toString().split(",");
	listfieldArray.push(valueToAdd);
	current.listfield = listfieldArray.join(",");
}

kemmy1
Tera Guru

Thank you all so much I took Michael's code and merged it into the Daniele linke (to get rid of dupes).