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.

Need to set reference field value in variable set on catalog task with business rule

Peter170
Tera Contributor

Hi,

I need to set the value of a reference field that is part of a variable set using a business rule. I know that the syntax for a non reference field would look something like current.variables.(variable set name).(field name) = x

How would I incorporate setValue to make this work for a reference field?

Here's what I have now - current.variables.fsa_add_access.setValue('u_sn_account', current.u_requested_for_s_name);

In another part of the business rule I am able to set a reference field that is not part of the variable set easily with current.setValue('u_requested_for_s_name', user.sys_id);

Thank you!

4 REPLIES 4

Brad Bowman
Kilo Patron
Kilo Patron

Hi Peter,

If the variable set is a single row, treat it the same as any other variable - no need to specify the variable set name to read or set a value - current.variables.variable_name = value, or current.setValue('variable_name', value).  If you're trying to update a variable in a multi-row variable set, that's a whole different animal.  I can provide some guidance on that once you confirm. 

Thank you for your response!

I tried to do that and am still not seeing the field get set. Below is the whole business rule: 

(function executeRule(current, previous /*null when async*/) {
	
		//gs.addInfoMessage("running, current.variables.u_sn_account: " + current.variables.u_sn_account);
		var user = new GlideRecord('sys_user');
		user.addQuery('name', current.u_requested_for_stringq);
		user.query();
		//gs.info('NAME: ' + current.u_requested_for_stringq);
		if(user.next()){
			if(current.u_requested_for_s_name == '') {
				current.setValue('u_requested_for_s_name', user.sys_id);
			}
// 			if(current.short_desctiption == "The Division Manager or COR needs to fill out the necessary information to add User's FSA Access"){
				current.setValue('u_sn_account', user.sys_id);
				gs.info("CHECK: " + current.variables.fsa_add_access.u_sn_account);
			//}
			current.update();
		}
	

})(current, previous);

Note that my code for setting the value for 'u_requested_for_s_name' is working, but the code for setting the variable set value of 'u_sn_account' is not working. This is on a service catalog task - the business rule is running on the sc_task table. 

And sorry forgot to mention that it is indeed a single row set 

Is the 'u_sn_account' variable a reference type, referencing the sys_user table, are you certain the name is exactly correct - case-sensitive and no extraneous leading or trailing spaces, and is it included/shown on the Catalog Task when this BR is executing?  If the variable isn't included on this Catalog Task you can use (or maybe try this anyway)

current.request_item.variables.u_sn_account = user.sys_id;  

Also, is u_requested_for_s_name a field on the sc_task table, or a variable defined within the Catalog Item?  I don't think I've ever used the current.setValue method on variables, so also try this after confirming all of the above

current.variables.u_sn_account = user.sys_id;