
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
Not sure how often we come across a request where we need get list of variables submitted for a request to be placed in a file to be auto generated (possibly xls, xlsx etc. format) as an attachment & should be attached to the Requested item submitted.
We did had something similar to be done for an request item which was achieved by using the below snippet in the Runscript activity of the workflow which was associated directly to the Begin activity for the item.
All that is required is to retrieve the list of variables & then use the Write functionality of GlideSysAttachment API class.
var varquestion='';
var varanswer='';
var itemjoin="";
var itemquestion = "";
var itemanswer='';
var v;
/* Put all variable values and labels from the variable pool into an array */
var sortedArray = sortVariables(current.variables);
for (var i in sortedArray) {
v = current.variables[sortedArray[i].index];
/* Only include non-empty variables, and exclude Label and Container variables */
if (v != '' && v != 'false' && v.getGlideObject().getQuestion().type != 11 && v.getGlideObject().getQuestion().type != 19 && v.getGlideObject().getQuestion().type != 20) {
itemquestion += v.getGlideObject().getQuestion().getLabel() + "\t";
itemanswer+=v.getDisplayValue()+"\t";
}
}
varquestion += itemquestion+"\t";
varanswer +="\n"+itemanswer;
var finaloutput=varquestion+varanswer;
function sortVariables ( variableArray ){
var sortedVariables = [];
var count = 0;
for ( var i in variableArray ){
var object = {index:i, order:variableArray[i].getGlideObject().getQuestion().order};
sortedVariables[count] = object;
count++;
}
sortedVariables.sort( function compare(a,b){return a.order - b.order});
return sortedVariables;
}
//converts the file to xls format with file name as RITM number (considering workflow is on RITM table) & is attached to the RITM record
var attachfile = new GlideSysAttachment();
attachfile.write(current, current.number+'.xls','test/csv', finaloutput);
This would then print data in Excel in format as below
Where Row 1 represents all Variables questions while Row 2 containing the values (answers) for the variables submitted.
If at all data is required to be printed in the format as below
with all Variable questions in Column A & its corresponding values in column B (something in tabular form) then all you need is to use the below snippet
var varquestion='';
var varanswer='';
var itemjoin="";
var itemquestion = "";
var itemanswer='';
var v;
/* Put all variable values and labels from the variable pool into an array */
var sortedArray = sortVariables(current.variables);
for (var i in sortedArray) {
v = current.variables[sortedArray[i].index];
/* Only include non-empty variables, and exclude Label and Container variables */
if (v != '' && v != 'false' && v.getGlideObject().getQuestion().type != 11 && v.getGlideObject().getQuestion().type != 19 && v.getGlideObject().getQuestion().type != 20) {
itemquestion += v.getGlideObject().getQuestion().getLabel() + "\t"+ v.getDisplayValue()+"\n";
}
}
varquestion += itemquestion+"\n";
var finaloutput=varquestion;
function sortVariables ( variableArray ){
var sortedVariables = [];
var count = 0;
for ( var i in variableArray ){
var object = {index:i, order:variableArray[i].getGlideObject().getQuestion().order};
sortedVariables[count] = object;
count++;
}
sortedVariables.sort( function compare(a,b){return a.order - b.order});
return sortedVariables;
}
//converts the file to xls format with file name as RITM number (considering workflow is on RITM table) & is attached to the RITM record
var attachfile = new GlideSysAttachment();
attachfile.write(current, current.number+'.xls','test/csv', finaloutput);
Thanks,
Jaspal Singh
- 2,934 Views
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.