Copy Attachment with out duplicates
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-27-2014 06:45 AM
Hi All,
I am trying to copy attachment from task table to request with out duplicates
can any one help me
thanks in advance
- Labels:
-
Performance Analytics
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-16-2015 10:23 PM
I did this for RITM created from a Call record...
function onAsync(current) {
attachMove(current);
}
function attachMove(theRITM){
//Get the Call record from the parent request
var theNewCall;
var gr = new GlideRecord('new_call');
var theRequest = theRITM.request;
gs.log("attempting to find " + theRequest);
if(gr.get('transferred_to.sys_id', theRequest)){
theNewCall = gr;
gs.log("Our new Call is " + gr.number);
moveAttachments(theNewCall, theRITM.getTableName(), theRITM.sys_id);
}
}
function moveAttachments(call, currTable, currRecSID) {
//move the attachements
GlideSysAttachment.copy('new_call', call.sys_id, currTable, currRecSID);
var attach = new GlideSysAttachment();
attach.deleteAll(call);
}
Note that it has to run async...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-20-2015 08:29 AM
Hi Russell,
Can you provide any further particulars on your BR. I've been looking for something similar to this, but am struggling to get it to function within our environment. I set it up as an async rule on the sc_req_item table and set to run on insert/update.
Did I mix something up or is there anything else you set on the BR?
Thanks,
scott
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-20-2015 03:30 PM
Hi Scott,
Here is the script with a bit more commentary, and it is running on sc_req_item, only on insert, its async, and I've set the order at 100000 to make sure it runs last of all. (this is to ensure that the parent sc_request has the call reference populated.
//Script to move attachements from a new_call record to the resulting sc_req_item
//Looking to the parent request for the relationship to the new_call
function onAsync(current) {
attachMove(current);
}
function attachMove(theRITM){
//Get the Call record from the parent request
var theNewCall;
var gr = new GlideRecord('new_call');
var theRequest = theRITM.request;
//If there is a parent call, move the attachments from the new_call table to sc_req_item
if(gr.get('transferred_to.sys_id', theRequest)){
theNewCall = gr;
moveAttachments(theNewCall, theRITM.getTableName(), theRITM.sys_id);
}
}
function moveAttachments(call, currTable, currRecSID) {
//Move the attachements GlidSysAttachment params (<from table>, <from sys_id>, <to table>, <to sys_id>
GlideSysAttachment.copy('new_call', call.sys_id, currTable, currRecSID);
var attach = new GlideSysAttachment();
//Remove the attachments from the call record (save space)
attach.deleteAll(call);
}
Hope that is a little more clear, let me know if you have any luck.
Cheers
Russell
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-05-2015 01:57 PM
Russell,
Do you have any suggestions about copying the attachments from the child hr_task table to the parent hr_case when an attachment is added to the HR task? I do not want duplicate files copied.
I've looked at is article and the Wiki for "Copy Attachments from Record to Record" but am having no luck.
Thank you,
Shane
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-05-2015 02:23 PM
The solution I chose was to create a related list on the child that shows the parent's attachments as well as all sibling attachments, and then on the parent's record you can use the list to see all the children's attachments.
To make these lists you have to go to System Definition>Relationships and make sure the "queries from" is sys_attachment but in the code you have to reference what table you want it to look for others on... for example here is mine for sc_task and I have a different relationship defined for each type of form this Relationship will be shown on:
(function() {
var csfam = commaSeparatedTaskFamily('sc_task',parent.sys_id);
current.addQuery('table_sys_id', 'IN', csfam);
})()
All the relationships call this same Script Include:
function commaSeparatedTaskFamily(fromtable, recid){
var family = ''; //output a comma-separated string of related sys_ids
//STEP 0 finding nucleus/source/starting point
var gr = new GlideRecord('task');
if (fromtable == 'sysapproval_approver'){
var aprv = new GlideRecord('sysapproval_approver');
aprv.get(recid);
gr.get(aprv.sysapproval); //record being approved is the source record
family += (gr.sys_id + ', ');
}
else
gr.get(recid); //initiating record is the source record
//STEP 1 up & across
if (gr.parent){
family += (gr.parent + ', ');
var pfind = new GlideRecord('task');
pfind.get(gr.parent);
while (pfind.parent){ //parent-grandparent-great grandparent-infinity
family += (pfind.parent + ', ');
pfind.get(pfind.parent);
}
if (fromtable != 'sysapproval_approver' && fromtable != 'pm_project_task'){ //look for siblings, unless an Approval or Project Task
var sfind = new GlideRecord('task');
sfind.query('parent',gr.parent);
while (sfind.next()){
if (sfind.sys_id != gr.sys_id) //don't want it finding itself
family += (sfind.sys_id + ', ');
}
}
}
//STEP 2 down & down
var cfind = new GlideRecord('task');
cfind.query('parent',gr.sys_id);
while (cfind.next()){ //add children
family += (cfind.sys_id + ', ');
var gcfind = new GlideRecord('task');
gcfind.query('parent',cfind.sys_id);
while (gcfind.next()){ //add grandchildren
family += (gcfind.sys_id + ', ');
//rarely used great grandchildren, decreased performance if enabled!
/*var ggcfind = new GlideRecord('task');
ggcfind.query('parent',gcfind.sys_id);
while (gccfind.next())
family += (ggcfind.sys_id + ', ');*/
}
}
return family;
}
And this is how it looks on the RITM form showing the Catalog Task's (child) Attachments:
Also wanted to link all the attachments in the email notifications so the templates just have ${mailscript:attach_links} and this is the mail script I used:
attachLinks();
function attachLinks() {
var rectype = '';
var gr = new GlideRecord('sys_attachment');
//APPROVALS DONT HAVE CLASS NAMES
if (!current.sys_class_name && current.source_table && current.source_table == 'sc_task' || current.source_table == 'sc_req_item')
rectype = 'sysapproval_approver';
else if (current.sys_class_name == 'sc_req_item')
rectype = 'sc_req_item';
//FOR CATALOG ITEMS/TASKS THEMSELVES
if (rectype != ''){
var csfam = commaSeparatedTaskFamily(rectype,current.sys_id);
var famarray = csfam.split(',');
var hgr = gr.addQuery('table_sys_id',famarray[0]); //first record's attachments
for (var i=1; i<famarray.length; i++){
hgr.addOrCondition('table_sys_id',famarray[i]); //then gets the rest
}
}
//FOR ALL OTHER RECORDS THAT WILL ONLY ATTACH APPROVAL'S ATTACHMENTS
else {
gr.addQuery('table_sys_id',current.sys_id);
}
gr.query();
if(gr.hasNext()){
template.print("Attachments: ");
while (gr.next()) {
var attachLink = '<a href="' + gs.getProperty("glide.servlet.uri") + gs.generateURL(gr.getTableName(),gr.sys_id) + '">' + gr.file_name + '</a>';
template.print(attachLink);
if(gr.hasNext())
template.print(", ");
else
template.print("\n");
}
}
}
Even sabell2012 might be impressed with this... not as elegant as some of his code but he can make it work