- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-07-2025 01:42 AM - edited 02-07-2025 01:42 AM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-08-2025 05:19 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-08-2025 05:19 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2025 07:09 AM
@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!!!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2025 09:30 AM
@servicenow14710 Glad it work for you. 🙂