SuiteScript – Fails to read CSV

This is was amazingly frustrating. I’m calling this a bug. NetSuite… I hope you’re listening. When uploading a file with SuiteTalk, setting the file type to CSV does not make the file a CSV. Unless the file name ends in “.csv”, the file shows as type “Other Binary.” This precludes any script (I only tried SuiteScript 2.0) from reading the contents of the file. File.getReader() and File.iterator() blow up.

My use case: I have a large number of records to upload to the server. In order to make the upload performant, I chose to push them up in a CSV and then kick off a server-side script to actually create the records. The number of records was large enough that the hit the 10 Meg upload limit. So I broke the CSVs into multiples. I set the type to CSV but I did not add “.csv” to the end of the file name. Whoa, that a mistake!

This is my SuiteTalk app written in C# which breaks up and uploads my CSVs.

Code Example CSV

As always… Here’s the code in a form you can cut and paste.

private static void uploadFile(NetSuiteService service, StringBuilder csv, int fileSuffix)
{
Console.WriteLine(“Writing CSV file to ” + path);
System.IO.File.WriteAllText(path, csv.ToString());

string NetSuiteFileName = nsFileName + String.Format(“_{0:000}.csv”, fileSuffix);

Console.WriteLine(“Creating upload file to send to NetSuite”);
File file = new File()
{
attachFrom = FileAttachFrom._computer,
attachFromSpecified = true,
name = NetSuiteFileName,
folder = new RecordRef()
{
internalId = “615990”
,
type = RecordType.folder
,
typeSpecified = true
},
fileType = MediaType._CSV,
fileTypeSpecified = true,
textFileEncoding = TextFileEncoding._utf8,
textFileEncodingSpecified = true,
content = localFileToContents(path)
};

Console.WriteLine(“Uploading file to NetSuite”);
service.tokenPassport = TBAtoken.createTokenPassport();
WriteResponse writeResponse = service.add(file);

if (writeResponse.status.isSuccess)
{
Console.WriteLine(“Success!”);
}
else
{
Console.WriteLine(“Failed”);
foreach (var detail in writeResponse.status.statusDetail)
{
Console.WriteLine(” ” + detail.message);
}
}
}

static byte[] localFileToContents(String sFileName)
{
System.IO.FileStream inFile;
byte[] data;

try
{
inFile = new System.IO.FileStream(sFileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
data = new Byte[inFile.Length];
long bytesRead = inFile.Read(data, 0, (int) inFile.Length);
inFile.Close();
}
catch (System.Exception exp)
{
// Error creating stream or reading from it.
Console.WriteLine(exp.Message);
return null;
}

return data;
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s