The CreatorCon Call for Content is officially open! Get started here.

Brian Workman
Tera Contributor

I wanted to create a method to include variables in both catalog task and requested item notifications. I decided to house the script within our Utils generalised script include and then create a mail script that I could drop into any notification. I also wanted it to be available for other purposes and so I include the ability to return sys_ids when raw is opted in.

It is important to note that if you want variables which are only visible on the catalog task, that you include the setTaskID method.

Lastly, you may also pass a sysapproval_approver record and it will return the variables of the associated catalog task or requested item. Cheers!

Script Include Function

    /**
     * Get the questions and answers from a Requested Item or Catalog Task.
     * 
     * @param   {GlideRecord} item   The requested item, catalog task, or approval.
     * @param   {boolean}     [raw]  Whether to include sys_ids instead.
     * 
     * @returns {JSON}               Question and answer pairs in JSON format.
     */
    getVariables: function(item, raw) {
        var ritm, task;

        if(item.instanceOf("sc_task")) {
            task = item;
            ritm = item.request_item.getRefRecord();
        } else if(item.instanceOf("sc_req_item")) {
            ritm = item;
        } else if(item.instanceOf("sysapproval_approver")) {
            return this.getVariables(item.sysapproval.getRefRecord(), raw);
        } else {
            return {};
        }

        var ret = {}, set = new GlideappVariablePoolQuestionSet();
        set.setRequestID(ritm.getValue("sys_id"));
        if(JSUtil.notNil(task)) set.setTaskID(task.getValue("sys_id"));
        set.load();
        var questions = set.getFlatQuestions(), q, szq = questions.size();
        for(q = 0; q < szq; q++) {
            var question = questions.get(q), label, value;
            if(!question.isVisibleSummary()) continue;
            label = "%1"
                .replace("%1", question.getLabel());
            if(JSUtil.nil(label) || label === "null") continue ;
            if(label.toLowerCase().startsWith("order guide")) continue ;            
            if(label.endsWith(":")) label = label.substring(0, label.length-1);
            value = "%1"
                .replace("%1", raw ? question.getValue() : question.getDisplayValue());
            if(JSUtil.nil(value) || value === "null") continue ;
            value = value.trim();
            ret[label] = value;
        }
        return ret;
    }

Mail Script

(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
          /* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
          /* Optional GlideRecord */ event) {

	var out = "", variable, variables = new Utils().getVariables(current);
	for(variable in variables) {
		out += "<p><strong>%1:</strong>\n<br />%2</p>\n"
			.replace("%1", variable)
			.replace("%2", variables[variable]);
	}	
	template.print(out);

})(current, template, email, email_action, event);

 

 

 

 

Version history
Last update:
‎02-03-2019 12:25 PM
Updated by: