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 Angshuman,

which variables you were expecting not to come over in csv but which came?

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, The ones where u see no values present... Host Log Location Environment Source Types Based on the snapshot I have provided above, this variables shouldn't come over, when "Openshift logs onboarding" is selected.

Thanks, Angshuman

Hi Angshuman,

I think they got pushed over because of the last else condition; because if the previous if and else if is not satisfied it would go inside the else statement

I think the code needs to be revisited again

Regards

Ankur

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

What changes is required, if you can please help

Hi Angshuman,

can you try to update code as below and try once

I have updated it with both as if conditions

if(variableLabel != 'Requested For' && variableLabel != 'Opened By' && variableLabel != 'Splunk Application'){
		
		if(variableLabel == 'Regular new application logs onboarding' && arrayUtil.contains(regularVariables, variableLabel)){
			csvHeader.push(variableLabel.toString());
			valuesArray.push(variableValue.toString());
		}
		if(variableLabel == 'Openshift logs onboarding' && arrayUtil.contains(openshiftVariables, variableLabel)){
			csvHeader.push(variableLabel.toString());
			valuesArray.push(variableValue.toString());
		}
	}

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