Catalog client script returns sysID instead of user's name

johnsonjohn
Tera Contributor

Hi - I know there are several posts about this and I've read through most of them, but I don't know how to apply it to my use case.  I have a request form with 2 reference fields, requested_by and requested_for.  I created an onChange catalog client script that intends to show a message with the user's name in this field (i.e. if requested_for has a value, show that name, if it's blank show name in requested_by).  The value is stored in a variable called vdiuser.  However, using this script, vdiuser shows the sysID not the actual user's name.  I think I'm supposed to use a script include, but I'm not sure what that means and what the script should say.  Thanks in advance for your patience and guidance for this Service Now newbie.

 

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

//Type appropriate comment here, and begin script below
var deletetype = g_form.getValue('u_vdi_delete_type');
var reqFor = g_form.getValue('requested_for');
var reqBy = g_form.getValue('requested_by');
var vdiuser;

if(reqFor) {
vdiuser = reqFor;
}
else {
vdiuser = reqBy;
}

if (deletetype == 1) {
g_form.showFieldMsg('u_vdi_delete_type', '!!!! Warning: Choosing this option will delete ALL VDIs for ' + vdiuser + '. This action is irreversible !!!', 'error');
g_form.hideFieldMsg('u_vdi_delete_vdiname');
}

else {
g_form.showFieldMsg('u_vdi_delete_vdiname', 'Please note that the VDI name must be entered exactly correct for the deletion to occur', 'info');
g_form.hideFieldMsg('u_vdi_delete_type');
}


}

1 ACCEPTED SOLUTION

Sorry I assumed this was for Service Portal.

 

Try this instead will work for both.

 

 var reqFor = '';
 var reqBy = ''; 

if(window === null){
//run sp code
  reqFor = g_form.getDisplayValue('requested_for');
  reqBy = g_form.getDisplayValue('requested_by');
}else{
//run desktop code
  reqFor = g_form.getDisplayBox('requested_for').value;
  reqBy = g_form.getDisplayBox('requested_by').value;
}

View solution in original post

21 REPLIES 21

Hi Dylan, thanks for responding.  Unfortunately that doesn't seem to work either.  When I use the dot walk method as you suggested, it also returns undefined for the vdiuser value.

Nate23
Mega Guru

use this you won't regret it. 

 

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

//Type appropriate comment here, and begin script below
var deletetype = g_form.getValue('u_vdi_delete_type');
var reqFor = g_form.getDisplayValue('requested_for');
var reqBy = g_form.getDisplayValue('requested_by');
var vdiuser;

if(reqFor) {
vdiuser = reqFor;
}
else {
vdiuser = reqBy;
}

if (deletetype == 1) {
g_form.showFieldMsg('u_vdi_delete_type', '!!!! Warning: Choosing this option will delete ALL VDIs for ' + vdiuser + '. This action is irreversible !!!', 'error');
g_form.hideFieldMsg('u_vdi_delete_vdiname');
}

else {
g_form.showFieldMsg('u_vdi_delete_vdiname', 'Please note that the VDI name must be entered exactly correct for the deletion to occur', 'info');
g_form.hideFieldMsg('u_vdi_delete_type');
}


}

 

The key is using g_form.getDisplayValue('variable_name'); as that gets the references display value. getValue() gets the actual record value(unique value).

Cheers,

Nate

Hi Nate, I tried your suggestion and it returns nothing (not sysID nor undefined).  The vdiuser value appears to be blank when I use the .getDisplayValue method:

 

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

//Type appropriate comment here, and begin script below
var deletetype = g_form.getValue('u_vdi_delete_type');
var reqFor = g_form.getDisplayValue('requested_for');
var reqBy = g_form.getDisplayValue('requested_by');
var vdiuser;

if(reqFor) {
vdiuser = reqFor;
}


else {
vdiuser = reqBy;
}

Sorry I assumed this was for Service Portal.

 

Try this instead will work for both.

 

 var reqFor = '';
 var reqBy = ''; 

if(window === null){
//run sp code
  reqFor = g_form.getDisplayValue('requested_for');
  reqBy = g_form.getDisplayValue('requested_by');
}else{
//run desktop code
  reqFor = g_form.getDisplayBox('requested_for').value;
  reqBy = g_form.getDisplayBox('requested_by').value;
}

Still no luck, Nate.  In fact, someone had previously suggested trying this method (TheQueenIsDead, see above).  When I use .getDisplayBox, vdiuser returns undefined.  Appreciate your efforts in helping me get this resolved.