Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Copying attachment from one instance to another

dinesh91n
Tera Expert

Hi guys, i have task to copy an attachment from an incident, from one instance to a change request in another instance using soap web services. Can anyone help me on this.

1 ACCEPTED SOLUTION

dinesh91n
Tera Expert

hi guys i found the solution


used these two to finish the code GlideStringUtils, GlideSysAttachment


revised code:


var StringUtil = new GlideStringUtil();


  var attachments = new GlideSysAttachment();


  var fileName, contentType;


  var gR = new GlideRecord('sys_attachment');


  gR.addEncodedQuery('table_name=incident^table_sys_id='+current.sys_id);


  gR.query();


  while(gR.next()){


  try {


  var binData =   attachments.getBytes(gR);


  var encData =   StringUtil.base64Encode(binData);


  gs.log('XML Respons for payload='+encData);


  var s = new sn_ws.SOAPMessageV2('Copy attachments', 'insert');


  s.setStringParameter('source', 'change_request:'+current.u_change_sysid);


  s.setStringParameter('payload', encData);


  s.setStringParameter('topic', 'AttachmentCreator');


  s.setStringParameter('agent', 'AttachmentCreator');


  s.setStringParameter('name', gR.file_name+':'+gR.content_type);


  var response = s.execute();


  var responseBody = response.getBody();


  var status = response.getStatusCode();


  }


  catch(ex) {


  var message = ex.getMessage();


  }


  }


View solution in original post

11 REPLIES 11

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi Dinesh,



Please refer the below link for more info.


http://www.servicenowguru.com/integration/sending-attachment-servicenow/


Hi pradeep,



        Thanks for the reply i have a doubt payload they have mentioned like 'base64 encoded binary (or ASCII) data', is this applicable for all file types


i tried the following code in BRULE



var gR = new GlideRecord('sys_attachment');


  gR.addEncodedQuery('table_name=incident^table_sys_id='+current.sys_id);


  gR.query();


  if(gR.next()){


  try {


  var s = new sn_ws.SOAPMessageV2('Copy attachments', 'insert');


  s.setStringParameter('source', 'change_request:'+current.u_change_sysid);


  s.setStringParameter('payload', 'base64 encoded binary (or ASCII) data');


  s.setStringParameter('topic', 'AttachmentCreator');


  s.setStringParameter('agent', 'AttachmentCreator');


  s.setStringParameter('name', gR.file_name+':'+gR.content_type);


  var response = s.execute();


  var responseBody = response.getBody();


  var status = response.getStatusCode();



it is copying the file but the file is corrupted


dinesh91n
Tera Expert

hi guys i found the solution


used these two to finish the code GlideStringUtils, GlideSysAttachment


revised code:


var StringUtil = new GlideStringUtil();


  var attachments = new GlideSysAttachment();


  var fileName, contentType;


  var gR = new GlideRecord('sys_attachment');


  gR.addEncodedQuery('table_name=incident^table_sys_id='+current.sys_id);


  gR.query();


  while(gR.next()){


  try {


  var binData =   attachments.getBytes(gR);


  var encData =   StringUtil.base64Encode(binData);


  gs.log('XML Respons for payload='+encData);


  var s = new sn_ws.SOAPMessageV2('Copy attachments', 'insert');


  s.setStringParameter('source', 'change_request:'+current.u_change_sysid);


  s.setStringParameter('payload', encData);


  s.setStringParameter('topic', 'AttachmentCreator');


  s.setStringParameter('agent', 'AttachmentCreator');


  s.setStringParameter('name', gR.file_name+':'+gR.content_type);


  var response = s.execute();


  var responseBody = response.getBody();


  var status = response.getStatusCode();


  }


  catch(ex) {


  var message = ex.getMessage();


  }


  }