Using parameters to set short description in RITMS
//get the short description defined on the catalog item.
var shortDescription = current.cat_item.u_target_short_description;
//define a short description variable we can manipulate and shorten to extract what we need
var targetShort = shortDescription;
//get the total length of the cat_item description to use in the substring method.
var totalLength = shortDescription.length;
//declare an array of variables that will be used throughout.
var variables = [];
//count how many instances } we have in the new cat_item short description field. Each one represents another variable.
var count = (targetShort.match(/}/g)||[]).length;
//iterate through our count pushing values back to the variables array.
for ( var i = 0; i < count ; i++){
//get start position of the first { character
var start = targetShort.indexOf('{') + 1;
//find the next closing } character
var end = targetShort.indexOf('}',start);
//the extracted value between the two brackets
var userVar = targetShort.substring(start,end);
//push some values back into the array to be used later.
variables.push({
//push back the original value, we will use this value to be replaced in the string later.
oldValue:'{'+ userVar +'}',
//new value includes current.variables which will be used to set the actual value of the variable.
newValue:'current.variables.' + userVar,
});
//reduce the targetShort string by taking off the part we just added to the array.
targetShort = targetShort.substring(end + 1, totalLength);
}
//count how many variables we have found in the cat_item short description field
var variableCount = variables.length;
//iterate through the count of variables and find some values for them
for ( var j = 0; j < variableCount ; j++){
//update the new value in our array to the result of the getVar function.
variables[j].newValue = getVar(variables[j].newValue,variables[j].oldValue,current.cat_item.sys_id);
//update the short description variable each time we iterate through.
shortDescription = shortDescription.replace(variables[j].oldValue, variables[j].newValue);
}
//update current short description with everything we have just done.
current.short_description = shortDescription;
//function to find out if we need to get the display value or not of the variable.
function getVar(variable,oldValue,currentSys) {
//declare a string variable for our list of variable sets
var variableSets='';
//gr into the variable set relationship table
var varSet = new GlideRecord("io_set_item");
//add query for the catalog item which this requested item is for.
varSet.addQuery("sc_cat_item", currentSys);
//query table
varSet.query();
//as we loop through, add the name of the variable set which we will use later.
while (varSet.next()) {
//push the variable set names back into the variable we defined
variableSets+=varSet.variable_set.getDisplayValue() + ', ';
}
//remove the { from the variable name we passed into function
var variableName = oldValue.replace(/{/g, '');
//remove the } from the variable name we passed into function
variableName = variableName.replace(/}/g, '');
//declare the type variable that we will use to check within an if statement.
var type ='';
//filter that will be used to find any variables that are part of this item or variable set that has the same name as the current iteration we are on as part of the j loop.
var query = 'cat_item.sys_id='+currentSys+'^ORvariable_set.nameIN'+variableSets + '^name='+variableName;
//gr into item variables table
var gr = new GlideRecord("item_option_new");
//add our query as above
gr.addEncodedQuery(query);
//query table
gr.query();
//if we find a record that matches, set the type
if (gr.next()) {
//set type to whatever we find, this is the type of variable it is (reference, select etc.)
type = gr.type;
}
//if the type is one of the types that require us to get a display value, get it.
if(type=='1' ||
type=='3' ||
type=='5' ||
type=='7' ||
type=='8' ||
type=='18' ||
type=='21' ||
type=='22'
){
//add get display value onto the current.variables.field string that we will use.
variable +='.getDisplayValue();';
}
//use the service eval function
var value = GlideEvaluator.evaluateString(variable);
//pass back the final current.variables.field +- .getDisplayValue();
return value;
}