Pass values from a variable set using a client script

billi_lumley
ServiceNow Employee
ServiceNow Employee

I'm trying to use a variable set to capture basic contact information (requested_for, phone, email) to apply to multiple catalog items. I want to be able to pass these values onto the Requested Item that is created (at least the requested_for). I created the following catalog client script that applies to the variable set, however it is just not working.

When I open the item; the requested_for will default to the person logged in. However, if I change that value to someone else...it is not passed along onto the RITM. Any help is appreciated.

function onChange(control, oldValue, newValue, isLoading) {
if(!isLoading){
var userid = g_user.userName;
var newid = g_form.getValue('requested_for');
var cart = new GlideRecord('sc_cart');
cart.addQuery('user.user_name', userid);
cart.query();
if (cart.next()) {
cart.requested_for = newid;
cart.update();
}
}
}

6 REPLIES 6

justin_drysdale
Mega Guru

I haven't had to use a client script for this, and I use a similar 'user info' variable set on my catalog items. Do you have cascading variables turned on on your Order Guide?


It's not on an order guide...rather on single items that produce a single request with one RITM.


neetusingh
Giga Guru

I am just curious to know, why are you using Client script to set the value of requested_for. You must have the workflow running for your request items. If you have a workflow running on your requested_item table, put the following script in the run script activity in your workflow itself just after the begin box.. It will set the requested_for value.

var reqRecord = current.request.getRefRecord();
reqRecord.requested_for = current.variable_pool.requested_for;
reqRecord.update();

I have done it on demo21 : https://demo021.service-now.com/com.glideapp.servicecatalog_cat_item_view.do?sysparm_id=10b44eb6c6112276005acf13b5c13274&sysparm_link_parent=109cdff8c6112276003b17991a09ad65

I hope this would help!!!

Regards/Neetu


Neetu...I was using a client script as I was not sure as to what was the best approach. I wanted to avoid using multiple scripts for each item, however using the workflow seems to work out great! Thanks for your help!!!