Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Issue with base64 image after conversion

Hari1
Mega Sage

Hi,

I have an issue after the base64 string is converted to an image and attached to the incident record. The image is blank. Please find the below screenshot.

Hari1_0-1725882943644.png

 

Below is the code that i am using:

 

Client Script - OnChange:

var ajx = new GlideAjax('AttachRec');
ajx.addParam('sysparm_name', 'AttachInc');
ajx.addParam('url', dataURL); // Base64 URL
ajx.addParam('inc', g_form.getUniqueValue()); //incident record sys_id
ajx.getXML(attachImg);

function attachImg(response) {
           var answer = response.responseXML.documentElement.getAttribute("answer");
           g_form.addInfoMessage("Attachment added successfully and answer: " + answer);
}

Script Include:

var AttachRec = Class.create();
AttachRec.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    AttachInc: function() {

        var incUrl = this.getParameter('url');
        var incSysId = this.getParameter('inc').toString();
        var base64Bytes = GlideStringUtil.base64DecodeAsBytes(incUrl);
       
        var rec = new GlideRecord('incident');
        rec.get(incSysId);

        var attachment = new GlideSysAttachment();
	var fileName = "newImg.png";
        var contentType = '';
        var agr = attachment.write(rec, fileName, contentType, base64Bytes);
       
       return attachment.sys_id;
    },
    type: 'AttachRec'
});

 Thanks

5 REPLIES 5

@Rajesh Chopade1I was able to achieve the functionality of adding the screenshot for the attachment table but for some reason i see the attachments are getting added twice. The old image(previous image) and the new image both are getting added to the attachment record.