Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Table

Ram012
Tera Contributor

Create one list type field reference to user table when someone remove any values from the list or add any users to the list fields , then it will shows the
alert/pop up with the added/removed users list to the end - user use your own table ?

 

Client Script:


function onLoad() {
// Get the initial value of the watchers field
var initialWatchers = g_form.getValue('u_watchers');
}

function onSubmit() {
// Get the current value of the watchers field
var currentWatchers = g_form.getValue('u_watchers');

// Find the added and removed users
var addedUsers = [];
var removedUsers = [];
for (var i = 0; i < currentWatchers.length; i++) {
if (initialWatchers.indexOf(currentWatchers[i]) == -1) {
addedUsers.push(currentWatchers[i]);
}
}
for (var i = 0; i < initialWatchers.length; i++) {
if (currentWatchers.indexOf(initialWatchers[i]) == -1) {
removedUsers.push(initialWatchers[i]);
}
}

// Show the alert/pop up with the added/removed users
if (addedUsers.length > 0 || removedUsers.length > 0) {
var message = 'Watchers updated:';
if (addedUsers.length > 0) {
message += '\nAdded: ' + addedUsers.join(', ');
}
if (removedUsers.length > 0) {
message += '\nRemoved: ' + removedUsers.join(', ');
}
alert(message);
}
}

 Write the code using client script but not working.

 

Any idea above scenaior code help me

1 REPLY 1

HIROSHI SATOH
Mega Sage

I have modified the script for your reference.

var initialWatchers;

function onLoad() {
    // u_watchersフィールドの初期値を配列として取得
    initialWatchers = g_form.getValue('u_watchers').split(',');
}

function onSubmit() {
    // u_watchersフィールドの現在の値を配列として取得
    var currentWatchers = g_form.getValue('u_watchers').split(',');

    var addedUsers = [];
    var removedUsers = [];

    // 追加されたユーザーを検出
    currentWatchers.forEach(function(user) {
        if (initialWatchers.indexOf(user) == -1 && user) {
            addedUsers.push(user);
        }
    });

    // 削除されたユーザーを検出
    initialWatchers.forEach(function(user) {
        if (currentWatchers.indexOf(user) == -1 && user) {
            removedUsers.push(user);
        }
    });

    // 追加/削除されたユーザーを含むアラートを表示
    if (addedUsers.length > 0 || removedUsers.length > 0) {
        var message = 'ウォッチャーが更新されました:';
        if (addedUsers.length > 0) {
            message += '\n追加: ' + addedUsers.join(', ');
        }
        if (removedUsers.length > 0) {
            message += '\n削除: ' + removedUsers.join(', ');
        }
        alert(message);
    }

    return true; // フォームの送信を許可
}