Writing all variables from a catalog item

Often ServiceNow users want the full information of a Requested Item’s variables included in the outgoing Approval emails to give their approvers a better idea of what they are approving without having to go into the ServiceNow instance unnecessarily. There isn’t a native way in ServiceNow to add these to an email because every catalog item has different form fields that require different data sets so I came up with a function to return all of the variables in a Catalog Item in the form of “Question: Value” which can be added to string fields, or more likely, to an email script for a Requested Item’s Approval.

/*
This function grabs all the variable labels and values from a catalog item (not including variable sets) and returns it in the format:
“Question1:Value1”
“Question2:Value2”
“QuestionN:ValueN”
The ‘item’ variable is the catalog item.
*/
function pcgVariableWriter(item){

var descriptionText = ”
/*
Create an array with the following types who should not be included in the description. To add another type, add the integer to the arrayOfExcludedTypes array.
12 = Break, 20 = Container End, 24 = Container Split, 19 = Container Start, Label = 11, Macro = 14, Macro with Label = 17, UI Page = 15
*/
var arrayOfExcludedTypes = [12,20,24,19,11,14,17,15];

var variable = new GlideRecord(“item_option_new”);
variable.addEncodedQuery(“typeNOT IN” + arrayOfExcludedTypes.toString() + “^cat_item=” + item.cat_item);
variable.orderBy(“order”);
variable.query();
while(variable.next()){
//find the question text (label) and the value and add it to the overall string (description text)
descriptionText += variable.question_text + “: ” + item.variables[variable.name].getDisplayValue();
//If any variables contain blank data, prints it as [empty]
if (item.variables[variable.name] == “”){
descriptionText += “[empty]\n”;
}else{
descriptionText += “\n”;
}

}
//at this point descriptionText has a list of ‘Variable Lable : Variable Value’ for the catalog item
return descriptionText;
}