Generate catalog item values in CSV format on submission of catalog item

Angshuman3
Mega Guru

Hi All,

Greetings..!!

I was looking for a solution related to Service Catalog.

We were having a requirement, where we want whatever values we provide on the catalog item form, on clicking on Order Now, the values along with the variable name should get generated in .csv format.

For example,

If there are 2 variable (Name & Age)
and Values entered respectively is (Angshuman & 24)

post this when we click on Order Now, the same information should get generated in .csv format marking Name & Age as to Column names and its values as records entered under each column.

Any leads to this will be very helpful.

Thanks,
Angshuman

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

following script you can add in the workflow attached to the RITM

or have this in after insert business rule on sc_req_item table

give your own file name

this will add a csv file as an attachment to the RITM record

var ritmSysId = current.sys_id;
var set = new GlideappVariablePoolQuestionSet();
set.setRequestID(ritmSysId);
set.load();
var vs = set.getFlatQuestions();

var valuesArray = [];

var csvHeader = [];

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

var variableLabel = vs.get(i).getLabel();

csvHeader.push(variableLabel.toString());

var variableValue = vs.get(i).getDisplayValue();

valuesArray.push(variableValue.toString());

}

var csvHeaderRow = csvHeader.toString();
var valueRow =  valuesArray.toString();
var sa = new GlideSysAttachment();

var document = csvHeaderRow + "\n" + valueRow;

var ritmRec = new GlideRecord('sc_req_item');
ritmRec.get(ritmSysId);

sa.write(ritmRec, "data1.csv", "test/csv", document);

Also posted a blog for the same:

https://community.servicenow.com/community?id=community_blog&sys_id=e4f8bd4edbbc881014d6fb243996190e

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

23 REPLIES 23

Hi @Ankur Bawiskar, Many thanks for helping me out here, but we have got many tweaks related to the same requirement. 
I will let you know if there is some other help I require in future in relation this opening a new query.

Again, Thanks a lot.... 🙂

Thanks Ankur , Is it possib le to do it in a flat file (.txt) ?

@KouassiP 

I already replied to your another question and shared the approach.

Do mark my response as correct there and close the thread.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Tarish
Kilo Contributor

Hi @Ankur Bawiskar ,
Thanks for the solution but there's an issue that I am facing with this statement:
var vs = set.getFlatQuestions();

 

I think its working fine for some variables, but not for others.

When I log the output of 'vs', it gives me information like - 
com.glideapp.questionset.CheckBox

com.glideapp.questionset.Yes/No
com.glideapp.questionset.BreakQuestion

And, this is causing 
var variableValue = vs.get(i).getDisplayValue();
this statement to not work properly. 

Can you help me with this?