- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2024 08:00 AM
Hi,
I've got a requirement to update the field 'u_region_2' on our change request form with the region of the business service. I'm trying to write a script but I've never written anything with an array, and I'm super confused. To outline this a bit more clearly:
- On the change request form, there is a 'business_service' field which is a reference to the table cmdb_ci_service.
- On the business service form (i.e. on the table cmdb_ci_service) there is a 'u_region_2' field. This is a list that references the table u_user_region. So this field could contain the regions UK, Netherlands and Spain, for example.
- On the change request form, there is another field also called 'u_region_2' (not ideal to have two fields on different tables with the same name, I know). This is again a list field that references the u_user_region table.
- When the field 'business_service' is changed on the change request form, I need an array created that stores the regions related to that business service. Then, the 'u_region_2' field on the change request form should be updated with the results of this array.
Does anyone have any idea of a script that could do this? I am so lost!
Thanks,
Lyndsey
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2024 08:24 AM
I assume u_region_2 field on respective table refers to same table i.e. "u_user_region"
you can use 2 ways to achieve this.
1) onChange client script with getReference() callback method to see in real-time the field gets populated
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}
if(newValue == '')
g_form.clearValue('u_region_2');
if(oldValue != newValue)
var ref = g_form.getReference('business_service', callBackMethod); // requestedFor variable name
}
function callBackMethod(ref){
if(ref.u_region_2)
g_form.setValue('u_region_2', ref.u_region_2);
}
OR
2) before insert/update BR with condition as Business Service Changes -> this will reflect when record is saved
(function executeRule(current, previous /*null when async*/) {
// Add your code here
current.u_region_2 = current.business_service.u_region_2.toString();
})(current, previous);
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2024 08:24 AM
I assume u_region_2 field on respective table refers to same table i.e. "u_user_region"
you can use 2 ways to achieve this.
1) onChange client script with getReference() callback method to see in real-time the field gets populated
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}
if(newValue == '')
g_form.clearValue('u_region_2');
if(oldValue != newValue)
var ref = g_form.getReference('business_service', callBackMethod); // requestedFor variable name
}
function callBackMethod(ref){
if(ref.u_region_2)
g_form.setValue('u_region_2', ref.u_region_2);
}
OR
2) before insert/update BR with condition as Business Service Changes -> this will reflect when record is saved
(function executeRule(current, previous /*null when async*/) {
// Add your code here
current.u_region_2 = current.business_service.u_region_2.toString();
})(current, previous);
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2024 08:39 AM
This doesn't work I'm afraid. The region is a list field so I need an array. I don't believe I can update without an array being stored and then used to update the field.
I wouldn't use a client script as that's really bad practice - it would slow down loading times of the pages and wouldn't work very well on those with slow phones, slow browser latency etc.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2024 11:09 AM
@LyndseySharpe Ideally, the business rule suggested by Ankur should work as even if u_region_2 is a list field on business service, it should still return a comma separated list of sys_ids which can be directly assigned to u_region_2 field on the change_request record without needing a separate array variable.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-01-2024 12:19 AM
Ah sorry, this was me being really dim! There was a typo in the script, so I fixed that which solved the problem. Thanks!