Attach image to image field in db_image table

Siddhesh2
Giga Guru

I have a requirement where ServiceNow instance will receive the image through API and we need to store it to image field on db_image table. I tried using GlideSysAttachment() but it is attaching image as attachment and not storing it on actual image field.

 

Siddhesh2_0-1723039167383.png

 

1 ACCEPTED SOLUTION

Hi @Siddhesh2 - Your file data is base64 encoded, so you will either need to use writeBase64 or decode the content before using write (see GlideSysAttachment). The example below should work; however, I recommend following the examples in the documentation.

 

var base64contentofimage = 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/wcAAgAB/wHcD9UAAAAASUVORK5CYII=' // This base64 string represents a 1x1 pixel transparent PNG image

var fileData = base64contentofimage;
var decodedBytes = GlideStringUtil.base64DecodeAsBytes(fileData);

var rec = new GlideRecord('incident');
rec.addQuery('sys_id', '6725368f2bb302102c5e8b4cad01a000');
rec.query();

if (rec.next()) {
    var sa = new GlideSysAttachment();
    sa.write(rec, 'test3.png', 'image/png', decodedBytes)
    var responseBody = {};
    responseBody.incNumber = '123';
    responseBody.status = "Success";
    //response.setBody(responseBody);
} else {
    var responseBodyFailure = {};
    responseBodyFailure.status = "Failure";
    //response.setBody(responseBodyFailure);
}

 

View solution in original post

6 REPLIES 6

Hi,

You mentioned that the attachment was getting attached to the record on the db_image, but not to the image field. So my reply is in reference to that. If you're still trying to work through even getting the image attached through your current solution, then that is a different question than what you mentioned originally.

 

As I've called out to answer your exact question...in order to get the image to link to the image field within a db_image record, it first needs to be a sys_attachment record, then you can use the sys_id from that sys_attachment record and set the image field to that sys_id within the relevant db_image record.


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

bhargav ram
Tera Contributor

I have stored image in attachment table. How to save that image in db_image table?