Facing Issue, while copy attachment from incident task to incidet.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-23-2024 05:40 AM
Hi every one,
I am have written a script as below and used it in before insert and update business rule, but i am facing issue with it.
1.it is copying when i attach one time and the next time it is not copying.
2..when i delete attachment in incident task , it is not deleting in incident.
the written script is as below, please let me know what to do
var incidentGr = new GlideRecord('incident');
incidentGr.addQuery('sys_id', current.incident);
incidentGr.query();
if (incidentGr.next()) {
// Check if there are attachments to copy
GlideSysAttachment.copy("incident_task", current.sys_id, "incident", incidentGr.sys_id);
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-23-2024 05:53 AM
For Delete - you are not handling that at all .
I assume you have it working only on insert+attached attachment and eventually on update+added attachment and changed value so the BR can trigger.
I had similar issues in the past and used BR on sys_attachment table to fire event where I handled with event script which was doing the needed.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-23-2024 06:01 AM
Recently I adopted this OOB logics for interactions for my custom needs for managing child-parent attachment relationships. Check this script include and you can follow its logic - InteractionRelationshipUtil
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-23-2024 05:54 AM
It looks like you're trying to copy attachments from an incident task to the parent incident and also ensure that when an attachment is deleted from the incident task, it is also deleted from the incident. There are a couple of issues with your current script and approach.
Here's a revised version of your script that should be placed in a Business Rule for the `incident_task` table. This script will copy attachments from the incident task to the parent incident when a new attachment is added. To handle deletions, you will need a separate business rule or a script include that listens for deletion events on attachments.
(function executeRule(current, previous /*null when async*/) {
// Check if the current operation is an insert or update
if (current.operation() !== 'insert' && current.operation() !== 'update') {
return;
}
var incidentGr = new GlideRecord('incident');
incidentGr.get(current.getValue('incident'));
if (incidentGr.isValidRecord()) {
// Check if there are attachments to copy
GlideSysAttachment.copy('incident_task', current.sys_id, 'incident', incidentGr.sys_id);
}
})(current, previous);
For handling deletions, you can create a separate Business Rule that triggers on delete for the `sys_attachment` table. Here's an example of what that might look like:
(function executeRule(current, previous /*null when async*/) {
// Check if the attachment being deleted is from an incident task
if (current.table_name == 'incident_task') {
// Find the corresponding attachment on the incident
var attachmentGr = new GlideRecord('sys_attachment');
attachmentGr.addQuery('table_name', 'incident');
attachmentGr.addQuery('table_sys_id', current.table_sys_id.incident.toString());
attachmentGr.addQuery('file_name', current.file_name);
attachmentGr.query();
while (attachmentGr.next()) {
// Delete the corresponding attachment
attachmentGr.deleteRecord();
}
}
})(current, previous);
Please note that the above deletion script assumes that the file names are unique and uses the file name to find the corresponding attachment on the incident. If file names are not unique, you may need to use a different method to identify the corresponding attachment.
Please mark this response as correct or helpful if it assisted you with your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-01-2024 02:51 AM
the attachment script is working if i attach any thing for the first time and from second time on wards it is not working,
the delete script is not working i have written it in sys_attach ment table in a after delete business rule.