Error while decoding attachment in base64

FredericoM
Tera Contributor

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?

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@FredericoM 

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.

GlideSysAttachment - Scoped 

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

var anexo = new GlideSysAttachment();
anexo.writeBase64(gr, fileName, contentType, item.arquivo);

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

View solution in original post

7 REPLIES 7

Sateesh Kumar D
ServiceNow Employee
ServiceNow Employee

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

 

Hi,

Thanks for the tip. I changed the line to include the MIME Type but the error remains the same.

 

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

There is another function to write base64 directly , can you try that ?

 

writeBase64(GlideRecord now_GR, String fileName, String contentType, String content_base64Encoded)

 

https://www.servicenow.com/docs/bundle/xanadu-api-reference/page/app-store/dev_portal/API_reference/...

 

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);