- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-31-2018 01:44 PM
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');
}
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-01-2018 10:48 AM
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;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-04-2018 06:25 AM
@JohnsonJohn,
This works perfectly in my instance.
- are you in a scoped app?
- What version is your instance on?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-04-2018 06:47 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-04-2018 08:04 AM
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');
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-04-2018 09:08 AM
@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');
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-04-2018 12:15 AM
This is i think work successfully you can use it!