Alert message is not working
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-30-2024 10:24 AM
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;
}
If I try it on the user assigned as primary contact, the alert message will show up and the logs are showing correctly.
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-30-2024 11:04 AM - edited 09-30-2024 11:10 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-30-2024 11:31 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-30-2024 12:15 PM - edited 09-30-2024 12:30 PM
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?