ServiceNow integration with Postman

dipanjansaha47
Tera Contributor

Hello,

 

I am trying to send an attachment via Scripzted Rest API from Postman to ServiceNow for the incident table.

For the attachment I have written the below code in Global scope application.

 

I am receiving an error "

Can't find method com.glide.ui.SysAttachment.write(string,string,string,string,string). (sys_ws_operation.3e37358093234a10c796718efaba10f8.operation_script;

"

 

Kindly let me know what is the exact issue in the code

 

Regards,

Dipanjan Saha

 

 

 

(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
    try {
        var requestBody = request.body.data;
        var shortDescription = requestBody.short_description;
        var description = requestBody.description;
        var fileData = requestBody.file_data; // Base64 encoded file data
        var fileName = requestBody.file_name;
        var contentType = requestBody.content_type; // e.g., "image/png"

        // Log the incoming request data for debugging
        gs.info('Request received: shortDescription=' + shortDescription + ', description=' + description + ', fileName=' + fileName + ', contentType=' + contentType);

        // Create Incident
        var incidentGr = new GlideRecord('incident');
        incidentGr.initialize();
        incidentGr.short_description = shortDescription;
        incidentGr.description = description;
        var incidentSysId = incidentGr.insert();
        
        gs.info('Incident created with sys_id=' + incidentSysId);

        // Validate the file data
        if (!fileData || !fileName || !contentType) {
            throw new Error('Missing file data, file name, or content type');
        }

        // Attach file to incident
        var attachment = new GlideSysAttachment();
        var decodedFileData = GlideStringUtil.base64DecodeAsBytes(fileData);
        attachment.write('incident', 'incidentSysId', 'fileName', 'contentType', 'decodedFileData');
        
        gs.info('File attached to incident with sys_id=' + incidentSysId);

        // Return the incident sys_id
        var responseBody = {
            sys_id: incidentSysId
        };
        response.setStatus(201);
        response.setBody(responseBody);
    } catch (ex) {
        gs.error('Error processing request: ' + ex.message);
        var errorResponse = {
            error: ex.message
        };
        response.setStatus(400);
        response.setBody(errorResponse);
    }
})(request, response);

 

2 ACCEPTED SOLUTIONS

Nicholas_Gann
Mega Guru

The error suggests you may not be using the GlideSysAttachment class correctly. Looking at the documentation, it seems to support that.

 

See below:

GlideSysAttachment - Global (servicenow.com)

 

Nicholas_Gann_0-1721652747643.png

Looking at your script above, you have fed .write() 5 strings instead of feeding it parameters inline with the image above

 

I would have expected:

var attachment = new GlideSysAttachment();
var decodedFileData = GlideStringUtil.base64DecodeAsBytes(fileData);
attachment.write(incidentGr, requestBody.file_name, requestBody.content_type, decodedFileData);

View solution in original post

GlideStringUtil.base64DecodedAsBytes does not seem to be documented, so I was skeptical around how it worked. I have tested it with an example in background script and it appears it works fine (provided you feed it a base64encoded string). Documentation here:

GlideStringUtil - Scoped, Global | ServiceNow Developers

 

In your postman example above you don't provide the base64 encoded image, can you confirm it's actually sending this and not "<convert your image to base 64 and paste it here>"

 

This is the background script example that does work, proving it should work if fed a base64 encoded image

 

 

var gr = new GlideRecord('incident');
gr.get('03b2b73d83eb8210e51a1530ceaad394');

var type = "image/png";
var content = "a base 64 encoded image, which this site does not allow me to include as it breaches some kind of policy";
var contentDecoded = GlideStringUtil.base64DecodeAsBytes(content);

new GlideSysAttachment().write(gr, 'testpng.png', type, contentDecoded);

 

 

 

I created the base64 string using the snipping tool and encoded using a website but I was prevented from including it in the above example due to some form of restriction

View solution in original post

5 REPLIES 5

dipanjansaha47
Tera Contributor

Hello,

 

It worked for me now.. The url i was using to convert the image to base 64 was giving a wrong output so I changed the URL to "https://base64.guru/converter/encode/image" and converted my image.

now everything is working fine for me.

Thanks a lot for your help.