The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Can we convert Base 64 image to png image using servicenow script

Kajal8
Tera Expert

Hello team,

I have Base 64 image string but I want to convert it to PNG format to create record in db_image table. Could somebody help in how to proceed.

3 REPLIES 3

Neeraj Modi
Tera Guru

Hi @Kajal8 

 

Yes, it is possible to convert a Base64 encoded image to a PNG image using a script in ServiceNow. You can do this by using the GlideSystem class in ServiceNow to create a new Attachment record, and then decoding the Base64 encoded image data and writing it to the newly created Attachment.

Here is an example of how you can convert a Base64 encoded image to a PNG image using a script in ServiceNow:

 

// Decode the Base64 encoded image data
var imageData = new GlideString(base64EncodedImage);
var binaryData = GlideEncryption.decode(imageData);

// Create a new Attachment record
var attachment = new GlideRecord('sys_attachment');
attachment.initialize();
attachment.setValue('table_name', 'YOUR_TABLE_NAME');
attachment.setValue('table_sys_id', 'YOUR_TABLE_SYS_ID');
attachment.setValue('file_name', 'image.png');
attachment.setValue('content_type', 'image/png');
attachment.setValue('data', binaryData);
attachment.insert();

 

Keep in mind that, if the image is not a PNG image, you should change the 'content_type' and the 'file_name' properties accordingly.

 

Please mark the suggestion as helpful, if you find it useful to you or others who wants to refer similar content.
Please mark the solution as correct, if the answer provided by me has resolved your query.


Thank you!

 

I tried to run this but it is giving error message "GlideString is not defined".