MIME type is required to be added dynamically when attaching a document.

imkhan
Tera Contributor

Hi Everyone,

We are getting attachment data from a 3rd party in base 64, so we had to decode it. It is working fine, and a MIME ID is needed to set it based on the attachment. If it is a PNG type attachment, then the MIME type should be image/png. How to achieve this.

 

fetchAndAddAttachments: function(docID, reqNo, accesstoken, fileName, mimeType) {
try {

var r = new sn_ws.RESTMessageV2('Integration-D', 'Download');
r.setStringParameterNoEscape('access_token', accesstoken);
r.setStringParameterNoEscape('DocumentID', docID);
var response = r.execute();
var responseBodyText = response.getBody();
var httpStatus = response.getStatusCode();
gs.info("fetchAndAddAttachments: Response Status" + httpStatus);
gs.info("fetchAndAddAttachments: Response Body" + responseBodyText);

if (httpStatus != 200) {
gs.error('fetchAndAddAttachments: Failed to fetch' + docID);
return;
}

var responseBody;
try {
responseBody = JSON.parse(responseBodyText);
} catch (e) {
gs.error("fetchAndAddAttachments: JSON Parse Error " + e.message);
return;
}


var base64Content = responseBody.Data;
var grAttachment = new GlideSysAttachment();
var targetTable = 'incident';
var target = new GlideRecord(targetTable);
target.addQuery('number', reqNo);
target.query();

if (target.next()) {
 grAttachment.write(target, fileName, mimeType, GlideStringUtil.base64DecodeAsBytes(base64Content));

}

Thanks!

5 REPLIES 5

Ankur Bawiskar
Tera Patron
Tera Patron

@imkhan 

the 3rd party should send the file type, it's just that they need to send a file type i.e. image/jpeg etc in the request body

You can use your own logic to determine the file type based on the starting content of base64 but better if 3rd party sends it, so you need not do additional processing of file.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader