Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to import a CSV file without headers?

jamesgb
Kilo Explorer

Hello,

I am wondering if it is possible to import a CSV file that it doesn't any header. I tried and ServiceNow says expected two columns but there is only one.

I checked out on google and I see that some peope having same requirement sometimes while working this type of no header data files.

Have you ever tried? Is there any feature in ServiceNow for this?

---

1 ACCEPTED SOLUTION

So the short answer is "You need headers to import a CSV in ServiceNow." How you get those headers in is up to you (or the upstream data provider.)



If I have answered your question, please mark my response as correct so that others with the same question in the future can find it quickly and that it gets removed from the Unanswered list.



If you are viewing this from the community inbox you will not see the correct answer button.   If so, please review How to Mark Answers Correct From Inbox View.



Thank you


View solution in original post

13 REPLIES 13

Would love to do if I had a HI account Chuck, am fairly new to SN and learning the ropes by building a custom portal application. Love the platform and hope to spend a lot of time making the most of it.


Hi,

Headers are not needed when importing CSV files. It can be treated as text file. From the Data Source select from "Format" field the option "Custom (Parsy by Script)". Add the following code:

var map = {};
var items = line.split(",");
map['u_field1'] = items[0];
map['u_field2'] = items[1];
map['u_field3'] = items[2];	
result.addRow(map);
	

Variable "line" contains each row from the file. Use the map object to include fields names from staging table.

If I have answered your question, please mark my response as correct. Thanks

 

How would you exclude the header when using this script?  I am using "Custom (Parse by Script)" in a CSV with headers because the last column ends with a comma.  ServiceNow thinks there is another column and the transform fails.

Hadyn
Tera Expert

I just had the same need. My data source comes from a csv hosted on a web server so I use the HTTPS method. What I did was created a custom processor on my instance ie https://blah.service-now.com/mycsv. In the processor I use REST api to get my original csv and then I add a header row to it and serve it back to the requestor (data source).

This is my processor script: 

(function process(g_request, g_response, g_processor) {
	var sm = new sn_ws.RESTMessageV2();
	sm.setEndpoint("http://originalsourceofcsv.com.au/thecsv");
	sm.setHttpMethod("get");
	sm.setHttpTimeout(6000);  //optional
	sm.setLogLevel("all");  //optional
	sm.setMIDServer("Mid1"); //optional
	var response = sm.execute(); 
	
	//Write output
	var headerrow = "Column1,Column2,Column3,Column4\n";	
	g_processor.writeOutput("text/plain",headerrow + response.getBody());
})(g_request, g_response, g_processor);