Client Script: User Assignment Checker

tindiz
Giga Guru

I have a Client Script here where a confirmation message pops-up before proceeding with the user deactivation.

Now, how can I add the following requirement on the script?

If the user being deactivated is the assigned user on the "primary_contact" or "u_authorised_approvers" field on "customer_account" table then an alert message should show up asking to get the assigned user replaced on the mentioned fields first before proceeding with the deactivation.

They cannot proceed with deactivating it unless the user assigned to these fields are replaced.

 

tindiz_0-1727615475573.png

 

7 REPLIES 7

tindiz
Giga Guru

I tried to use the below script but it did not work -

 

function onCellEdit(sysIDs, table, oldValues, newValue, callback) {
var saveAndClose = false;

if ((newValue == false || newValue == 'false') && g_user.hasRole('sn_customerservice_manager')) {
var userID = sysIDs[0]; // Assuming you're deactivating a single user
var userInUse = false;

// Check if user is assigned to "primary_contact" or "u_authorised_approvers"
var gr = new GlideRecord('customer_account');
gr.addQuery('primary_contact', userID);
gr.addOrCondition('u_authorised_approvers', userID);
gr.query();

if (gr.next()) {
userInUse = true;
}

if (userInUse) {
alert('Please replace the assigned user in the primary_contact or u_authorised_approvers fields before deactivation.');
} else {
var confirmation = confirm('Are you sure you want to deactivate this user? This action cannot be undone.');
if (confirmation) saveAndClose = true;
}
}

callback(saveAndClose);
}

tindiz_0-1727615955319.png

 

I even tried to create a Script Include

 

tindiz_0-1727618158163.png

And called that Script Include in my Client Script but still did not work as expected. Please help.

 

---

 

function onCellEdit(sysIDs, table, oldValues, newValue, callback) {
    var saveAndClose = false;

    if ((newValue == false || newValue == 'false') && g_user.hasRole('sn_customerservice_manager')) {
        var userId = sysIDs[0]; // Assuming sysIDs contains the user ID to deactivate

        // Call the Script Include to check assignments
        var ga = new GlideAjax('UserAssignmentChecker');
        ga.addParam('sysparm_name', 'hasAssignments');
        ga.addParam('sysparm_user_id', userId);
        ga.getXMLAnswer(function(response) {
            var hasAssignments = response === 'true';

            if (hasAssignments) {
                alert('This user cannot be deactivated because they are assigned as a primary contact or an authorized approver. Please replace them before deactivating.');
                callback(false);
            } else {
                var confirmation = confirm('Are you sure you want to deactivate this user? This action cannot be undone.');
                if (confirmation) {
                    saveAndClose = true;
                }
                callback(saveAndClose);
            }
        });
    } else {
        callback(false);
    }
}

Juhi Poddar
Kilo Patron

Hello @tindiz , 

 

It is not recommended to use glide record in client script, instead you can use script include called using glide ajax from client script to run server side scripting such as glide record. 

 

Thanks & Regards

Juhi Poddar