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

Yes I also did that, here's my Script Include -

 

tindiz_0-1727620067092.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);
    }
}

Hello @tindiz I have tried to achieve the similar requirement using sys_user table where I have checked if the user  we are trying to deactivate is a manager for any other user. You can modify the same thing based on your requirement in the addencodedQuery section mentioned in script include.

 

Here is what client script looks like:

 

user deactivation.png

 

Code in client script:

 

function onCellEdit(sysIDs, table, oldValues, newValue, callback) {
    var saveAndClose = true;
    //Type appropriate comment here, and begin script below

    if (newValue == 'false' && g_user.hasRole('sn_customerservice_manager')) {

        var gaUser = new GlideAjax('Confirmation_check');
        gaUser.addParam('sysparm_name', 'userInUse');
        gaUser.addParam('sysparm_id', sysIDs);
        gaUser.getXMLAnswer(myCallBack);
    }

    function myCallBack(response) {

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

 

The Script include is called which looks like this:

 

script include user deactivation.png

 

Code in script include:

 

var Confirmation_check = Class.create();
Confirmation_check.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    userInUse: function() {
        var sysIDs = this.getParameter('sysparm_id');
        var grUser = new GlideRecord('sys_user');
        grUser.addencodedQuery('manager.sys_id=' + sysIDs);
        grUser.query();
        return grUser.next();
    },
    type: 'Confirmation_check'
});
 
Please modify the table name and query parameters based on your requirements.
I hope this helps and answer your query.
 
Thanks & Regards
Juhi Poddar

briannice
Kilo Sage

Hi @tindiz 

 

1. You need to make sure that your script include is client callable. You can do this by checking the 'Client callable' checkbox on the script include record. This will make sure the script include extends the AbstractAjaxProcessor class.

var UserAssignmentChecker = Class.create();
UserAssignmentChecker.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    // ....
    type: 'UserAssignmentChecker'
});

 

2. I also think that you should add the 'OR' condition on one line in the query (but I could be wrong about this). See the code below.

var UserAssignmentChecker = Class.create();
UserAssignmentChecker.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	intialize: function () {},

	hasAssignments: function (userId) {
		gr = new GlideRecord("customer_account");
		gr.addQuery("primary_contact", userId).addOrCondition("u_authrorised_approvers", userId);
		gr.query();
		return gr.hasNext();
	},

    type: 'UserAssignmentChecker'
});

 

Everything else looks fine at first sight. You could maybe do a 'console.log(response)' for some debugging.

 

Let me know if this works.

 

Kind regards,

Brian