ServiceNow Reference Lists: Don’t make this mistake
When working with ServiceNow, it's essential to understand how reference lists operate.
A simple task can have unintended consequences if you're not careful.
Lets see this on an example.
For 659 groups given in excel file add new type of “approval”.
Use ctrl + shift + down arrow to mark all groups.
Then copy ctrl + C.
And then copy the query.
We will need it as encodedQuery.
First lets store in variable our encoded query as it is quite long (659 groups)
As “type” field in “Groups” table is a reference list, we will need sys_id of “type” record to be sure we selected exactly record we want.
For that I go to “sys_user_group_type.LIST” and copy sys_id of “approval”:
Well the answer is NO.
There is quite big mistake in this code.
Do you see what can it be?
Have a look first and we will explore it in the next slide.
It’s the line 17. The one I highlighted.
Reference List stores reference to records in other table.
By writting: “grGroup.type = newType;” we just replaced all what was currently there with new value instead of adding it.
On Slide 4 you can see that there was already existing type.
Now it got replaced.
We need to:
1. Store existing values in a variable.
2. Add new type to the list.
3. Set new constructed list as the “type”
This script on next slide will do that.
The Script I provided on Slide 15 is decent, but still it lacks something crucial for such task.
Can you guess what is that ?
Let me know in the comments.
Certainly it can also be written in other ways, let me know if you would like to see them.
List mutability is a fundamental concept in programming.
Overlooking this can lead to data loss or incorrect data representation.
Always be attentive to how lists work in ServiceNow and avoid making this mistake.