Is there any way to attach document on Catalog Task creation ?

baqar rizvi
Mega Expert

We have a requirement where a excel sheet needs to be attached when a task is assigned to Service Desk.

After browsing articles I tried to upload that excel file to catalog item from where this task will gets generated after approval and was trying to copy that excel file using a business rule which is running after and on INSERT.

Any Help will be much appreciated.

 

find_real_file.png

Thanks

Baqar

2 REPLIES 2

Mark Stanger
Giga Sage

Rather than copy attachments (and risk having them not copy or not match) I would recommend just creating a defined related list that will show all attachments from the request, request item, and tasks all in one place in a related list at the bottom of the Catalog task form.  Then you won't have to worry about copying everything and trying to sync it.  You can also direct your users to look at the bottom of the catalog task form for all attachments using a UI message at the top.  Here's how you can get an attachments related list with everything you need.

https://www.servicenowguru.com/system-definition/relationships/defined-related-lists/

sachin_namjoshi
Kilo Patron
Kilo Patron

IF you want to add attachment in sc_task record, then you will have to insert record in sys_attachment table and then use below business rule script to attach this file to sc_task table.

Please use below syntax in your business rule

 

//fetch the attachment file that needs to be edited   


var attachmentGR = new GlideRecord("sys_attachment");   


attachmentGR.addEncodedQuery('GOTOtable_name=ecc_agent_attachment^content_type=test/csv^file_nameSTARTSWITH1');   


attachmentGR.orderByDesc('sys_created_on');   


attachmentGR.query();   




if(attachmentGR.next()){   


   var table_sys_id = attachmentGR.table_sys_id;   


   // read the attachment...   


   var stringUtil = new GlideStringUtil();   


   var sa = new GlideSysAttachment();   


   var binData = sa.getBytes(attachmentGR);   


   // convert it to Encoded Data..   


   var encData = stringUtil.base64Encode(binData);   


   // decode the encoded data into string..   


   var csv_string = stringUtil.base64Decode(encData) + '';   


   var csv_array = csv_string.split("\r\n");


   csv_array.shift(); //removes the first row, edit file   


   // convert this back into a CSV string...   


   csv_string = csv_array.join("\r\n");


   // attach it back...   


   var base64EncodeString = stringUtil.base64Encode(csv_string);   


   var data = stringUtil.base64DecodeAsBytes(this.base64EncodeString);   


   //attach the attachment. 


   var attachment = new Attachment();   


   var attachment_sys_id = attachment.write("ecc_agent_attachment", table_sys_id, "1111.csv", "test/csv", data);   


}   

 

Regards,

Sachin