Auto populate catalog item field from client script

Kai Tingey
Tera Guru

Hi All

i'm trying to write a client script for a catalog item to populate a reference field, based on another reference field. Basically i want to take the selected user in the u_requested_for field, look up their computer from the cmdb_ci_computer table, and then populate the u_cmdb_ci field on the form with that value.

I have:

(on change client script, based on u_requested_for variable)

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}

var user = g_form.getValue('u_requested_for');

var gr = new GlideRecord('cmdb_ci_computer');
gr.addQuery('assigned_to', user);
gr.query();
if(gr.next()){
g_form.setValue('u_cmdb_ci',gr.sys_id);
}

}

a variation of this script works elsewhere, where we are populate an incident record's ci value based on who is in the caller field. however on this catalog item when trying to do it with the form values it's not working. In that case it's part of the record producer so clearly i'm doing it wrong here or not understanding the difference in how scripts work in different places.

any help to steer me in the right direction would be appreciated.

1 ACCEPTED SOLUTION

Lets do GlideAjax then 🙂

Add this onChange Script in the Requested by:

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}
	
	//Type appropriate comment here, and begin script below
	
	var gajax = new GlideAjax('CMDBUtils');
	gajax.addParam('sysparm_name', 'getCIDetails');
	gajax.addParam('sysparm_user', newValue);
	gajax.getXML(setCIDetails);
	
	function setCIDetails(serverResponse) {
		var cc = serverResponse.responseXML.documentElement.getAttribute("answer");
		g_form.setValue('computer', cc);
		
		
	}
	
}

And this script include:

var CMDBUtils = Class.create();
CMDBUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	getCIDetails: function () {
		var usr = this.getParameter('sysparm_user');
		var gr = new GlideRecord('cmdb_ci');
		gr.addQuery('assigned_to',usr);
		gr.query();
		
		if (gr.next()) {
			return gr.sys_id;
			
		}
		else {
			return '';
		}
		
		
	},
	
	
	type: 'CMDBUtils'
});

 

find_real_file.png

Let me know how you go.

 

Regards,

Raf

View solution in original post

11 REPLIES 11

I see, you best bet then if an onChange script. the only caveat is when a user is assigned to multiple CI's, which then do you use as default?

Can you try this on your onChange Script under Requested For field:

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '')
		return;
	
	var gr = new GlideRecord('cmdb_ci_computer');
	gr.addQuery('assigned_to', newValue);
	gr.addQuery('sys_class_name', 'cmdb_ci_computer');
	gr.query();
	
	if(gr.next()){
		g_form.setValue('u_cmdb_ci',gr.sys_id);
	}
	
	
}

 

find_real_file.png

no joy unfortunately.

find_real_file.png

find_real_file.png

Lets do GlideAjax then 🙂

Add this onChange Script in the Requested by:

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}
	
	//Type appropriate comment here, and begin script below
	
	var gajax = new GlideAjax('CMDBUtils');
	gajax.addParam('sysparm_name', 'getCIDetails');
	gajax.addParam('sysparm_user', newValue);
	gajax.getXML(setCIDetails);
	
	function setCIDetails(serverResponse) {
		var cc = serverResponse.responseXML.documentElement.getAttribute("answer");
		g_form.setValue('computer', cc);
		
		
	}
	
}

And this script include:

var CMDBUtils = Class.create();
CMDBUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	getCIDetails: function () {
		var usr = this.getParameter('sysparm_user');
		var gr = new GlideRecord('cmdb_ci');
		gr.addQuery('assigned_to',usr);
		gr.query();
		
		if (gr.next()) {
			return gr.sys_id;
			
		}
		else {
			return '';
		}
		
		
	},
	
	
	type: 'CMDBUtils'
});

 

find_real_file.png

Let me know how you go.

 

Regards,

Raf

thank you so much, that got it! just needed to make some tweaks to the form values for my variable names.