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

abdul_qulatein
Giga Expert

Hi Kris,



I tried your approach to no avail. Is this functionality still working for you or have you had to make changes post new release?


Hi Abdul,



It may not be specifically relevant to your requirements but it maybe worth noting that there a cute REST attachment api available in Geneva


Attachment API


which is not subject to a Max of 5MB


AttachmentCreator SOAP Web Service - ServiceNow Wiki


..


6 FAQ


  • How many attachments can you send in one message
    • Only one attachment per message
  • Is there size limit on the attachment in the message
    • Attachments are limited to a maximum of 5MB.

Best Regards



Tony


Thanks Tony, I have now implement this functionality.


Inactive_Use603
Kilo Expert

this might help to understand the approach of sending files via Web Service ServiceNow Attachment Files via Web Service - YouTube