How to access a catalog item variable in Script include?

Lucky18
Kilo Contributor

I created 2 catalog variables. one is referencing to 'sys_user' table, named as 'requested_for' and another one is referencing to 'alm_hardware' table, named as 'asset_name'.

Based on the user selected in 'requested_for' field the respective assets of the user should display in 'asset_name' field.

I've written a script include for that and tried to call it in advanced reference qualifier of the asset_name variable.

''var user=current.variables.requested_for"

I tried to access the requested_for variable as above, But I am unable to access the 'current' object in script include. 

So please give me a solution for this.

Thank you!

1 ACCEPTED SOLUTION

miked_jones
Giga Expert

Where are you trying to call your script include from? For a reference qualifier?

You can define your function in your script include to accept input, something like:

YourFunctionName: function(current) {}

And when you call the script include, pass current to the function. 

javascript: new YourScriptInclude().YourFunctionName(current);

 

Again, assuming you're calling this as a reference qualifier. 

 

View solution in original post

3 REPLIES 3

miked_jones
Giga Expert

Where are you trying to call your script include from? For a reference qualifier?

You can define your function in your script include to accept input, something like:

YourFunctionName: function(current) {}

And when you call the script include, pass current to the function. 

javascript: new YourScriptInclude().YourFunctionName(current);

 

Again, assuming you're calling this as a reference qualifier. 

 

Thank  you sir,its working now.

sachin_namjoshi
Kilo Patron
Kilo Patron

You can configure on change client script  on requested for variable to update asset_name for user based on requested_for.

You need to use Glideajax in your client script to  update asset_name based on requested for field.

 

Please use below sample code for client script and script include

 

Client Script:

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}
	
	if (oldValue != newValue) {
		
		
		var currentuser = g_form.getValue('requested_for');
		var ga = new GlideAjax('processUser');
		ga.addParam('sysparm_name', 'getManager');
		ga.addParam('sysparm_id', currentuser);
		ga.getXML(ProcessUserInfo);
	}
	function ProcessUserInfo(response) {
		var answer = response.responseXML.documentElement.getAttribute("answer");
		var returneddata = answer.evalJSON(true);
		g_form.setValue ('asset_name',returneddata.cost_center);

		
	}
}

Script Include:

var processUser = Class.create();
processUser.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	getManager: function() {
		
		var gr = new GlideRecord("sys_user");
		gr.addQuery("sys_id",this.getParameter('sysparm_id'));
		gr.query();
		if(gr.next()){
			if(gr.manager!=''){
				var json = new JSON();
				var object = {
					"manager" : gr.getValue("manager"),
					"manager_name": gr.getDisplayValue("manager.name"),
					"title": gr.getValue("title"),
					"phone": gr.getValue("phone"),
					"dept": gr.getDisplayValue("department"),
					"country": gr.getDisplayValue("u_country"),
					"cost_center": gr.getValue("cost_center"),
					"location": gr.getDisplayValue("location"),
					"vip": gr.getValue("vip"),
                                        "asset_name" : gr.getValue("asset_name"),  
					"Site_Code": gr.getValue("u_site_code")
				};
				var data = json.encode(object);
				return data;
				//return gr.manager;
			}else{
				return "";
			}
		}
	}
}};

 

Please date correct asset_name field from your sys_user table in script include.

 

Regards,

Sachin