How to get the attachments in sync between two records?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-11-2016 09:16 AM
We have come to a situation where we would like to have the attachments in sync between two records. When an attachment is added, updated or removed, it should reflect on the record linked to the current record. I implemented the following script, but I feel like it can be improved. Also, sometimes, for some strange reason, attachments get attached multiple times... Also using session variables does not sit well with me.
This is from a Business Rule that runs against the Attachment Table, after insert, update and delete. It only runs when the current.table_name is equal to Incident or u_back_office.
(function executeRule(current, previous /*null when async*/) {
//Get the GlideRecord for the (Back-Office) Incident
var gr = new GlideRecord(current.table_name);
var gotten = gr.get(current.table_sys_id);
if (!gotten) {
throw "Could not get GlideRecord for attachment " + current.getDisplayValue() +
" on table " + current.table_name + " with Sys ID " + current.table_sys_id;
}
//Are two records linked?
var field = (current.table_name == 'incident') ? 'u_back_office' : 'u_incident';
if (gr[field].nil()) {
return;
}
//Get needed variables
var gsa = new GlideSysAttachment();
var otherGR = gr[field].getRefRecord();
var synchronizing = gs.getSession().getClientData('synchronizing') == 'true';
//Count all the attachments for both GlideRecords
var countGR = getNumberOfAttachments(gr);
var countOtherGR = getNumberOfAttachments(otherGR);
//If we are not synchronizing yet
if (!synchronizing) {
//Start synchronizing
gs.getSession().putClientData('synchronizing', 'true');
if (countOtherGR > 0) {
//No matter the operation, just delete the other records
gsa.deleteAll(otherGR);
} else if (countGR > 0) {
//Copy all attachments
gsa.copy(gr.getTableName(), gr.getValue('sys_id'),
otherGR.getTableName(), otherGR.getValue('sys_id'));
} else {
//Shouldn't really happen, but for sake of completeness
//The synchronizing is done
gs.getSession().putClientData('synchronizing', 'false');
}
//If we were synchronizing already
} else {
//When deleting the last attachment, while there are others
if (current.operation() == 'delete' && countGR == 0 && countOtherGR > 0) {
//Copy all attachments
gsa.copy(otherGR.getTableName(), otherGR.getValue('sys_id'),
gr.getTableName(), gr.getValue('sys_id'));
//When deleting the last attachment, while there are no others
} else if (current.operation() == 'delete' && countGR == 0 && countOtherGR == 0) {
//The synchronizing is done
gs.getSession().putClientData('synchronizing', 'false');
//When inserting the last attachment
} else if (current.operation() == 'insert' && countGR == countOtherGR) {
//The synchronizing is done
gs.getSession().putClientData('synchronizing', 'false');
//During synchronizing
} else {
//Nothing should really be done here
}
}
})(current, previous);
function getNumberOfAttachments(gr) {
var aggregate = new GlideAggregate('sys_attachment');
aggregate.addAggregate('COUNT');
aggregate.addQuery('table_name', gr.getTableName());
aggregate.addQuery('table_sys_id', gr.getValue('sys_id'));
aggregate.query();
return (aggregate.next()) ? aggregate.getAggregate('COUNT') : 0;
}
Writing recursive Business Rules is just horrible...
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-11-2016 09:27 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-11-2016 12:36 PM
Could you explain how this is useful?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-23-2020 04:12 AM
HI Peter,
I am using this business rule for my sync between case and incident table, But it is giving me strange results some times.
Do you have any updated code available for this?
Thanks
Sudhir