add an attachment via catalog client script (on load). when i open the form it should add an attachment . i have written the below code but its ot working.

saurabh1
Giga Contributor

function onLoad() {

    //Type appropriate comment here, and begin script below

     

      var cat_id = gel('sysparm_item_guid').value.toString();

     

      //Check if attachment is available

      var gr = new GlideRecord('sys_attachment');

      gr.addQuery('table_name','kb_knowledge');

      gr.addQuery('file_name','test.dotx');

      gr.query();

      if(gr.next()){

           

              GlideSysAttachment.copy('kb_knowledge','d826f8222d4cda40ac0a45db236a591d', 'sc_cart_item', cat_id);

      }

     

     

     

}

5 REPLIES 5

Brad Tilton
ServiceNow Employee
ServiceNow Employee

Could you explain the purpose behind this? Are you copying the same document to each record on a table? If it's for reference, why not just add a link to a KB article that has the attachment on it?



The reason your script doesn't work is because GlideSysAttachment is only available server side and a client script runs client side.


saurabh1
Giga Contributor

requirement is : when i open the form it should have a document attached to it so that the user's can download the attachment for their reference and can edit it .


Brad Tilton
ServiceNow Employee
ServiceNow Employee

I think you're a lot better off doing this with a KB with the attachment attached to it, then you can show a link to the kb article at the top of the form with a display business rule using gs.AddInfoMessage(). The advantage here is that you're not copying attachments all over the place and creating a bunch of duplicates.


saurabh1
Giga Contributor

Thanks brad for the idea! i tried to implement the what you suggested using a macro. Its working fine for me.


cheers!