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,418 Views
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-20-2019 02:22 AM
Hi,
I am using the same code for the HR Application to generate report and it is giving 401 authentication error. If I tried manually, using my credentials it is working fine but via code it is not working.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-20-2019 05:45 AM
Have you tried using Scoped GlideRecord? Also, are you running the script from the same scope as the application?
Regards
Prasun
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-20-2019 09:21 AM
If you're using my approach with the REST message, be sure that on the line "rm.setBasicAuth()", you're supplying a username and password that has access to the scoped application (in strings).
var ritm = new GlideRecord('sc_req_item');
ritm.addQuery('request', current.sys_id);
ritm.addQuery('approval', 'approved');
ritm.query();
if(ritm.getRowCount() > 0) {
var rm = new sn_ws.RESTMessageV2();
rm.setHttpMethod('GET');
gs.log(current.number + ' ; ' + current.sys_id + ' ; ' + current.getTableName());
var url = gs.getProperty('glide.servlet.uri') + 'u_credit_request_list.do?XLS&sysparm_query=u_request%3D' + current.sys_id + '&sysparm_first_row=1&sysparm_view=';
rm.setEndpoint(url);
rm.setBasicAuth('attach_admin','attach_admin');
rm.saveResponseBodyAsAttachment(current.getTableName(),current.sys_id,current.number+'.xls');
var response = rm.execute();
//action.setRedirectURL(current.getTableName()+'.do?sys_id='+current.sys_id);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-11-2021 03:36 AM
Hi Jack,
How to use the script you mentioned in UI Action. Please suggest.
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);
}