How to get OldValue in an onChange Catalog Client script

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-08-2021 07:12 AM
We have an OnChange catalog client script on a reference variable(let's call it 'xx_approver') to grab the email address of the chosen user and put in a string field (later used to hash out whom to inform when ticket closes, let's call it 'all_the_emails'). This string field receives email addresses from multiple variables on the record producer, and a Multi Row variable set.
What I want to do is remove email address from the string field, if someone accidently selects the wrong user, then changes it to the correct user. To get the correct email address to remove, I need to see the sys_id of the wrong user.
I can get hold of the newValue sys_id, but not the oldValue, it stays blank in the InfoMessage.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}
var findprevious = oldValue;
var findnew = newValue;
g_form.addInfoMessage('Oldvalue: ' + findprevious + ' NewValue: '+ findnew);
var gr = g_form.getReference("xx_approver", populateEmail);
}
function populateEmail(gr) {
var email = gr.email;
var currentemails = g_form.getValue('all_the_emails');
var finddupl = currentemails.includes(email);
var emailfield = currentemails + ', ' + email;
if(!finddupl){
g_form.setValue('all_the_emails', emailfield);
}
}
Would like some help to be able to grab the value.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-08-2021 07:22 AM
You can try using g_scratchpad-https://community.servicenow.com/community?id=community_question&sys_id=d5ae8521dbed2050a08a1ea66896199f
https://community.servicenow.com/community?id=community_question&sys_id=47bc4fe5db9cdbc01dcaf3231f9619b4
or creating some dummy field https://community.servicenow.com/community?id=community_question&sys_id=b2001671dba85f484816f3231f961903

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-08-2021 07:24 AM
Hi,
OldValue will have only the sys_id of previous value. If you are using Service Portal, then you can use GlideAjax and retreive the value. If you are not using Service Portal, then you can call GlideRecord directly in the client script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}
var findprevious = oldValue;
var findnew = newValue;
g_form.addInfoMessage('Oldvalue: ' + findprevious + ' NewValue: '+ findnew);
var gr = g_form.getReference("xx_approver", populateEmail);
var prevGr = new GlideRecord("sys_user");
prevGr.addQuery("sys_id",oldValue);
prevGr.query()
if(prevGr.next()){
var oldEmail = prevGr.email;
// Add your script here
}
}
function populateEmail(gr) {
var email = gr.email;
var currentemails = g_form.getValue('all_the_emails');
var finddupl = currentemails.includes(email);
var emailfield = currentemails + ', ' + email;
if(!finddupl){
g_form.setValue('all_the_emails', emailfield);
}
}
Palani

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-09-2021 12:49 AM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-10-2021 01:14 AM
Well, a colleague found the obvious reason this didn't work. Apparently OldValue is the value the field held on initial load, and not updated for each time the onChange client script runs (i.e. each time you change the selection).