Skip to content

Commit f8120aa

Browse files
authored
Detect oldValue, newValue and Operation type in Glide List (#1763)
* detectOldValuenewValueOperation.js This onChange Client script comes handy when dealing with Glide List type fields where we have to detect whether the value was added or removed and return the sysIDs of items which was added/removed. * readme.md This onChange Client script comes handy when dealing with Glide List type fields where we have to detect whether the value was added or removed and return the sysIDs of items which was added/removed. * detectOldValuenewValueOperation.js This onChange Client script comes handy when dealing with Glide List type fields where we have to detect whether the value was added or removed and returns the display name of users who were added/removed along with name of operation performed. * readme.md This onChange Client script comes handy when dealing with Glide List type fields where we have to detect whether the value was added or removed and returns the name of users who were added/removed. * scriptUtil.js This script include handles the sysIds(oldValue & newValue) of users and returns the display name * readme.md This onChange Client script comes handy when dealing with Glide List type fields where we have to detect whether the value was added or removed and return the display name of users who were added/removed. * watchListCandidatesUtil.js This script include handles the sysIds(oldValue & newValue) of users and returns the display name * detectOldValuenewValueOperation.js This onChange Client script comes handy when dealing with Glide List type fields where we have to detect whether the value was added or removed and returns the display name of users who were added/removed along with name of operation performed. * readme.md This onChange Client script comes handy when dealing with Glide List type fields where we have to detect whether the value was added or removed and returns the display name of users who were added/removed along with name of operation performed. * watchListCandidatesUtil.js This script include handles the sysIds(oldValue & newValue) of users and returns the display name
1 parent d4b8154 commit f8120aa

File tree

3 files changed

+119
-0
lines changed

3 files changed

+119
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
2+
if (isLoading) {
3+
return;
4+
}
5+
6+
var prevValue;
7+
if (g_scratchpad.prevValue == undefined)
8+
prevValue = oldValue;
9+
else {
10+
prevValue = g_scratchpad.prevValue;
11+
}
12+
g_scratchpad.prevValue = newValue;
13+
14+
15+
var oldGlideValue = prevValue.split(',');
16+
var newGlideValue = newValue.split(',');
17+
18+
var operation;
19+
20+
if (oldGlideValue.length > newGlideValue.length || newValue == '') {
21+
operation = 'remove';
22+
} else if (oldGlideValue.length < newGlideValue.length || oldGlideValue.length == newGlideValue.length) {
23+
operation = 'add';
24+
} else {
25+
operation = '';
26+
}
27+
28+
var ajaxGetNames = new GlideAjax('watchListCandidatesUtil');
29+
ajaxGetNames.addParam('sysparm_name', 'getWatchListUsers');
30+
ajaxGetNames.addParam('sysparm_old_values', oldGlideValue);
31+
ajaxGetNames.addParam('sysparm_new_values', newGlideValue);
32+
ajaxGetNames.getXMLAnswer(function(response) {
33+
34+
var result = JSON.parse(response);
35+
36+
g_form.clearMessages();
37+
g_form.addSuccessMessage('Operation Performed : ' + operation);
38+
g_form.addSuccessMessage('OldValue : ' + result.oldU);
39+
g_form.addSuccessMessage('NewValue : ' + result.newU);
40+
41+
});
42+
43+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
In Client Scripts, oldValue will display the value of last value/record which is stored in that field.
2+
For new records, it is generally empty and for existing records it displays the value which is stored after load.
3+
If we will try to change the value in that field, it will still show oldValue the same value which was there during the load of form.
4+
5+
So, In order to identify the oldValue on change(as it does in business rule(previous)), This script comes handy and also it will
6+
detect the action performed.
7+
8+
9+
This onChange Client script comes handy when dealing with Glide List type fields where we have to detect whether the value was added or removed and returns the display name of users who were added/removed along with name of operation performed.
10+
11+
Setup details:
12+
13+
onChange client Script on [incident] table
14+
Field Name: [watch_list]
15+
Script: Check [detectOldValuenewValueOperation.js] file
16+
Script Include Name: watchListCandidatesUtil (client callable/GlideAjax Enabled - true), Check [watchListCandidatesUtil.js] file for script
17+
18+
19+
20+
Output:
21+
22+
Currently there is no one in the watchlist field:
23+
24+
<img width="873" height="298" alt="image" src="https://github.com/user-attachments/assets/a46ca53a-f031-4bf9-9f85-2056c408b66b" />
25+
26+
27+
Adding [Bryan Rovell], It shows the operation was addition, oldValue as 'No record found' as there was no value earlier.New Value shows the name of the user (Bryan Rovell)
28+
<img width="870" height="443" alt="image" src="https://github.com/user-attachments/assets/484284b6-846e-424c-b9c8-a53278f48c72" />
29+
30+
31+
Adding 2 users one by one:
32+
33+
<img width="987" height="484" alt="image" src="https://github.com/user-attachments/assets/35dfe96a-c932-4f95-9c8e-bdb48b1c7b5f" />
34+
35+
36+
Removing 2 at once:
37+
38+
<img width="879" height="496" alt="image" src="https://github.com/user-attachments/assets/c83d4e01-f150-44cb-9078-9841072ec949" />
39+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
var watchListCandidatesUtil = Class.create();
2+
watchListCandidatesUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {
3+
4+
5+
getWatchListUsers: function() {
6+
7+
var oldUsers = this.getParameter('sysparm_old_values');
8+
var newUsers = this.getParameter('sysparm_new_values');
9+
10+
var result = {
11+
oldU: this._getUserNames(oldUsers),
12+
newU: this._getUserNames(newUsers)
13+
};
14+
15+
return JSON.stringify(result);
16+
},
17+
18+
19+
_getUserNames: function(userList) {
20+
var names = [];
21+
22+
var grUserTab = new GlideRecord('sys_user');
23+
grUserTab.addQuery('sys_id', 'IN', userList);
24+
grUserTab.query();
25+
if (grUserTab.hasNext()) {
26+
while (grUserTab.next()) {
27+
names.push(grUserTab.getDisplayValue('name'));
28+
}
29+
return names.toString();
30+
} else {
31+
return 'No record found';
32+
}
33+
},
34+
35+
36+
type: 'watchListCandidatesUtil'
37+
});

0 commit comments

Comments
 (0)