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

dvp
Mega Sage
Mega Sage

By looking at the screenshot Name field looks like a Reference type not a Single line text.

For the reference field we can only set values by using a sys_id value

That is correct and in my script below the screenshot I am setting the Name field to 'requested_for.name'

Try the below script

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}
	
	var requested_for = g_form.getValue("requested_for");
	
	var tcra = g_form.getValue('tcra');
	
	if(tcra == 'true' && requested_for !=''){
		g_form.setValue('user_name', requested_for);
	}	
}

randrews
Tera Guru

unless things have changed getReference isn't eligible for mobile use.. works fine in classic but shouldn't work on the portal.

 I take that back it will now work but only in a call back as an asynch call..

 

https://docs.servicenow.com/bundle/kingston-application-development/page/script/client-scripts/reference/r_MobilePlatformMigrationImpacts.html

so it SHOULD work..howeever you need to mark get name as a function so

var requested_for = g_form.getReference("requested_for",getName

should be var requested_for = g_form.getReference("requested_for",getName()