OnChange Client Script in Service Portal

Brett14
Giga Expert

All,

My script below isn't working in the Service Portal.  It is just a simple getReference with a callback to set the name of Requested For field.

Basically if TCRA is TRUE then set the name field to what is in the Requested For field.  Seems simple enough, but Name field is coming up blank.  Any help would be appreciated as I am not sure what I am missing here.

find_real_file.png

 

The client script has the UI type set to 'All'

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

var requested_for = g_form.getReference("requested_for",getName);
}

function getName(requested_for){
var tcra = g_form.getValue('tcra');
if(tcra == 'true' && requested_for !=''){
g_form.setValue('user_name', requested_for.name);
}
}

1 ACCEPTED SOLUTION

Brent Sutton
Mega Sage

Hi Brett,

What type of variable is "user_name"? Your screenshot looks like you are using a reference variable which would normally be set with the sys_id of the record you are returning in your script. If you actually want the requested_for "user_name" from the sys_user table then you should amend the "user_name" variable to a type Single Line Text and use the following code:

find_real_file.png

Code Example:

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading)
      return;

   if (newValue == '' || newValue == null) {
      g_form.setValue('user_name', ''); //clear the user name variable if newValue is blank
      return;
   }
   var requested_for = g_form.getReference("requested_for",setName); //getReference with call back.
}

function setName(requested_for) {
	
	var tcra = g_form.getValue('tcra');
   /*if a record is returned set the user_name variable using the user_name field from the sys_user table*/
	if (requested_for && tcra == "true")
       g_form.setValue('user_name', requested_for.user_name); //user_name variable should be type - string
}

Let me know if this worked for you.

Brent

P.S. If my suggestion helped then please mark as helpful and/or correct so other community members can benefit from this information. 

View solution in original post

11 REPLIES 11

Jim Coyne
Kilo Patron

Assuming the Client Script is on the checkbox variable, the simple, easy way to do it:

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

	if (newValue == "true"){
		var requestedFor = g_form.getValue("requested_for");
		if (requestedFor != "" && requestedFor != g_form.getValue("user_name")) {
			g_form.setValue("user_name", requestedFor);
		}
	}
}

Using getReference with a call back function:

function onChange(control, oldValue, newValue, isLoading) {
	function _showResults(gr) {
		g_form.setValue("user_name", gr.sys_id, gr.name);
	}

	if (isLoading) {
		return;
	}

	if (newValue == "true"){
		var requestedFor = g_form.getValue("requested_for");
		if (requestedFor != "" && requestedFor != g_form.getValue("user_name")) {
			g_form.getReference("requested_for", _showResults);
		}		
	}
}

Some will argue you need to use a GlideAjax call, but I'm not sure the overhead is worth it.  Would be different if you were displaying multiple pieces of data.  The first method is the simplest and probably the least expensive in terms of execution as only the display name of the User record needs to be retrieved from the server in order to display the value in the reference variable (done behind the scene).  The second method with getReference will actually bring back all the fields associated to the Requested for's User record, even though you don't need the info.  It would, however, be best for the user experience in the case there's a delay in retrieving the data.

Hi Jim,

 

Thank you for the reply back and your examples resolved my issue once i changed the reference field to a single text field as Brent stated in his post.