how can i export data into excel [.xlsx] via script

snavuluri
Giga Contributor

Hi All,

 Please guide on,  how i can export data into excel [.xlsx] via script.

 

Regards,

Suresh.

1 ACCEPTED SOLUTION

@snavuluri 

we forgot adding the underscore

now it should work fine

g_navigation.open('/' + tableName + '_list.do?EXCEL&sysparm_view=ess', '_blank');

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

29 REPLIES 29

Hi,

you might have to develop some UI page and ask user to set some filter and then download the record

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

snow123
Kilo Contributor

Hi All,

   Any one can give suggestions for script.

 

Regards,

Suresh.

Please check below links for UI page help

https://servicenowguru.com/system-ui/glidedialogwindow-advanced-popups-ui-pages/

https://www.learnnowlab.com/UI-Page-example/

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

snow123
Kilo Contributor

Hi ,

     Please suggest on this. Not working the below code in UI Action .

var output = "Number,Company,Short_Description,Incident State"; //header for csv file
var table = "incident";
var recordId = ""; //using for attachment file
var gr = new GlideRecord(table);

var query = current.getParmVal('sysparm_query');
gr.addEncodedQuery(query);
gr.query();
var count = 0;
while (gr.next()) {
count++;
output += "\n" + gr.number + "," + gr.company.getDisplayValue() + "," + gr.short_description + "," + gr.incident_state.getDisplayValue();
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);
}

 

Regards,

Suresh.

Hi,

update as this

var output = "Number,Company,Short_Description,Incident State"; //header for csv file
var table = "incident";
var recordId = ""; //using for attachment file
var gr = new GlideRecord(table);

var query = gs.action.getGlideURI().getMap().get('sysparm_query');
gr.addEncodedQuery(query);
gr.query();
while (gr.next()) {
    output += "\n" + gr.number + "," + gr.company.getDisplayValue() + "," + gr.short_description + "," + gr.incident_state.getDisplayValue();
}
gs.info(recordId);

writeAttachmentFile(output,current);

function writeAttachmentFile(data,currentObject) {
    var attachment = new Attachment();
    var attachmentRec = attachment.write(currentObject, "export.csv", "text/csv", data);
}

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader