The CreatorCon Call for Content is officially open! Get started here.

Background Script to read text file Line by Line

Vinod27
Kilo Contributor

Hello Everyone, 

I am new to SNOW and I am in learning phase and need someones help me to figure out solution/approach for below requirement.

We are receiving a text file which needs to be formatted before loading to staging table. For this scenario we are planning to build a background script to format text file and save it in a CSV file. And we will loaded CSV file into staging table using datasource.

The tricky part is, each record which needs to be loaded in Staging table is spitted into different lines in source text file. I need help in building new background script which reads the text file line by line. It will help us to build the record as per our requirement. Is there a way to read file Line by Line until end of file using java script in SNOW.

Can someone please give your suggestion if anyone faces this scenario. I would like to know is there any limitation in SNOW related to file size and processing time. The reason being, the text file which we receive contains huge data. 

Thanks,

Vinod.

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Vinod,

below code when runs in background script will give you the csv file content

ensure you give proper table name and record sys_id; for example purpose I have given incident and my record sys_id

var sa = new GlideSysAttachment();
var bytesContent = sa.getBytes("incident", "bdd212d04fbf7340fc11fa218110c7d5");
var strData = String(Packages.java.lang.String(bytesContent));
gs.print(strData);

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

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Vinod,

Can you also mark my answer as correct, 👍 helpful if you were able to achieve the requirement. Thanks in advance.

Regards

Ankur

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

MarioP1
Tera Contributor

@Ankur Bawiskar  - I know this is an old post but does this still work?

 

I am getting following error:

Evaluator: com.glide.script.RhinoEcmaError: Cannot find function getBytes in object [object GlideSysAttachment].
   script : Line(2) column(0)

I have a similar requirement for table is "sc_req_item" need to grab value from attached text file and save it to a field on custom table.