Export to CSV or XML using script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-11-2017 06:38 PM
Hi Everyone, I need help with exporting to either XML or CSV.
I am aware that I can do this using the UI but I would rather do it using a script.
Here is the script I am using currently. It prints exactly what I want. I just want this information to be saved as either a CSV or XML file.
//--------------------------------------------------------------------------------------------------------
var gr = new GlideRecord("wm_task");
gr.addEncodedQuery("company=2j43ac524fbe8200148a0bd81818fd149");
gr.addEncodedQuery("u_contract_id=null");
gr.addEncodedQuery("u_charge_type=No charge");
gr.query();
var count = 0;
while ( gr.next() )
{
count++;
gs.print(gr.number);
gs.print(gr.u_job_type);
gs.print(gr.company);
}
gs.print(count);
//--------------------------------------------------------------------------------------------------------
Thanks in advance
- 25,412 Views
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-11-2017 08:09 PM
lets say you want to add a new record in demo table along with the attachement.
so write this before the script
var gr1=new GlideRecord('demo');
gr1.initialize();
gr1.field1='whatever';
..
....
......
....
......
attachment.write('wm_task', gr1.sys_id, fileName, content_type, fileBytes);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-11-2017 09:07 PM
Let try this:
var output = "Number,Company,Short_Description"; //header for csv file
var table = "incident";
var recordId = ""; //using for attachment file
var gr = new GlideRecord(table);
gr.addEncodedQuery("assigned_to=46d44a23a9fe19810012d100cca80666"); //Incident was assigned to Beth Anglin
gr.query();
var count = 0;
while (gr.next()) {
count++;
output += "\n" + gr.number + "," + gr.company.getDisplayValue() + "," + gr.short_description;
if (!recordId) {
recordId = gr.sys_id;
}
}
gs.print(recordId);
writeAttachmentFile(output);
function writeAttachmentFile(data) {
var attachment = new Attachment();
var attachmentRec = attachment.write(table, recordId, "export.csv", "text/csv", data);
}
==> this code will be query all incident was assigned to Benth Anglin => write an attachment file (as csv) and attach it into the first incident in query command.
==> you can navigate to sys_attachment.list to download export.csv file.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-03-2022 10:44 AM
Excellent work.
Can you educate me to know more of this?
We have any documentation for this function?
What are the parameter your passing apart from table, recordId?
var attachment = new Attachment();
var attachmentRec = attachment.write(table, recordId, "export.csv", "text/csv", data);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-21-2019 04:19 AM
Hi Jack,
I tried using your code above and it works for me in xml format. However, an empty XML is getting attached to the record.
Can you please help what am I missing ?
Thanks in advance!!
Regards,
Sailee
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-12-2019 10:12 AM
hi saylee,
was your issue solved? i am facing same issue, can u please help me on this?
Above code is working for me for getting data in CSV format, but i want xml file. How can we achieve this?