- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-16-2017 03:36 AM
Hello Experts,
We are performing a third-party incident Integration, we need to send attachments to the third party while we create a new incident via business rule only.
we have written a business rule which has 2 fields: FileName & FileType through which we need to send the attachments to the third-party.
Can anyone help on how to send the attachments to the third-party through the below business rule.
Thanks in advance
Khozema Attar
Solved! Go to Solution.
- Labels:
-
Integrations
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-02-2022 06:38 AM
I am able to send attachment but after download that attachment that file(png) is not opening and differ in size
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-16-2017 07:19 AM
Hi Khozema,
Below is your updated script:
(function executeRule(current, previous /*null when async*/ ) {
var attachment = new GlideRecord('sys_attachment');
attachment.addQuery('table_sys_id', current.sys_id);
attachment.addQuery('table_name', current.getTableName());
attachment.query();
while (attachment.next()) {
sendAttachment(attachment);
}
function sendAttachment(att) {
try {
var StringUtil = new GlideStringUtil();
var file_name = att.file_name.toString();
var gsa = GlideSysAttachmentInputStream(att.sys_id.toString());
var baos = new Packages.java.io.ByteArrayOutputStream();
gsa.writeTo(baos);
baos.close();
var base64EncodedData = StringUtil.base64Encode(baos.toByteArray());
sendRequest(base64EncodedData, file_name);
} catch (Exception) {
gs.log('Failed sending Attachment to due to Exception: ' + Exception);
}
}
function sendRequest(encData, file_name) {
//Set up variables
s.setStringParameter('attachment.filename', file_name);
s.setStringParameter('attachment.type', encData);
}
})(current, previous);
Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-16-2017 11:25 PM
Hello Ankur ,
Thank you for the updated script, but there seems to be a problem with the attachments of size greater than 5MB.
When I send the attachment of size greater than 5MB it returns "ClientUnmarshalling Error: Java heap space" in the response body.
However, the script works fine when we are sending attachments of less than 5MB.
Can you please help me out on where we are going wrong.
Please find screenshot :
Thanks & Regards
Khozema attar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-17-2017 03:14 AM
Hi Khozema,
As per my knowledge it is something to do at the receiving end. Do they have the capability to accept base64encoded data for file larger than 5MB.
Is the third party app to which you are connecting via API call a Java based?
Because it seems the error is related to Java application
Following links can be helpful to you
java - OutofmemoryError while uploading a hugefile using cxf webservices - Stack Overflow
One suggestion I would give is you try to consume the API using SOAP UI tool which allows to send API call and use file of 4MB and file of 4.5MB and file of 5.2/5.5MB and check whether the same error you are getting.
If you are getting the same error then it is something to do with the receiving end.
Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-05-2017 11:55 PM
Hello Ankur,
For handling attachments , coming from third-party(incoming attachments) , we are facing issues with it.
The problem is attachment is getting attached on the incident form but while opening it is giving an error.
Can you help me with this issue, it will be of great help.
For handling incoming attachments we have written an OnAfter transform Script as:
(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {
if(ignore!= true && error != true){
var StringUtil = GlideStringUtil;
var attachment = new Attachment();
attachment.write('incident',target.sys_id,source.u_attachment1_name,'',StringUtil.base64DecodeAsBytes(source.u_attachment1));
}
})(source, map, log, target);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-21-2019 03:55 AM
Hi Ankur,
thank you for the code.
I have tried in my personal instances. the attachment transmitted to other instance successfully, however, when we tested with image file, it was transferred to the other system successfully but file is not readable.
May I know where we have gone wrong.
Code Snippet I am using to transfer attachment to other system
(function executeRule(current, previous /*null when async*/) {
var gr = new GlideRecord('sys_attachment');
gr.orderByDesc("sys_created_on");
gr.addQuery('table_sys_id', current.sys_id);
gr.addQuery('table_name', current.getTableName());
//gr.addQuery('sys_created_by','!=','CCBF.Vology.Integration');
gr.addQuery('sys_id','NOT IN',current.u_transferred_attachment_files);
gr.query();
gs.addInfoMessage('before the soap loop');
while(gr.next()){
sendAttachment(gr);
}
function sendAttachment(gr)
{
gs.addInfoMessage('enter into attachment loop');
try {
var separator='';
if(!current.u_transferred_attachment_files.nil())
{
separator = ',';
}
current.u_transferred_attachment_files = current.u_transferred_attachment_files + separator + gr.sys_id;
var StringUtil = GlideStringUtil;
var file_name = gr.file_name.toString();
var gsa = GlideSysAttachmentInputStream(gr.sys_id.toString());
var baos = new Packages.java.io.ByteArrayOutputStream();
gsa.writeTo(baos);
baos.close();
var base64EncodedData = StringUtil.base64Encode(baos.toByteArray());
sendRequest(base64EncodedData, file_name);
} catch (Exception) {
gs.addInfoMessage('Failed sending Attachment to due to Exception: ' + Exception);
}
function sendRequest(encData, fileName)
{
gs.addInfoMessage('enter into the request loop');
var s = new sn_ws.SOAPMessageV2('Dev 45388', 'insert');
s.setStringParameterNoEscape('insert.u_number',current.correlation_id );
s.setStringParameterNoEscape('insert.u_correlation_id', current.number);
//s.setStringParameterNoEscape('insert.u_description', externalSystem);
s.setStringParameterNoEscape('insert.u_work_notes', 'attachments added');
s.setStringParameterNoEscape('insert.u_comments', current.comments.getJournalEntry(1));
s.setStringParameterNoEscape('insert.u_attachment_name', fileName);
s.setStringParameterNoEscape('insert.u_attachment1', encData);
gs.addInfoMessage('ebter the soap loop2');
var response = s.execute();
var responseBody = response.getBody();
var status = response.getStatusCode();
gs.addInfoMessage('executed the script');
current.update();
}
}
})(current, previous);
in other system
if(ignore != true && error != true)
{
var stringUtil = GlideStringUtil;
var dataStr = stringUtil.base64Decode(source.u_attachment1);
var sa = new GlideSysAttachment();
sa.write(target,source.u_attachment_name,"",dataStr);
//var attachment = new attachment();
//attachment.write('incident',target.sys_id,source.u_attachment_name,'', stringUtil.base64DecodeAsBytes(source.u_attachment1));
}
thanks in advance for your help