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,

Please find the responses below

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? -> yes this is possible; determine the variable value in script 

// put the variable name in these arrays; one array for storing variables which are shown based on value as option A and other for value as option B

var variablesOptionA = ['variable1', 'variable2'];
var variablesOptionB = ['variable3', 'variable4'];

var arrayUtil = new ArrayUtil();

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

var variableLabel = vs.get(i).getLabel();
var variableValue = vs.get(i).getDisplayValue();
var variableName = vs.get(i).getName();

// put the label here to compare
if(variableLabel != 'Requested By' && variableLabel != 'Opened For'){

if(variableValue == 'Option A' && arrayUtil.contains(variablesOptionA, variableName)))
csvHeader.push(variableLabel.toString());
valuesArray.push(variableValue.toString());
}
else if(variableValue == 'Option B' && arrayUtil.contains(variablesOptionB, variableName))){
csvHeader.push(variableLabel.toString());
valuesArray.push(variableValue.toString());
}
else{
csvHeader.push(variableLabel.toString());
valuesArray.push(variableValue.toString());
}

}

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? -> yes you can handle this in script as below

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

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

// put the label here to compare
if(variableLabel != 'Requested By' && variableLabel != 'Opened For'){
csvHeader.push(variableLabel.toString());
valuesArray.push(variableValue.toString());
}

}

Try and let me know

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 2nd part is working properly, but the 1st part of the code isn't working properly, it is providing me the same feed file with the empty values. 

Please find the below code :-

 

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

var regularVariables = ['Application Name', 'Splunk index name', 'Host', 'Environment', 'Log file location', 'Sourcetype'];
var openshiftVariables = ['Application Name', 'Splunk index name', 'Openshisft Namespace name'];

var arrayUtil = new ArrayUtil();

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

var variableLabel = vs.get(i).getLabel();
var variableValue = vs.get(i).getDisplayValue();
var variableName = vs.get(i).getName();

if(variableLabel != 'Requested For' && variableLabel != 'Opened By'){

if(variableValue == ('Regular new application logs onboarding' && arrayUtil.contains(regularVariables, variableName))){
csvHeader.push(variableLabel.toString());
valuesArray.push(variableValue.toString());
}
else if(variableValue == ('Openshift logs onboarding' && arrayUtil.contains(openshiftVariables, variableName))){
csvHeader.push(variableLabel.toString());
valuesArray.push(variableValue.toString());
}
else{
csvHeader.push(variableLabel.toString());
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, "splunk.csv", "test/csv", document);

Thanks,
Angshuman

Hi Ankur,

Did you get time to check my code?

Please if possible do let me know once you get hold of what I am doing wrong.

Thanks,
Angshuman

Hi Angshuman,

Can you update the line as below and test once

there was a round bracket just before the variable name and also at the end after arrayutil contains method; I have removed that from if and the else statement

you need to check the variable label is present in the array or not and not the variableValue since in the array you have put the variable labels

updated code below

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

var variableLabel = vs.get(i).getLabel();
var variableValue = vs.get(i).getDisplayValue();
var variableName = vs.get(i).getName();

if(variableLabel != 'Requested For' && variableLabel != 'Opened By'){

if(variableLabel == 'Regular new application logs onboarding' && arrayUtil.contains(regularVariables, variableLabel)){
csvHeader.push(variableLabel.toString());
valuesArray.push(variableValue.toString());
}
else if(variableLabel == 'Openshift logs onboarding' && arrayUtil.contains(openshiftVariables, variableLabel)){
csvHeader.push(variableLabel.toString());
valuesArray.push(variableValue.toString());
}
else{
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

Hi Ankur,

I did make the changes based on your code, yet when the feed file is being generated it is coming with empty values and the Variable Labels of those fields which we haven't selected while creating the Request.

Please find the below code :-

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

var regularVariables = ['Application Name', 'Splunk Index name', 'Host', 'Log Location', 'Environment', 'Source Types', 'Please enter keywords for your logs for tagging purpose', 'Is there any sensitive information in your logs?', 'Please enter a description and usage of the logs you are onboarding to Splunk', 'What do you intend to use Splunk for with the logs you are ingesting?'];

var openshiftVariables = ['Application Name', 'Splunk Index name', 'Openshisft Namespace name', 'Please enter keywords for your logs for tagging purpose', 'Is there any sensitive information in your logs?', 'Please enter a description and usage of the logs you are onboarding to Splunk', 'What do you intend to use Splunk for with the logs you are ingesting?'];

var arrayUtil = new ArrayUtil();

for(var i=0;i<vs.size();i++){
	
	var variableLabel = vs.get(i).getLabel();
	var variableValue = vs.get(i).getDisplayValue();
	var variableName = vs.get(i).getName();

	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());
		}
		else if(variableLabel == 'Openshift logs onboarding' && arrayUtil.contains(openshiftVariables, variableLabel)){
			csvHeader.push(variableLabel.toString());
			valuesArray.push(variableValue.toString());
		}
		else{
			csvHeader.push(variableLabel.toString());
			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, "splunk.csv", "test/csv", document);


The Output I am getting:-

find_real_file.png

Thanks,
Angshuman