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

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

Hi Ankur,

Thanks a lot for your input and the code is working.

But, can we do it conditional?

Like I have two options, if Option A is selected 3 field would be visible and same should get populated and if Option B is selected then the other 5 fields should get populated on the CSV file.

Can we have something like this.

Currently even if some field is hidden using UI Policy, the csv file is showing me the Column name with the Variable name and empty value.

At the same time, is it possible to prevent few variable and its value not to be captured in the cvs file?

Thanks,
Angshuman

Hi Angshuman,

Regarding the points you mentioned:

1) if the variable is hidden then also the script is fetching the value -> the script won't know what is the functionality on client side whether it was readonly or visible etc

2) Preventing few variable and its values -> you get the variable name in the script; check whether it is not your variable; if it is not then push into array; if yes then don't push into array

3) Not sure on the point you mentioned -> Like I have two options, if Option A is selected 3 field would be visible and same should get populated and if Option B is selected then the other 5 fields should get populated on the CSV file.

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

Hi Ankur,

So let me clarify the complete scenario once again :-

1) The Form is a drop down (Option A & Option B). On selection of Option A certain set of variable are displayed and on selection of Option B another set. This is being handled by UI Policy on the catalog item form. But, when the .csv file is generating, it is showing all the variables of both Option A and B. Is it possible to generate the CSV based on the variables those are filled on the form at the time of submission?

2) I have 2 variables (Requested By & Opened For) on the form which would be auto populated based on the logged in user. But I don't want this field and its values to come up on the .csv file. Is that possible?

I am using the provided script in Business Rule

Thanks,
Angshuman