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

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. 

Hi Brent, thank you.  You know I just didn't think of it as being a single text field because the requirement sheet said reference. Most likely why it isn't working for as it is setting the sys_id in the reference field.  I guess I assumed it would just show the display value. 

No worries Brett, Glad I could help. If my reply answered your question then please mark as correct so this thread can be closed. Thanks in advance, Brent

changed the field and all is well in the world of coding.  Thanks Again.

Awesome Brett, glad you got it working 🙂