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,415 Views
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-21-2019 05:44 AM
Here's a method I had used to create an attachment and attach it to a record. You might need to tweak the glide record and url variable to reflect what you need.
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
‎01-21-2019 11:59 PM
Hi Ryan,
Thanks for your inputs!! This has helped me to achieve half of my requirement. Is there any way to import that same XML in another instance via script ? Currently I am trying to work this our using import set REST API to import the XML from the record I had export the and attached the files.
Please let me know if there is any best possible solution to achieve this scenario ?
Regards,
Sailee
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-22-2019 04:58 AM
Again you may need to tweak this a bit to fit your requirements, but you could try something like this:
var inc = new GlideRecord('incident');
inc.addQuery('sys_id', current.table_sys_id);
inc.query();
if(inc.next()){
try {
var rest_record ='[insert rest message record]';
var r = new sn_ws.RESTMessageV2(rest_record, 'POST');
r.setBasicAuth("[create user account for this]","[add password]");
}
r.setStringParameterNoEscape('incident_number', inc.number);
var stringUtil = GlideStringUtil;
var sa = new GlideSysAttachment();
var binData = sa.getBytes(current); //gets the data in binary form
var encData = stringUtil.base64Encode(binData); //then converts it to Base64 encoding which we can then use
r.setStringParameterNoEscape('filename_inc', current.file_name);
r.setStringParameterNoEscape('attachment_inc', encData);
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
}
catch(ex) {
var message = ex.getMessage();
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-31-2019 11:18 PM
Hi Paul,
I tried using this method, but however it just create a new record rather than importing the existing encoded record that we pass with "encData" variable.
Could you please suggest what am I missing here?
Regards,
Sailee
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-01-2019 07:02 AM
I tried the solutions posted here successfully - thanks Ryan - only to realise I was overcomplicating things and completely forgot the obvious solution (in my case, I didn't actually need the attachment to be saved automatically, just for a export UI action to not be client based, and hence supported by the portal form widgets).
In case it results useful to anyone, I'm leaving it here.