How to automatically add RITM attachments into SCTASK?

Luis Knez
Kilo Contributor

We are being asked to include the RITM attachments into the Catalog Tasks automatically. Can you share the steps or script?

6 REPLIES 6

Sudhanshu Talw1
Tera Guru

try this:

Create After business rule on "sys_attachment" table, operation would be "INSERT", use the business rule condition Table Name | IS | sc_req_item table.

 

copyAttachments(current);

function copyAttachments(attachment) {
	var tasks = new GlideRecord("sc_task");
	tasks.addActiveQuery();
	tasks.addQuery('request_item', current.table_sys_id);
	tasks.query();
	while(tasks.next()) {
		gs.log('Copying attachment for task ' + tasks.number, 'ServiceNow Community');
		var gsa = new GlideSysAttachment();
		gsa.copy(current.table_name, current.table_sys_id, tasks.getTableName(), tasks.sys_id);
	}
}

//The above one will work otheriwse this one also works 

(function executeRule(current, previous /*null when async*/) {

            var gr = new GlideRecord('sys_attachment');

            gr.addQuery('table_sys_id',current.sys_id);

            gr.query();

            if (!gr.hasNext())

            {

                        GlideSysAttachment.copy('sc_req_item', current.request_item, 'sc_task', current.sys_id); 

            }

  })(current, previous);​

 

https://community.servicenow.com/community?id=community_question&sys_id=78bb2c64db87d74423f4a345ca96...

Brad Bowman
Kilo Patron
Kilo Patron

ServiceNow recommends not copying attachments for a number of reasons - database bloat, out of sync attachment updates, and timing/coverage (you would need to run business rules on the sys_attachment table AND sc_task to be sure attachments are shown as each new task and attachment gets created). 

Instead you can simply show the RITM attachments on the Catalog Task as a related list.

  1. Navigate to System Definition -> Relationships.
  2. Click the New button to create what will become a new related list.
  3. Type a Name - whatever you want to appear on the Related List/tab.  Applies to table sc_task.  Queries from table sys_attachment.  The Query with script will look like this
    (function refineQuery(current, parent) {
     current.addQuery('table_sys_id' , parent.request_item);
    })(current, parent);
  4. When viewing a Catalog Task record, right-click the header and choose Configure > Related Lists.
  5. Select the Name you created in step 3 so that it appears in the Selected box.find_real_file.png find_real_file.png

Were you able to test this solution for your requirements?

Thanks @Brad Bowman worked like a charm.  Sincerely, your former co-worker 😛 I hope all is well!

Also added this as related list in Workspace view.

Chris