SERVICENOW ATTACHMENT

BRUNO BANDEIRA
Tera Guru

Hello everybody!

 

Does any one have any idea how to send a servicenow record from one instance to another? I have tried everything, but It just won't work!

I've been receiving errors on octet-stream type or even the file type. I put the code bellow:


sendAttachment: function(ticket, record) {

try {
var output = {
hasError: false
};

// CARREGA anexo do registro indicado
var attachmentGR = new GlideRecord('sys_attachment');
attachmentGR.addQuery('sys_id', record);
attachmentGR.query();
if (attachmentGR.next()) {
var fileName = attachmentGR.file_name.toString();
var contentType = attachmentGR.content_type;
var attachmentSysId = attachmentGR.sys_id;
}

// Recupera o binário do anexo
var stream = new GlideSysAttachmentInputStream(attachmentSysId);
var byteStream = new Packages.java.io.ByteArrayOutputStream();
stream.writeTo(byteStream, 0, 0);

var request = new sn_ws.RESTMessageV2();
request.setEndpoint(this.baseurl + '/api/now/attachment/file?table_name=incident&table_sys_id=' + ticket + '&file_name=' + encodeURIComponent(fileName));

request.setHttpMethod('POST');
request.setBasicAuth(this.username, this.password);
request.setRequestHeader('Content-Type', contentType);
request.setRequestHeader('Accept', 'application/json');
request.setRequestBody(byteStream.toByteArray());

var response = request.execute();
var httpStatus = response.getStatusCode();
var responseBody = response.getBody();

if (httpStatus < 200 || httpStatus >= 300) {
throw output;
}

return output;

} catch (error) {
output.hasError = true;
output.httpStatus = httpStatus;
output.errorMessage = responseBody;
}
}

 

I have sucessfuly send a file, but it's content get on the other side broken. If I use the content type of the table, some times it gets a file type error

 

I have tried to mount a multipart sending too, but it fails!

 

Any help would be appreciated!

1 ACCEPTED SOLUTION

And lastly, is this one-time transfer or do you aim to do it regularly?

 

For the one-timer it might be possible to export it (but still the target record must be defined).

 

Debug: check the target sys_attachment table, perhaps the record is there you just cannot see it in any form as the associated sys ID will be unknown.

———
/* If my response wasn’t a total disaster ↙️ drop a Kudos or Accept as Solution ↘️ Cheers! */


View solution in original post

5 REPLIES 5

parabéns @BRUNO BANDEIRA 🤩

———
/* If my response wasn’t a total disaster ↙️ drop a Kudos or Accept as Solution ↘️ Cheers! */