Alert message is not working

tindiz
Giga Guru

I created a Script Include where it checks if a user in customer_account table is assigned to either primary_contact or u_athorised_approvers field: 

 

var UserAssignmentChecker = Class.create();
UserAssignmentChecker.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    isUserAssigned: function() {
        var userId = this.getParameter('sysparm_user_id'); // Get user ID from parameters
        gs.info('Checking assignment for user ID: ' + userId); // Log user ID

        if (!userId) {
            gs.info('No user ID provided.');
            return false; // Return false if no user ID is provided
        }

        var customerAccountGr = new GlideRecord('customer_account');
        customerAccountGr.query();

        while (customerAccountGr.next()) {
            var primaryContact = customerAccountGr.primary_contact;
            var approvers = customerAccountGr.u_authorised_approvers ? customerAccountGr.u_authorised_approvers.split(',') : [];

            gs.info('Primary Contact: ' + primaryContact);
            gs.info('User to check: ' + userId);
            gs.info('Authorized Approvers: ' + approvers.join(', ')); // Log the approvers list

            if (primaryContact == userId) {
                gs.info('User is assigned as Primary Contact: ' + userId);
                return 'primary_contact'; // Return identifier for primary_contact
            }
            if (approvers.some(approver => approver.trim() === userId)) {
                gs.info('User is assigned as Authorized Approver: ' + userId);
                return 'u_authorised_approvers'; // Return identifier for u_authorised_approvers
            }
        }
        gs.info('User ' + userId + ' is not assigned.');
        return false; // User is not assigned
    }
});

// Make it client callable
UserAssignmentChecker.prototype.type = 'UserAssignmentChecker';

 

And then I created a Client Script where a confirmation message will show up if a user in customer_contact table is being deactivated and an alert message will show up if a user that is assigned as primary contact or authorised approver is being deactivated. 

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

    // Check if the user is assigned as primary_contact or in u_authorised_approvers
    var userIdToCheck = sysIDs[0]; 
    console.log('User ID to check: ' + userIdToCheck); // Log the user ID

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

    // Call the Script Include
    ga.getXMLAnswer(function(response) {
        console.log('Response from the Script Include: ' + response); // Log response

        // Show alert based on response
        if (response === 'primary_contact') {
            alert('This user is designated as the Primary Contact. Please update this before deactivation.');
            return; // Exit early if the user is assigned as primary_contact
        } else if (response === 'u_authorised_approvers') {
            alert('This user is designated as an Authorized Approver. Please update this before deactivation.');
            return; // Exit early if the user is assigned as u_authorised_approvers
        }

        // Proceed with deactivation logic if the user is not assigned
        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;
            }
        }

        // Callback with saveAndClose
        callback(saveAndClose);
    });

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

 

Now, it is working properly for the user assigned in primary_contact - if I try to deactivate the user assigned as a primary contact, the alert message will show up and will not let me deactivate it unless I replace it first.
 
However, it is not doing that same behaviour for user assigned in u_authorised_approvers.
I've added several logs but still not working.
 
The user is assigned as u_authorised_approvers but it is coming up as false.
 
tindiz_0-1727716876272.png

If I try it on the user assigned as primary contact, the alert message will show up and the logs are showing correctly.

 

tindiz_1-1727716935447.png

 

I am not sure if this has bearing but when I checked the fields, the primary_contact has a Reference as a type and u_authorised_approvers has List as a type.

 

Please help how to fix the alert for u_authorised_approvers... Thank you!

 

 

tindiz_2-1727717025717.png

 

3 REPLIES 3

Animesh Das2
Mega Sage

Hi @tindiz ,

 

In the SI when you log 'Authorized approvers', I hope it returns the array of approvers list. I am talking about this below line.

gs.info('Authorized Approvers: ' + approvers.join(', ')); // Log the approvers list

If so and the response is still coming as 'False' then it could be the below line is creating some issue. Just a guess.

if (approvers.some(approver => approver.trim() === userId))

 

Can you please try with a simplified 'If' condition as below instead? If you are just checking in the 'Approvers' array has the userID or not. If I understood correctly.

 

if (approvers.indexOf(userID) > -1)

{.........}

 

If this address your question, please mark this response correct by clicking on Accept as Solution and/or Kudos.

You may mark this helpful as well if it helps you.

Thanks, 

Animesh Das

Hi @Animesh Das2 I followed your advise but it is still coming up as false on the console log, and alert is still not showing up

 

tindiz_0-1727720979887.png

 

tindiz_2-1727721020068.png

 

Hi @tindiz ,

In the SI, when you log 'Authorized approvers', it returns the array of approvers list. just confirming again.

Also trying to understand little more, you did gliderecord query without any conditions on 'customer_account', hence for each record in that table you are checking both the conditions primary_contact or in approvers list. Are you getting multiple responses in console log for each record in 'customer_account' table or only one set of response for primary contact and authorized approvers?

 

Can you please check this behavior once?