How to Attach and attachment in Target instance on Incident record when attached on Source incident
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi All,
We have 2 instances Source and Target and we have Integrated both for incident creation but for attachments we have created REST Message in source and trying to send the attachment to target.
The attachment is getting attached to target record but the data is missing in the attachment.
For example : REST Message
{
"u_file_name": "Demo doc 2.txt",
"u_content_type": "text/plain",
"u_table_name": "incident",
"u_base64_payload": "VVJMOiBodHRwczovL3ZvcndlcmtkZXYuc2VydmljZS1ub3cuY29tL2FwaS88eW91cl9hcGlfaWQ+L2FkZF9hdHRhY2htZW50DQoNCmpzb24NCnsNCiAgImluY2lkZW50TnVtYmVyIjogIklOQzAwMTA4ODYiLA0KICAiZmlsZU5hbWUiOiAibXlmaWxlLnR4dCIsDQogICJjb250ZW50VHlwZSI6ICJ0ZXh0L3BsYWluIiwNCiAgImZpbGVEYXRhIjogImJYa2djMkZ0Y0d4bElHWnBiR1U9IiANCn0=",
"u_record_sys_id": "6f2ce376833f2e9029977065eeaad3da"
}
In target we have created a staging table and from staging table to incident table. payload data is coming to staging table but not passing to target incident record. Just attachment is inserting to record but not the data.
Can anyone help on this?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hello @Vinod Kumar Tan
During the integration, attachments can be synchronized using a Business Rule on the sys_attachment table. Since attachments are stored separately on the sys_attachment table, simply moving payload data into the staging table will not automatically sync the attachment contents.
The solution is to create an After Insert Business Rule on the [sys_attachment] table. This ensures that whenever a new attachment is added, it is processed after being inserted into the system.
This is the working BR for attachments, Kindly check:
(function executeRule(current, previous /*null when async*/ ) {
var targetInstanceURL = " ";
var targetUserID = " "; // Instance userid
var targetUserPassword = " "; // Instance password
var p = new GlideRecord('incident');
if(p.get(current.table_sys_id))
{
var targetsysid=p.correlation_display;
var amsg= new sn_ws.RESTMessageV2();
amsg.setHttpMethod('post');
amsg.setBasicAuth(targetUserID, targetUserPassword);
amsg.setEndpoint(targetInstanceURL+'api/now/attachment/file');
amsg.setQueryParameter('table_sys_id', targetsysid);
amsg.setQueryParameter('table_name',current.table_name);
amsg.setQueryParameter('file_name', current.file_name);
amsg.setRequestHeader('Content-Type', current.content_type);
amsg.setRequestBodyFromAttachment(current.sys_id);
var response = amsg.execute();
var responseBod = response.getBody();
var txt="Image sent";
}
else
{
txt="Image not sent";
}
gs.addInfoMessage(txt);
})(current, previous);
Please hit like and mark my response as correct if that helps
Thanks & Regards
J Rohit
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @Vinod Kumar Tan,
You might want to check out this community discussion it covers a very similar issue where attachments were created on the target but without file data. The problem was caused by sending only metadata rather than the actual content.
Here’s the reference you will find this related discussion very helpful:
https://www.servicenow.com/community/developer-forum/attachment-api-doesn-t-move-all-attachments/m-p...
Thanks & Regards,
Muhammad Iftikhar
If my response helped, please mark it as the accepted solution so others can benefit as well.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
it means the file data is not passed correctly and getting corrupted.
how are you loading data from staging to target table in target instance?
you must be using Transform map, transform script etc
share that and what debugging did you do for that
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader