Attach variable inputs to a file and then attach the file to change

jawshoeah
Kilo Contributor

We have a very unique request that I am looking for some advice on. The owner of a Service Catalog item would like the inputs of all the variables on the Service Catalog request form to be exported to a file (csv, excel, pdf, anything) and then have that file automatically attached to the change request that the request item creates in its workflow. Is this something that is even possible? All my research into this topic seems to indicate no, but I wanted to ask the question to a general audience to see if anyone has ever had to do something similar.

Any feedback is welcome. Thanks!

1 ACCEPTED SOLUTION

Michael Ritchie
ServiceNow Employee
ServiceNow Employee

It can be posted as a text file.   There is a workflow activity called Attachment Note that generates an attachment to a record.   You supply it a name and the data and it will create the attachment automatically.



Thinking via typing here...


Since you want this attached to the change record, the Attachment Note activity would need to be in the change workflow.   I assume that parent value of the change_request record is the request and if so you could put a Run Script activity before the Attachment Note activity, loop through all the variables leveraging the parent reference and create the data for the attachment in a workflow scratchpad variable and then use that in the Attachment Note.



There is an OOB script that runs in the request.itil.approve.role email template for catalog request approval emails that may help you get all the variable questions and answers:


var keys = new Array();


var set = new GlideappVariablePoolQuestionSet();    


set.setRequestID(item.sys_id);


set.load();


var vs = set.getFlatQuestions();


for (var i=0; i < vs.size(); i++) {


      if(vs.get(i).getLabel() != '') {


            template.space(4);


            template.print('         ' +   vs.get(i).getLabel() + " = " + vs.get(i).getDisplayValue() + "\n");


      }


}



Obviously the template.print items would need to be replaced by something else, but again this should get you going.


View solution in original post

8 REPLIES 8

Michael Ritchie
ServiceNow Employee
ServiceNow Employee

It can be posted as a text file.   There is a workflow activity called Attachment Note that generates an attachment to a record.   You supply it a name and the data and it will create the attachment automatically.



Thinking via typing here...


Since you want this attached to the change record, the Attachment Note activity would need to be in the change workflow.   I assume that parent value of the change_request record is the request and if so you could put a Run Script activity before the Attachment Note activity, loop through all the variables leveraging the parent reference and create the data for the attachment in a workflow scratchpad variable and then use that in the Attachment Note.



There is an OOB script that runs in the request.itil.approve.role email template for catalog request approval emails that may help you get all the variable questions and answers:


var keys = new Array();


var set = new GlideappVariablePoolQuestionSet();    


set.setRequestID(item.sys_id);


set.load();


var vs = set.getFlatQuestions();


for (var i=0; i < vs.size(); i++) {


      if(vs.get(i).getLabel() != '') {


            template.space(4);


            template.print('         ' +   vs.get(i).getLabel() + " = " + vs.get(i).getDisplayValue() + "\n");


      }


}



Obviously the template.print items would need to be replaced by something else, but again this should get you going.


Hi Michael,



I am kind of new in this area. Could you assist how do I perform this whole thing.


I tried your script in a Run Activity :



var variable_label = '', variable_values = '';


var variableObject = {};


var keys = [];


var set = new GlideappVariablePoolQuestionSet();


set.setRequestID(current.sys_id);


set.load();


var vs = set.getFlatQuestions();


for (var i=0; i < vs.size(); i++) {


  if(vs.get(i).getLabel() != '') {


  //template.space(4);


  //template.print('         ' +   vs.get(i).getLabel() + " = " + vs.get(i).getDisplayValue() + "\n");


  variable_label   = vs.get(i).getLabel();


  variable_values = vs.get(i).getDisplayValue();


  variableObject = { variable_label : variable_values };


  }


  workflow.scratchpad.catalog_variables = variableObject;


}




And tried to grab and attach the variables in the Attachment Note activity by writing the below in the Attachment Data field of the Attachment Note activity:


workflow.scratchpad.catalog_variables = variableObject;



But the attached content has just that line in the attached file instead of my catalog variables.



Please could you help.



Thanks heaps in advance!



Regards,


Samiul


Samiul, here is a working example:


var varList = [];


var set = new GlideappVariablePoolQuestionSet();    


set.setRequestID(current.sys_id);


set.load();


var vs = set.getFlatQuestions();


for (var i=0; i < vs.size(); i++) {


      if(vs.get(i).getLabel() != '') {


          varList.push(vs.get(i).getLabel() + " = " + vs.get(i).getDisplayValue());


      }


}


workflow.scratchpad.varString = varList.join("\n");



Then set the Attachment Data value in your Attachment note to ${workflow.scratchpad.varString}


Thank you so much Michael!



I have been sweating for this line: ${workflow.scratchpad.varString} !!



Thanks heaps mate!!