Dynamic alert/getMessage

servicenow14710
Tera Expert

Hello developers,

 I have an implementation where i need to send the variables i selected which stores string in array format to be shown on alert / get Message . This should be dynamic as i need to infr=orm the requestor of what he has choosen.

 

E.g: var a = ['test1', "test2","test3","test4"];

 

Alert should be like-     This test1, test 2 already exists in your profile.

 

 

Please suggest how to implement this . Any help is appreciated,

 

 

 

Thanks.

1 ACCEPTED SOLUTION

Viraj Hudlikar
Giga Sage

Hello @servicenow14710 

I am not sure with your exact requirement but here is something sample code, you can check and modify further as per your requirement.

// Array of roles the requester already has in their profile
var existingRoles = ['test1','test4','test3'];

// Array of roles selected in the request
var selectedRoles = ['test1', 'test2', 'test3', 'test4', 'test5'];

// Function to create the alert message
function createAlertMessage(existingRoles, selectedRoles) {
    var overlappingRoles = selectedRoles.filter(role => existingRoles.includes(role));
    if (overlappingRoles.length === 0) {
        return "No selected roles already exist in your profile.";
    }
    var message = "The following roles already exist in your profile: ";
    for (var i = 0; i < overlappingRoles.length; i++) {
        if (i === overlappingRoles.length - 1 && overlappingRoles.length > 1) {
            message += "and " + overlappingRoles[i];
        } else if (i === overlappingRoles.length - 2) {
            message += overlappingRoles[i] + " ";
        } else {
            message += overlappingRoles[i];
            if (i < overlappingRoles.length - 2) {
                message += ", ";
            }
        }
    }
    message += ".";
    return message;
}

// Display the alert
gs.print(createAlertMessage(existingRoles, selectedRoles)); //since i tried in background script i used gs.print you can use alert if you are doing this in client script.

 

If my response has helped you hit helpful button and if your concern is solved do mark my response as correct.

Thanks & Regards
Viraj Hudlikar.

View solution in original post

7 REPLIES 7

Ankur Bawiskar
Tera Patron
Tera Patron

@servicenow14710 

if you iterate through that array and show alert then it will show 4 alerts which is not a good user experience.

what's your actual business requirement?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hello @Ankur Bawiskar : Thanks, for the implementation i have multiple checkboxs

each checkbox consists a role lets say as discusses test 1 , test 2 , etc., lets suppose a requester raise a request 

first we need to check if he already has any role in his profile . Lets suppose he has a role - test1 in his profile then before submit a request we need to show an alert saying this this role(test1) name exists in his profile.

 

 

Let me know if the requirement is clear, Thanks! 

 

 

var a = ['test1', 'test2', 'test3', 'test4'];

// Function to create the alert message
function createAlertMessage(array) {
    if (array.length === 0) {
        return "No items selected.";
    }
    var message = "This ";
    for (var i = 0; i < array.length; i++) {
        if (i === array.length - 1 && array.length > 1) {
            message += "and " + array[i];
        } else if (i === 0) {
            message += array[i];
        } else {
            message += ", " + array[i];
        }
    }
    message += " already exists in your profile.";
    return message;
}

// Display the alert
alert(createAlertMessage(a));  let me know if you need any info thanks 

@servicenow14710 

I believe you want user to raise request to get some role.

Then why not have a reference variable to roles table and show only those roles which the logged in user doesn't have.

In this way whatever role they select is the role which they require and then they can submit it

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader