building html setup to be added to work notes thru script include
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-22-2017 11:22 AM
Hi community,
working with a script include that is meant to populate the WorkNotes on an incident that is generated from a request.
What I need to do is to have the script include take the variables from the request and to put them into a grid (see screenshot) and then to have that grid appear as a worknote on the new incident.
So I want something like this:
to appear in the worknotes when the new incident is created from the request.
I've done this before in email scripts successfully. But struggling to get it to work in this context. Here's my script so far:
//Displays ERP Request Variables in Created Incident Work Notes
var reqObj = new GlideRecord('x_cur_erp_sm_request');
reqObj.get(current.sys_id);
var sInc = new global.reqUtils();
var vArr = sInc.parseVars(reqObj);
template.print("<table style=\" text-align: left;background-color: F2F3F3;border-collapse: collapse;font-family: arial, helvetica, sans-serif;font-size: 12pt; padding: 5px; border: 1px solid; border-color: grey;\">");
//the first row in the table contains the request type
template.print("<tr><td style=\"padding: 5px; border: 1px solid; border-color: grey;\" >");
template.print("Request Type");
template.print("</td><td style=\"padding: 5px; border: 1px solid; border-color: grey;\" >");
template.print(reqObj.template.getDisplayValue());
template.print("</td></tr>");
for (y = 0; y < vArr.length; y++) {
var q = vArr[y].question;
var a = vArr[y].answer;
try {
if (a.toString() != 'undefined'){
template.print("<tr><td style=\"padding: 5px; border: 1px solid; border-color: grey;\" >");
template.print(q);
template.print("</td><td style=\"padding: 5px; border: 1px solid; border-color: grey;\" >");
template.print(a);
template.print("</td></tr>");
}
}
catch (err)
{
}
}
template.print("</table>");
incident.work_notes = vArr;//this line populates the work notes on the new incident
thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-26-2017 12:49 AM
Hi Patrick,
It would be great if you can share the code of "reqUtils" script include.
I have no idea how parseVars method gets the variables.
Thank you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-26-2017 08:00 AM
Hi Vitaly,
here's the reqUtils script include:
var reqUtils = Class.create();
reqUtils.prototype = {
initialize: function() {
},
parseVars: function(rec) {
var qaArr = this._getBlank(rec.sys_id);
for (var i in rec.variables) {
var dStr = rec.variables[i].getDisplayValue();
var vStr = rec.variables[i];
if (!vStr.nil()) {
var qObj = this._getQuestion(rec.sys_id, vStr);
qObj.answer = dStr;
qaArr.push(qObj);
}
}
qaArr.sort(function(a, b) {
return a.order-b.order;
});
return qaArr;
},
_getBlank: function(qID) {
var qa = new GlideRecord('question_answer');
qa.addQuery('table_sys_id', qID);
qa.addQuery('value', '');
qa.query();
var rArray = [];
while (qa.next()) {
if (qa.question.visible_summary) {
var result = {"order": qa.question.order,
"question": qa.question.getDisplayValue(),
"answer": ''
};
rArray.push(result.toString());
}
}
return rArray;
},
_getQuestion: function(qID, v) {
var qa = new GlideRecord('question_answer');
qa.addQuery('table_sys_id', qID);
qa.addQuery('value', v);
qa.query();
var result = '';
if (qa.next()) {
result = {"order": qa.question.order,
"question": qa.question.getDisplayValue()
};
}
return result;
},
type: 'reqUtils'
};
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-26-2017 12:05 PM
Hi Patrick,
I think I found a better implementation of parseVars, check it out:
parseVars: function(rec) {
var qaArr = [];
var qs = new GlideappVariablePoolQuestionSet();
qs.setRequestID(rec.sys_id);
qs.load();
var vs = qs.getFlatQuestions();
for (var i = 0; i < vs.size(); i++) {
qaArr.push({
"order": vs.get(i).order,
"question": vs.get(i).getLabel(),
"answer": vs.get(i).getDisplayValue()
});
}
qaArr.sort(function(a, b) {
return a.order - b.order;
});
return qaArr;
},
EDIT: Bad formatting
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-26-2017 01:30 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-27-2017 02:18 PM
is there maybe a way to build into this script something that checks whether the new "q" is the same as a "q" that's already been identified? and then to have it not print it in the "note"?
for (y = 0; y < vArr.length; y++) {
var q = vArr[y].question;
var a = vArr[y].answer;
try {
if (a.toString() != null && q.toString != q) {//something like this?
note += "<tr><td style=\"padding: 5px; border: 1px solid; border-color: grey;\" >";
note += q;
note += "</td><td style=\"padding: 5px; border: 1px solid; border-color: grey;\" >";
note += a;
note += "</td></tr>";
}
} catch (err) {}
}
note += "</table>[/code]";