Script Include is being blocked

tindiz
Giga Guru

Hello everyone, so I created a Script Include to check if a user is assigned on the fields primary_contact or u_authorised_approvers in customer_account table. This is how it looks like -

 

tindiz_1-1727698996511.png

 

And then I created a Client Script - this script is designed to handle edits in a customer_contact table by confirming user deactivation actions and checking if the user is assigned in specific roles.

 

tindiz_2-1727699174823.png

Here's the complete script 

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

    // Type appropriate comment here, and begin script below
    if ((newValue == false || newValue == 'false') && g_user.hasRole('sn_customerservice_manager')) {
        var confirmation = confirm('Are you sure you want to deactivate this user? This action cannot be undone.');
        if (confirmation) {
            saveAndClose = true;
        }
    }

    // Check if the user is assigned as primary_contact or in u_authorised_approvers
    var userIdToCheck = g_user.sys_id; // Assuming you want to check the current user's sys_id

    // Create a new GlideAjax object
    var ga = new GlideAjax('UserAccountChecker');
    ga.addParam('sysparm_name', 'isUserAssigned');
    ga.addParam('sysparm_user_id', userIdToCheck); // Pass the user sys_id

    // Call the Script Include
    ga.getXMLAnswer(function(response) {
        var isAssigned = response === 'true';
        if (isAssigned) {
            alert('This user is assigned as primary_contact or in u_authorised_approvers.');
            // Optionally, you could prevent further actions here
        } else {
            // Proceed with the original logic
            callback(saveAndClose);
        }
    });

    // Return false to avoid calling callback immediately
    return false;
}

 

Now, whenever I tried to change the active field from true to false in customer_contact table, I am getting the confirmation message which is expected but not the alert message and I am getting the below message stating that my Script Include is being blocked.

 

tindiz_0-1727698976783.png

I tried to create an ACL as show below but it didn't help resolving the issue. 

P.S. I just deactivated it for the meantime, but it was active when I was testing it. 

Please help.

 

tindiz_3-1727699515264.png

 

 

4 REPLIES 4

Sandeep Rajput
Tera Patron
Tera Patron

@tindiz Instead of creating the ACL in Global scope, you should instead create it in the Customer Service Scope. Once created the ACL related issue will not appear. 

Brad Bowman
Kilo Patron
Kilo Patron

Since your Client Script is in a different scope than the Script Include, you need to add the application scope to the GlideAjax call

var ga = new GlideAjax('global.UserAccountChecker');

Your Script Include is marked Client callable, but this must have been checked after the script was populated, so the first line does not include extended object

var UserAccountChecker = Class.create();
UserAccountChecker.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	isUserAssigned: function() {
     ...
    },
    type: 'UserAccountChecker'
});

Hello, I followed your advice, I fixed my scripts and this is how it look like now -

SCRIPT INCLUDE:

tindiz_0-1727701874479.png

And then I updated my Client Script:

tindiz_1-1727701943463.png

But when I tested it, I am still getting the confirmation message which is expected, no longer getting the Script Include blocked message but the alert message is still not coming up. I am not sure which part is not making it work as expected.

Here's the complete Client Script:

tindiz_2-1727702072946.png

 

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

    // Type appropriate comment here, and begin script below
    if ((newValue == false || newValue == 'false') && g_user.hasRole('sn_customerservice_manager')) {
        var confirmation = confirm('Are you sure you want to deactivate this user? This action cannot be undone.');
        if (confirmation) {
            saveAndClose = true;
        }
    }

    // Check if the user is assigned as primary_contact or in u_authorised_approvers
    var userIdToCheck = g_user.sys_id; // Assuming you want to check the current user's sys_id

    // Create a new GlideAjax object
    var ga = new GlideAjax('global.UserAccountChecker');
    ga.addParam('sysparm_name', 'isUserAssigned');
    ga.addParam('sysparm_user_id', userIdToCheck); // Pass the user sys_id

    // Call the Script Include
    ga.getXMLAnswer(function(response) {
        var isAssigned = response === 'true';
        if (isAssigned) {
            alert('This user is assigned as primary_contact or in u_authorised_approvers.');
            // Optionally, you could prevent further actions here
        } else {
            // Proceed with the original logic
            callback(saveAndClose);
        }
    });

    // Return false to avoid calling callback immediately
    return false;
}

 

In the Script Include you need to get the parameter like this, not in an argument in the function declaration

var userId = this.getParameter('sysparm_user_id');

For more troubleshooting, add some gs.info lines to the Script Include to confirm it is running and see how far it is getting.

You should also review this excellent guide on GlideAjax

https://www.servicenow.com/community/developer-articles/glideajax-example-cheat-sheet-updated/ta-p/2...