Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Issue with Duplicate Attachment Copying from HR Task to HR Case

Satyam123
Tera Contributor

 

I have created a business rule that uses the insert and update actions to copy attachments from an HR Task to an HR Case. However, I am still encountering issues with duplicate attachments being copied. Additionally, attachments are only being copied from the HR Task to the HR Case when I comment on the HR Task.

I want to add the attachment so that whenever I attach any document in the HR Task, it automatically sends it to the parent HR Case, only the newly added doc not all the previous one.
Thanks

(function executeRule(current, previous /*null when async*/) {
// Ensure attachments are only copied once
var hrCase = new GlideRecord('sn_hr_core_case');
if (hrCase.get(current.parent.sys_id) && !hrCase.attachments_copied) {

// Copy new attachments from HR Task to HR Case (you may have a specific function for this)
new hr_Task().updateAttachments(current);

// Set the flag to prevent re-copying
hrCase.attachments_copied = true;
hrCase.update();
}
})(current, previous);

5 REPLIES 5

Satyam123
Tera Contributor

Asyn Business rule : Insert
code: 

// (function executeRule(current, previous) {
// // Get the current user's groups as an array
// var userGroups = gs.getUser().getMyGroups();
 
// // If user has no groups, return no records
// if (userGroups.length === 0) {
// current.addQuery('sys_id', 'NONE');
// return;
// }
// // Filter cases where assignment_group is in the user's groups
// current.addQuery('assignment_group', 'IN', userGroups);
// })(current, previous);


(function executeRule(current, previous) {
// Get the current user's groups as an array
var userGroups = gs.getUser().getMyGroups();

// If user has no groups, return no records
if (userGroups.length === 0) {
current.addQuery('sys_id', 'NONE');
return;
}

// If the Universal Record checkbox is true, allow access to all assignment groups and users
if (current.u_universal_record) {
// Ensure all values from the Multiple Assignment Group and Multiple Assigned to fields are included in the query
var multipleAssignmentGroups = current.u_multiple_assignment_group;
var multipleAssignedTo = current.u_multiple_assigned_to;

// Add query to include multiple assignment groups and assigned users
current.addQuery('assignment_group', 'IN', multipleAssignmentGroups);
current.addQuery('assigned_to', 'IN', multipleAssignedTo);
return;
}

// Filter cases where assignment_group is in the user's groups
current.addQuery('assignment_group', 'IN', userGroups);
})(current, previous);