- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Friday
We have an emailed CSV attachment that we want to load and map to our environment but the first three /r/n prevent us from being able to load the data in.
Has anyone made a custom flow designer action or script that can remove the first X lines from a CSV attachment inbound?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Saturday
Hi @RyanPSchneider,
Approach: Flow Designer Custom Action to Strip First X Lines
Inputs:
attachmentSysId → sys_id of the CSV attachment
linesToSkip → number of lines to remove (e.g., 3)
Outputs:
cleanedCSV → CSV string with header/junk lines removed
Server-side Script (example):
(function execute(inputs, outputs) {
var gsa = new GlideSysAttachment();
var bytes = gsa.getBytes(inputs.attachmentSysId);
var content = Packages.java.lang.String(bytes, "UTF-8"); // convert to string
// Split into lines, handle both \r\n and \n
var lines = content.split(/\r?\n/);
// Remove first X lines
var cleaned = lines.slice(parseInt(inputs.linesToSkip, 10)).join("\n");
// Return cleaned CSV as output
outputs.cleanedCSV = cleaned;
})(inputs, outputs);
🔄 How to Use in a Flow
Flow trigger → e.g. Inbound Email with attachment.
Custom Action (above) → input = attachment sys_id, linesToSkip = 3.
Use cleanedCSV output in your next step:
Write back to a new attachment, or
Parse directly with your CSV parser/import logic.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Friday
Hello @RyanPSchneider ,
Below code will help you to read the content of attachment. Use this code to read attachment and remove any unwanted lines, delete the current attachment and add new attachment with the updated value to the record:
var att = new GlideRecord('sys_attachment');
att.addQuery('table_sys_id', current.sys_id);
att.orderByDesc('sys_created_on');
att.query();
while (att.next()) {
var attach = new GlideSysAttachment().getContentStream(att.sys_id.toString());
var reader = new GlideTextReader(attach);
var ln = ' ';
while ((ln = reader.readLine()) != null) {
gs.info(ln);
}
}
Palani
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Saturday
Hi @RyanPSchneider,
Approach: Flow Designer Custom Action to Strip First X Lines
Inputs:
attachmentSysId → sys_id of the CSV attachment
linesToSkip → number of lines to remove (e.g., 3)
Outputs:
cleanedCSV → CSV string with header/junk lines removed
Server-side Script (example):
(function execute(inputs, outputs) {
var gsa = new GlideSysAttachment();
var bytes = gsa.getBytes(inputs.attachmentSysId);
var content = Packages.java.lang.String(bytes, "UTF-8"); // convert to string
// Split into lines, handle both \r\n and \n
var lines = content.split(/\r?\n/);
// Remove first X lines
var cleaned = lines.slice(parseInt(inputs.linesToSkip, 10)).join("\n");
// Return cleaned CSV as output
outputs.cleanedCSV = cleaned;
})(inputs, outputs);
🔄 How to Use in a Flow
Flow trigger → e.g. Inbound Email with attachment.
Custom Action (above) → input = attachment sys_id, linesToSkip = 3.
Use cleanedCSV output in your next step:
Write back to a new attachment, or
Parse directly with your CSV parser/import logic.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Saturday
Starting with the answer provided by @nityabans27
I wrote some code which worked perfectly for writing a new attachment to the sys_attachment table.
I created a new Script Action in flow designer.
I have written the steps and code in the attached word document.
Do let me know if it helps.
Thank you
Anu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday
Thank you so much! I had to do some small tweaks to get this to work but this did end up working!
First was that I had initially inputted the string of the attachmentSysID vs. the object reference of the attachment. The object reference works with the getBytes. This was probably on me to realize earlier so I struggled with that one for a bit.
For the code, here is what I ended up doing down below
For the Line 7 - I didn't need the full regex for carriage return/line break. I just needed \n for it to work.
For line 10, I removed the parse int function for line var cleaned and just set an input integer to use.
I then wanted to write the attachment directly to the data source so used .write() to use set inputs for my data record, file name and content type.
Lastly to test, I output the different variables for content, lines and cleaned, I set them to outputs so I didn't have to write to a log and search that way. It helped confirm faster when I tested.
Hopefully this helps someone else as it did for me!
(function execute(inputs, outputs) {var gsa = new GlideSysAttachment();var bytes = gsa.getBytes(inputs.attachmentSysId);var content = Packages.java.lang.String(bytes, "UTF-8"); // convert to string// Split into lines via \nvar lines = content.split("\n");// Remove first X linesvar cleaned = lines.slice(inputs.linesToSkip).join("\n");gsa.write(inputs.datasourcesys, inputs.active_custodians_trim, inputs.contentType, cleaned);outputs.content = content;outputs.cleancsv = cleaned;})(inputs, outputs);