- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-07-2024 07:00 AM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-08-2024 08:42 AM
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);
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-07-2024 07:56 AM - edited 08-07-2024 07:57 AM
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-07-2024 08:13 AM
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-08-2024 04:30 AM
@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);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-08-2024 08:42 AM
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);
}