Error - Cant find method com.glide.ui.SysAttachment

Rama26
Tera Contributor

Below script i written in scripted rest API but not working as expected.

(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

    var incidentNumber = 'INC0009009';
    var grIncident = new GlideRecord('incident');
    grIncident.addQuery('number', incidentNumber);
    grIncident.query();

    if (grIncident.next()) {
        var shortDescription = grIncident.getValue('short_description');
        var priority = grIncident.getValue('priority');
        var state = grIncident.getValue('state');
        var assignedTo = grIncident.getDisplayValue('assigned_to'); // Using getDisplayValue to get the display name

        var csvContent = "ShortDescription,Priority,State,AssignedTo\n";
        csvContent += '"' + shortDescription + '","' + priority + '","' + state + '","' + assignedTo + '"\n';

        var attachment = new GlideSysAttachment();
        var excelFileSysID = attachment.write(grIncident.getTableName(), grIncident.sys_id.toString(), 'incident_data.csv', 'text/csv', csvContent);

        gs.info('CSV file created with sys_id: ' + excelFileSysID);

    } else {
        gs.info('No incident found with number ' + incidentNumber);
    }

})(request, response);
Error is:
{
  "error": {
    "message": "Script Evaluation Exception",
    "detail": "Can't find method com.glide.ui.SysAttachment.write(string,string,string,string,org.mozilla.javascript.ConsString). (sys_ws_operation.9708d678c3130210be043bec0501314a.operation_script; line 23)"
  },
  "status": "failure"
}
4 REPLIES 4

SN_Learn
Kilo Patron
Kilo Patron

Hi @Rama26 ,

 

The 'GlideSysAttachment - write' takes 4 parameter as below:

 

GlideSysAttachment - write(GlideRecord record, String fileName, String contentType, String content)

 

 

SN Doc:  GlideSysAttachment - Global 

 

So, after updating it will be as below:

(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

    var incidentNumber = 'INC0009009';
    var grIncident = new GlideRecord('incident');
    grIncident.addQuery('number', incidentNumber);
    grIncident.query();

    if (grIncident.next()) {
        var shortDescription = grIncident.getValue('short_description');
        var priority = grIncident.getValue('priority');
        var state = grIncident.getValue('state');
        var assignedTo = grIncident.getDisplayValue('assigned_to'); // Using getDisplayValue to get the display name

        var csvContent = "ShortDescription,Priority,State,AssignedTo\n";
        csvContent += '"' + shortDescription + '","' + priority + '","' + state + '","' + assignedTo + '"\n';

        // var attachment = new GlideSysAttachment();
        // var excelFileSysID = attachment.write(grIncident, 'incident_data.csv', 'text/csv', csvContent);

        gs.info('CSV file created with sys_id: ' + excelFileSysID);

    } else {
        gs.info('No incident found with number ' + incidentNumber);
    }

})(request, response);

 

Output:

SN_Learn_0-1720359551103.png

 

 

Mark this as Helpful / Accept the Solution if this helps

----------------------------------------------------------------
Mark this as Helpful / Accept the Solution if this helps.

SN_Learn
Kilo Patron
Kilo Patron

Thanks @Rama26 for marking as helpful.

If it resolved your query please consider marking 'Accept as solution' and close the thread.

----------------------------------------------------------------
Mark this as Helpful / Accept the Solution if this helps.

Rama26
Tera Contributor

for CSV it's working but i need to attach xlsx file

Hi @Rama26 ,

 

Try the below suggested KB

Attach an excel file on a record by making REST API call to ServiceNow Excel Web Service 

 

Mark this as Helpful / Accept the Solution if this helps

----------------------------------------------------------------
Mark this as Helpful / Accept the Solution if this helps.