- 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-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-04-2025 10:46 AM
Hi Ankur
In the end I found out that the problem was that the files I was using for testing were not in binary format. I did the conversion again and got it working with writeBase64 as suggested.
Thank you very much.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-04-2025 10:38 PM
Glad to help.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader