Export to CSV or XML using script

benl
Giga Contributor

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

18 REPLIES 18

Ganesh73
Tera Contributor

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.

Have you tried using Scoped GlideRecord? Also, are you running the script from the same scope as the application?


Regards

Prasun

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);
  
}

snow123
Kilo Contributor

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);
}