- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-16-2016 12:45 PM
I want to make sure that attachments from a request item are visible on tasks.
I know I can easily copy them to the tasks, but unnecessary duplication and synchronizing issues make this less desirable.
I could display an info message with a link to any attachments. (this might be the next best thing)
What I would really like to do is modify the attachment macro to include the RITM attachments along with any sc_task attachments so that whoever is working a task will have visibility to any attachments the requester may have placed on it.
Anyone done this before? (Or have a better idea)
Thanks,
-Stephen
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-17-2016 12:23 AM
Today is your lucky day... "Consolidated Attachments" is actually something I've been working on lately, using a defined Relationship as Anthony suggests.
Here is a defined relationship you can create for the [task] table that will show you attachments for ALL references (including parent, request, requested_item, parent_incident, etc.), as well as from any emails on the current record AND all the references just mentioned. One nice thing is that the related list will also give you a nice label telling you which object the attachment comes from. To do so:
- Go to System Definition>Relationships and create New
- Set Applies to table: Task [task]
- Set Queries from table: Attachment [sys_attachment]
- For the "Query with" script, enter the following:
try{
var itemList = getItemList();
//Get attachments for all of the above
current.addQuery('table_sys_idIN' + itemList);
}
catch(e){
gs.log("Error(" + e.lineNumber + "): " + e, "CONSOLIDATED ATTACHMENTS");
}
function getItemList(){
var arrItems=[],references=[],relTasks=[],relEmails=[];
relTasks=[parent.sys_id];
//Get References
references = getReferences(parent);
//Get Emails
var instanceList = relTasks.concat(references);
relEmails = getRelatedEmails(instanceList.join());
arrItems = instanceList.concat(relEmails);
return arrItems.join(); //Return as a CSV list
}
function getReferences(parent){
//Get referenced record_ids
var refFields = parent.getFields();
var itemListRefs = [];
for (var i=0; i<refFields.size(); i++) {
var field = refFields.get(i);
var ed = field.getED();
if(field.getED().getInternalType() == 'reference' && field.hasValue()){
itemListRefs.push(field.toString());
}
}
return itemListRefs;
}
function getRelatedEmails(instanceList){
var strItems = '';
var grRelEmail = new GlideRecord('sys_email');
grRelEmail.addQuery('instanceIN' + instanceList);
grRelEmail.query();
while(grRelEmail.next()){
strItems += grRelEmail.sys_id + ',';
}
return strItems.split(',');
}
After you've created this defined relationship, just go to Configure->Related Lists to add it to your form (to avoid redundancies, I would remove the standard "Attachments" related list for recundancy's sake).
This is actually a trimmed-down version of what I've been working on... the full version includes attachments from all records that reference the current record as well (i.e., those that appear on its related lists). But I'm still working on the efficiencies of that method, so haven't included it here. The thing that makes it work nicely is that attachments rely on a document ID field... so you only need to use the item list of sys_ids to find all related attachments, and forget about what table they belong to.
Enjoy,
-Brian
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-16-2016 08:28 PM
If you create a relationship, you can include attachments from the parent RITM as a related list.
You will need to query from sys_attachment. You could apply your relationship to either sc_req_item or even task to make it usable for any type of task.
From a related list, parent refers to the main record, so parent.parent is referring to the parent of the sc_task.
current.addQuery('table_name', parent.parent.sys_class_name);
current.addQuery('table_sys_id', parent.parent);
Alternatively, you could apply a similar theory to a UI Macro and create a formatter that can be included on the sc_task form layout

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-17-2016 12:23 AM
Today is your lucky day... "Consolidated Attachments" is actually something I've been working on lately, using a defined Relationship as Anthony suggests.
Here is a defined relationship you can create for the [task] table that will show you attachments for ALL references (including parent, request, requested_item, parent_incident, etc.), as well as from any emails on the current record AND all the references just mentioned. One nice thing is that the related list will also give you a nice label telling you which object the attachment comes from. To do so:
- Go to System Definition>Relationships and create New
- Set Applies to table: Task [task]
- Set Queries from table: Attachment [sys_attachment]
- For the "Query with" script, enter the following:
try{
var itemList = getItemList();
//Get attachments for all of the above
current.addQuery('table_sys_idIN' + itemList);
}
catch(e){
gs.log("Error(" + e.lineNumber + "): " + e, "CONSOLIDATED ATTACHMENTS");
}
function getItemList(){
var arrItems=[],references=[],relTasks=[],relEmails=[];
relTasks=[parent.sys_id];
//Get References
references = getReferences(parent);
//Get Emails
var instanceList = relTasks.concat(references);
relEmails = getRelatedEmails(instanceList.join());
arrItems = instanceList.concat(relEmails);
return arrItems.join(); //Return as a CSV list
}
function getReferences(parent){
//Get referenced record_ids
var refFields = parent.getFields();
var itemListRefs = [];
for (var i=0; i<refFields.size(); i++) {
var field = refFields.get(i);
var ed = field.getED();
if(field.getED().getInternalType() == 'reference' && field.hasValue()){
itemListRefs.push(field.toString());
}
}
return itemListRefs;
}
function getRelatedEmails(instanceList){
var strItems = '';
var grRelEmail = new GlideRecord('sys_email');
grRelEmail.addQuery('instanceIN' + instanceList);
grRelEmail.query();
while(grRelEmail.next()){
strItems += grRelEmail.sys_id + ',';
}
return strItems.split(',');
}
After you've created this defined relationship, just go to Configure->Related Lists to add it to your form (to avoid redundancies, I would remove the standard "Attachments" related list for recundancy's sake).
This is actually a trimmed-down version of what I've been working on... the full version includes attachments from all records that reference the current record as well (i.e., those that appear on its related lists). But I'm still working on the efficiencies of that method, so haven't included it here. The thing that makes it work nicely is that attachments rely on a document ID field... so you only need to use the item list of sys_ids to find all related attachments, and forget about what table they belong to.
Enjoy,
-Brian
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-21-2016 10:58 AM
Not quite what I was hoping for when I asked the question, but the end result was liked better than my original suggestion.
Thanks Brian, well done!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-21-2016 02:17 PM
I've been thinking on this one for a while...
enjoy,
-Brian