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

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

dipanjansaha47
Tera Contributor

Thank you and now the incident is getting created with the attachment getting added as well. But after downloading the file from the ServiceNow incident form, i am getting the attached error message.

The body that i am sending from the postman is 

 


{
"short_description": "Issue with Application_Dipanjan",
"description": "Detailed description of the issue_Dipanjan",
"file_name": "issue_dipanjan.png",
"content_type": "image/png",
"file_data": "<convert your image to base 64 and paste it here>"
}

can you let me know the issue in the body of the POSTMAN?

 

Thanks in advance.

 

Regards,

Dipanjan Saha

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

dipanjansaha47
Tera Contributor

Hello,

 

I tried using the background script that you provided and the image alo got attached to the target incident record. However when i downloaded the image the same issue is showing up. The Base 64 image code cannot be copied here since the content does not allow more than 250,000 characters. 

My email id id dipanjansaha1991@gmail.com.. Can you send me the base 64 code there? or I can send mine to your email address just to verify if this is a system issue.

Thanks in advance.

Regards,

Dipanjan Saha