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

Allen Andreas
Administrator
Administrator

Hi,

You'd want to create the record on the sys_attachment table first, then create your db_image record with the image field set to the sys_id of the sys_attachment record.

 

Normally, when you work with the db_image table within the platform and create a new record there first...it'll create the sys_attachment record as well to link to it, but since you're doing this via API, you'll need the attachment record there first, to then link the db_image to it instead.


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

Sheldon  Swift
ServiceNow Employee
ServiceNow Employee

Hi @Siddhesh2 - Are you saying that the image is added an attachment to the sys_attachment, not stored in the image field? Can you share your code?

Siddhesh2
Giga Guru

@Allen Andreas Do you have a complete solution or link to solution which I can refer? I tried inserting image to attachment table but when I try to download it then it does not work.

 

var fileData = base64contentofimage;

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

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

 

Siddhesh2_0-1723116519094.png

 

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