Skip to content

Instantly share code, notes, and snippets.

@waldothedeveloper
Last active February 3, 2023 23:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save waldothedeveloper/eb35dcde1f3952829c0ae4b402bbb036 to your computer and use it in GitHub Desktop.
Save waldothedeveloper/eb35dcde1f3952829c0ae4b402bbb036 to your computer and use it in GitHub Desktop.
Servicenow Catalog Client Script to add dynamic options to catalog variable of type choices
// services categories for the applications that live in the cmdb_ci_services table
var categories = [
{
id: 0,
name: "Access Request",
},
{
id: 1,
name: "New Database Request",
},
{
id: 2,
name: "Add New Data Process",
},
{
id: 3,
name: "Report Request",
},
{
id: 4,
name: "New Application Request(<40 hrs)",
},
{
id: 5,
name: "Report Modification",
},
{
id: 6,
name: "New Project Request",
},
{
id: 7,
name: "Server Specs Request",
},
{
id: 8,
name: "Process Modification",
},
{
id: 9,
name: "New Sites",
},
{
id: 10,
name: "Modification to Data Process",
},
{
id: 11,
name: "Server Installs",
},
{
id: 12,
name: "Application Modification (<40 hrs)",
},
{
id: 13,
name: "New Org Request",
},
{
id: 14,
name: "Server Patches",
},
];
// little helper function to do something like: application_name_like_this_with_lowdash
function createChoicevalue(value) {
if (typeof value !== "string") {
return null;
}
return value.trim().toLowerCase().replaceAll(" ", "_");
}
// this will return each of the fields that the GlideForm addOption method expects
function createChoice(choice) {
// choice should be a number always
if (isNaN(choice)) {
return null;
}
return {
choiceValue: createChoicevalue(categories[choice].name),
choiceLabel: categories[choice].name,
};
}
function setValuesBasedOnTheApplication(newValue) {
//start of with an empty set of values
var values = [];
if (typeof newValue !== "string") {
return null;
}
//Define the values you want to conditionally create options for
switch (newValue) {
case "SQL Database":
values = [
createChoice(1),
createChoice(7),
createChoice(14),
createChoice(11),
createChoice(0),
];
break;
case "ADT Report Emails":
values = [createChoice(0)];
break;
case "HIE Email Alerts Request":
values = [createChoice(0), createChoice(8)];
break;
case "Archer Email Alerts Request":
values = [createChoice(0), createChoice(8)];
break;
case "Share Point":
values = [createChoice(0), createChoice(9)];
break;
case "Nominal Services":
values = [createChoice(0), createChoice(3), createChoice(12)];
break;
case "Data Ingestion-Extraction Process":
values = [createChoice(2), createChoice(10)];
break;
case "Contestations":
values = [createChoice(0), createChoice(3), createChoice(12)];
break;
case "Center Ops Portal":
values = [createChoice(0), createChoice(3), createChoice(12)];
break;
case "CD Reports Portal":
values = [createChoice(0), createChoice(3)];
break;
case "Power Platform":
values = [createChoice(0), createChoice(4), createChoice(12)];
break;
default:
break;
}
return values;
}
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == "") {
return;
}
// when this value changes we call setValuesBasedOnTheApplication to get the values we will need for the choice options list
var getApplicationName = g_form.getDisplayValue("application_name");
// this will contain an array of objects that looks like this:
// [ {choiceValue: '', choiceLabel: ''}, {choiceValue: '', choiceLabel: ''}, {choiceValue: '', choiceLabel: ''}]
var applicationCategoriesArray =
setValuesBasedOnTheApplication(getApplicationName);
// alert(JSON.stringify(applicationCategoriesArray, null, 2));
// making sure we have an array of categories and that is not empty
if (
Array.isArray(applicationCategoriesArray) &&
applicationCategoriesArray.length > 0
) {
createDependentOptions("categories", applicationCategoriesArray, false);
}
}
//dependentField = name of the field you want to populate with options
//dependentValuesArray = an array of objects
//includeNone set to true or false. true will result in -- None -- being added as a label with no value to position 0
function createDependentOptions(
dependentField,
dependentValuesArray,
includeNone
) {
var index = 0;
//Start by clearing the options
g_form.clearOptions(dependentField);
//If includeNone is true, add a label for none with a value of ''
if (includeNone) {
g_form.addOption(dependentField, "", "-- None --", 0);
//If none was added, increment the index
index++;
}
for (var i = 0; i < dependentValuesArray.length; i++) {
g_form.addOption(
dependentField,
dependentValuesArray[i].choiceValue,
dependentValuesArray[i].choiceLabel,
i + index
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment