The CreatorCon Call for Content is officially open! Get started here.

Business rule to update a list on a change request form

LyndseySharpe
Tera Contributor

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

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@LyndseySharpe 

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.

 

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

@LyndseySharpe 

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.

 

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

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. 

@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.

LyndseySharpe
Tera Contributor

Ah sorry, this was me being really dim! There was a typo in the script, so I fixed that which solved the problem. Thanks!