Sending attachment with outbound SOAP message

kris10
Giga Contributor

Hi

 

I'm using the below code in Incident business rule in Dev ServiceNow to insert an incident in my Test ServiceNow using SOAP Message. It is working fine.

 

But I also want to send over the attachments using the SOAPMessage. Is it possible to send attachments using SOAPMessage or do I need to define a seperate soapEnvelope and then use createElement?

 

 

var s = new SOAPMessage('Test-Incident', 'insert');

s.setStringParameter('u_vendor_reference', current.u_vendor_reference);

s.setStringParameter('caller_id', current.caller_id);

s.setStringParameter('location', current.location);

s.setStringParameter('u_contact_number', current.u_contact_number);

s.setStringParameter('description', current.description);

s.setStringParameter('cmdb_ci', current.cmdb_ci);

s.setStringParameter('u_source', current.u_source);

s.setStringParameter('u_raised_by', current.u_raised_by);

s.setStringParameter('assignment_group', current.assignment_group);

s.setStringParameter('assigned_to', current.assigned_to);

s.setStringParameter('state', current.state);

s.setStringParameter('priority', current.priority);

s.setStringParameter('category', current.category);

s.setStringParameter('u_subcategory', current.u_subcategory);

s.setStringParameter('short_description', current.short_description);

s.setStringParameter('description', current.description);

var response = s.post();

 

Thank you

1 ACCEPTED SOLUTION

kris10
Giga Contributor

Thanks to Kalai and Subajit.



Based on your responses I created a SOAP Message for target instance and used the below script to check the response first and then send the attachments.



var s = new SOAPMessage('Test-Incident', 'insert');


s.setStringParameter('u_vendor_reference', current.u_vendor_reference);


s.setStringParameter('caller_id', current.caller_id);


s.setStringParameter('location', current.location);


s.setStringParameter('u_contact_number', current.u_contact_number);


s.setStringParameter('description', current.description);


s.setStringParameter('cmdb_ci', current.cmdb_ci);


s.setStringParameter('u_source', current.u_source);


s.setStringParameter('u_raised_by', current.u_raised_by);


s.setStringParameter('assignment_group', current.assignment_group);


s.setStringParameter('assigned_to', current.assigned_to);


s.setStringParameter('state', current.state);


s.setStringParameter('priority', current.priority);


s.setStringParameter('category', current.category);


s.setStringParameter('u_subcategory', current.u_subcategory);


s.setStringParameter('short_description', current.short_description);


s.setStringParameter('description', current.description);


var response = s.post();




gs.addInfoMessage(response.toString());




var helper = new XMLHelper(response);  


var obj = helper.toObject();  


 


logObj(obj, "*" );




function logObj(obj, sep){  


    for (x in obj){  


          if (typeof obj[x] != "function"){  


    if(x == "sys_id" && JSUtil.notNil(obj[x])){


      gs.log(sep + x + ":: " + obj[x]);  


    //Attachment Creator


    var grSysAtt = new GlideRecord('sys_attachment');


    grSysAtt.addQuery('table_sys_id', current.sys_id);


    grSysAtt.query();


    while (grSysAtt.next()){


   


    gs.log(grSysAtt.table_sys_id + 'sys id of the record foound');


    var sa = GlideSysAttachment();


    var binData = sa.getBytes(grSysAtt);


    var encData = GlideStringUtil.base64Encode(binData);


    gs.log("Encoded data:" + encData);


    var sm = new SOAPMessage('Test-Attachments', 'insert');


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


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


    sm.setStringParameter('payload', encData);


    sm.setStringParameter('name', grSysAtt.file_name+':'+grSysAtt.content_type);


    sm.setStringParameter('source', grSysAtt.table_name+':'+obj[x]);


    var response2 = sm.post();


          }  


    }


    }


          logObj(obj[x], sep + "*" );  


    }  


}


View solution in original post

8 REPLIES 8

kris10
Giga Contributor

After going through some community posts I modified my code as below. Incident is getting created in the target instance but the attachments are not carried over to target instance.



var s = new SOAPMessage('Test-Incident', 'insert');


s.setStringParameter('u_vendor_reference', current.u_vendor_reference);


s.setStringParameter('caller_id', current.caller_id);


s.setStringParameter('location', current.location);


s.setStringParameter('u_contact_number', current.u_contact_number);


s.setStringParameter('description', current.description);


s.setStringParameter('cmdb_ci', current.cmdb_ci);


s.setStringParameter('u_source', current.u_source);


s.setStringParameter('u_raised_by', current.u_raised_by);


s.setStringParameter('assignment_group', current.assignment_group);


s.setStringParameter('assigned_to', current.assigned_to);


s.setStringParameter('state', current.state);


s.setStringParameter('priority', current.priority);


s.setStringParameter('category', current.category);


s.setStringParameter('u_subcategory', current.u_subcategory);


s.setStringParameter('short_description', current.short_description);


s.setStringParameter('description', current.description);




var StringUtil = GlideStringUtil;


var attachments = s.createElement(instance, 'imp:attachments');




var gr = new GlideRecord('sys_attachment');  


gr.addQuery('table_sys_id',current.sys_id);


gr.addQuery('table_name', current.getTableName());


gr.query();  


while (gr.next()) {  


      var sa = new GlideSysAttachment();


  var binData = sa.getBytes(gr);


  var encData = StringUtil.base64Encode(binData);



  var attach = s.createElement(attachments, "com:attachment", encData);


      s.setAttribute(attach, "href", "cid:"+encData);  


      s.setAttribute(attach, "ContentID", encData);  


      s.setAttribute(attach, "action", "add");  


  s.setAttribute(attach, "name", gr.file_name);


  s.setAttribute(attach, "type", gr.content_type);


  s.setAttribute(attach, "len", gr.size_bytes);


  s.setAttribute(attach, "charset", "");


  s.setAttribute(attach, "attachmentType", "file");


}  



var response = s.post();


Subhajit1
Giga Guru

You can use this snippet:-



var attrecord = new GlideRecord("sys_attachment");


attrecord.addQuery("table_sys_id", 'current sys id');


attrecord.query();


var sa = new Packages.com.glide.ui.SysAttachment();


var gattachcontent = StringUtil.base64Encode(sa.getBytes(attrecord));



You will have to first Encode the attachment to Base 64 format and then send it via a SOAP message.


kris10
Giga Contributor

Thanks to Kalai and Subajit.



Based on your responses I created a SOAP Message for target instance and used the below script to check the response first and then send the attachments.



var s = new SOAPMessage('Test-Incident', 'insert');


s.setStringParameter('u_vendor_reference', current.u_vendor_reference);


s.setStringParameter('caller_id', current.caller_id);


s.setStringParameter('location', current.location);


s.setStringParameter('u_contact_number', current.u_contact_number);


s.setStringParameter('description', current.description);


s.setStringParameter('cmdb_ci', current.cmdb_ci);


s.setStringParameter('u_source', current.u_source);


s.setStringParameter('u_raised_by', current.u_raised_by);


s.setStringParameter('assignment_group', current.assignment_group);


s.setStringParameter('assigned_to', current.assigned_to);


s.setStringParameter('state', current.state);


s.setStringParameter('priority', current.priority);


s.setStringParameter('category', current.category);


s.setStringParameter('u_subcategory', current.u_subcategory);


s.setStringParameter('short_description', current.short_description);


s.setStringParameter('description', current.description);


var response = s.post();




gs.addInfoMessage(response.toString());




var helper = new XMLHelper(response);  


var obj = helper.toObject();  


 


logObj(obj, "*" );




function logObj(obj, sep){  


    for (x in obj){  


          if (typeof obj[x] != "function"){  


    if(x == "sys_id" && JSUtil.notNil(obj[x])){


      gs.log(sep + x + ":: " + obj[x]);  


    //Attachment Creator


    var grSysAtt = new GlideRecord('sys_attachment');


    grSysAtt.addQuery('table_sys_id', current.sys_id);


    grSysAtt.query();


    while (grSysAtt.next()){


   


    gs.log(grSysAtt.table_sys_id + 'sys id of the record foound');


    var sa = GlideSysAttachment();


    var binData = sa.getBytes(grSysAtt);


    var encData = GlideStringUtil.base64Encode(binData);


    gs.log("Encoded data:" + encData);


    var sm = new SOAPMessage('Test-Attachments', 'insert');


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


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


    sm.setStringParameter('payload', encData);


    sm.setStringParameter('name', grSysAtt.file_name+':'+grSysAtt.content_type);


    sm.setStringParameter('source', grSysAtt.table_name+':'+obj[x]);


    var response2 = sm.post();


          }  


    }


    }


          logObj(obj[x], sep + "*" );  


    }  


}