- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-02-2025 06:51 AM
Hello,
I am trying to create a Scripted REST Resource to receive attachments for a scoped application. The attachments are sent encoded in base64. It is working correctly when receiving .txt documents, but when receiving other types of attachments like jpeg or pdf, the file is attached but when performing a subsequent download, the file is corrupted.
here is my code:
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
var ticket = request.pathParams.ticketId;
var body = request.body.data;
var attachment;
try{
attachment = body.anexos;
var gr = new GlideRecord
('x_cdpdf_uai_200_item_de_integracao');
gr.addQuery ('ticket_externo', ticket);
gr.query();
if (!gr.hasNext()){
throw new Error ('Ticket não localizado.');
}
if (gr.next()){
//Tratar anexos
attachment.forEach(item =>
{
var anexo = new GlideSysAttachment();
var decodeData = gs.base64Decode(item.arquivo);
anexo.write(gr, item.nomeArquivo, decodeData);
});
gr.work_notes = "Attachment received";
gr.update();
response.setStatus(200);
response.setBody({
message: "Attachment received"
});
}
} catch(e){
response.setStatus(400);
response.setBody({error: "Erro ao incluir uma conversa." + e.message});
return;
}
})(request, response);
Any suggestion of what might be wrong?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-03-2025 08:17 PM
since you said your scripted REST API is in scoped app, you need to use this method writeBas64 which is available for scope app. Don't decode.
update script as this
Note: You will have to pass file name and content type so that system understands, ask 3rd party to send that if they are not sending it
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-02-2025 08:23 PM
Hello,
To the function .write it is suppose to have 4 parameters, make sure content type is correctly passed below is the list of for parameters in the order of arguments to the function .write as in docs
Name | Type | Description |
---|---|---|
record | GlideRecord | Record to which to attach the attachment. |
fileName | String | Attachment file name. |
contentType | String | MIME type of the attachment, such as image/png . Located in the Attachment [sys_attachment] table in the Content type field. |
content | String | Attachment content. |
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-03-2025 07:11 AM
Hi,
Thanks for the tip. I changed the line to include the MIME Type but the error remains the same.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-03-2025 06:32 PM
Can you check your scope, is it global or specific scope? If you are working on a scope then you might have to check functions specific to stopped GlideSysAttachment

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-03-2025 08:17 PM
There is another function to write base64 directly , can you try that ?
writeBase64(GlideRecord now_GR, String fileName, String contentType, String content_base64Encoded)
var attachment = new GlideSysAttachment();
var rec = new GlideRecord('incident');
var incidentSysID = 'ab1b30031b04ec101363ff37dc4bcbfc';
rec.get(incidentSysID);
var fileName = 'example.txt';
var contentType = 'text/csv';
var base64Encodedcontent = 'SSBhbSB0ZXh0Lg==';
var agr = attachment.writeBase64(rec, fileName, contentType, base64Encodedcontent);
gs.info('The attachment sys_id is: ' + agr);