Announcing the Global SNUG Board of Directors. Learn more here

Help
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

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

Prasun
Mega Guru

Do something like this




var attachment = new Attachment();


  var fileName = 'xyz.csv';


  var content_type = 'text/csv';


  var fileBytes='<DEFN>,,\n';


  fileBytes+='<NUM>,<JOBTYPE>,<COMPANY>\n';



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() )


{




fileBytes+=gr.number+','+gr.u_job_type+','+gr.company;


fileBytes+='<EOD>,,';


}


attachment.write('tablename', recordsysid, fileName, content_type, fileBytes);


benl
Giga Contributor

Thanks Prasun,



This script returns the following error. I don't know why it wouldn't work. b88c5ace4fa3e2001ff2faf11310c7d0 is the sys ID of a wm_task record.   Any Ideas?




"b88c5ace4fa3e2001ff2faf11310c7d0" is not defined.


  Caused by error in script at line 45



  42:


  43: }


  44:


==> 45: attachment.write('wm_task', b88c5ace4fa3e2001ff2faf11310c7d0, xyz, content_type, fileBytes);


attachment.write('wm_task', sys_id, xyz, content_type, fileBytes);



that sys_id should be the record sys_id of the table where you try to attach the csv file. not the record sys_id of the gr. and please remove xyz and give the fileName variable declared.


benl
Giga Contributor

Thanks again Prasun, The error is still the same when using the record sys_id of the table I am trying to attach the file.