Deleting Attachments from a Catalog Item

tiyasa
Giga Expert

Hello All,

I have a requirement to show and force attachment for certain request types. When the request type is change to options which do not enforce

an attachment, how can I remove the already uploaded attachment from the catalog item view?

find_real_file.png

find_real_file.png

I have tried to put a catalog client script on Change of Request Type and perform a delete if an attachment record matches, but both deleteRecord and deleteMultiple does nott work. Any help is appreciated.

1 ACCEPTED SOLUTION

If you want to trigger it from a client script, then yes, you can use a GlideAjax query to trigger a server side script include.   If you delete from a client script, the form won't be automatically refreshed to recognize the attachment has been deleted.



If you want it to happen when the record is saved, you can use a business rule. It's up to you.



Docs: Client Scripts


Docs: GlideForm


Docs: GlideAjax


Client Script Best Practices - ServiceNow Wiki


Business Rules - ServiceNow Wiki


Business Rules Best Practices - ServiceNow Wiki  


     


View solution in original post

3 REPLIES 3

Chuck Tomasi
Tera Patron

The attachment lives in the sys_attachment table. You cannot remove this from the client script. It must be done from the   server side with a GlideRecord query.



Example:



var att = new GlideRecord('sys_attachment');


att.addQuery('table_name', 'sc_req_item');


att.addQuery('table_sys_id', 'SYS_ID_OF_THE_RITM');


att.query();


att.deleteMultiple();


Hi Chuck,



Thank you.
Can you suggest some ideas on how to achieve this? Since not all items have similar requirements, I would really want to delete only those which do not match Request Type + Attachment required combination. Should I use a Script include and pass the current item whenever there is a request type changed and attachment not required?
Also, will this also clear/refresh the current view of the catalog item?



Thanks,


T


If you want to trigger it from a client script, then yes, you can use a GlideAjax query to trigger a server side script include.   If you delete from a client script, the form won't be automatically refreshed to recognize the attachment has been deleted.



If you want it to happen when the record is saved, you can use a business rule. It's up to you.



Docs: Client Scripts


Docs: GlideForm


Docs: GlideAjax


Client Script Best Practices - ServiceNow Wiki


Business Rules - ServiceNow Wiki


Business Rules Best Practices - ServiceNow Wiki