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

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.

@Viraj Hudlikar : Thanks a lot for the solution this is exactly what my implementation is & wants , only that when the condition is not matched there is no need of this code:

  if (overlappingRoles.length === 0) {
        return "No selected roles already exist in your profile.";
    }

I Just needed to tweak at some places. Thanks for responding and for correct response. Thanks!!!

@servicenow14710  Glad it work for you. 🙂