We're reclaiming inactive PDIs to keep them available for active builders. Learn what's changing, who's affected, and how to protect your work. Read More

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

@JohnsonJohn,

This works perfectly in my instance.

- are you in a scoped app? 
- What version is your instance on?

Give this a go and see what happens:

var reqFor = window.document.getElementById('sys_display.'+ g_form.getControl('requested_for').id).value;

var reqBy = window.document.getElementById('sys_display.'+ g_form.getControl('requested_by').id).value;

^This is what we call DOM and it is not best practice to use at all, I am just curious if this works for you. 

Hi Nate, @Shweta gave me a script that is working.  Here it is:

 

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.getReference('requested_for').name;
var reqBy = g_form.getReference('requested_by').name;

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

var vdiuser;

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');
}


}

@Nate, thanks a million for your help with this.  Below is the final script that you advised me to use and it is working correctly:

 

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 = '';
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;
}

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');
}


}

This is i think work successfully you can use it!