Attachment UI action

brozi
ServiceNow Employee
ServiceNow Employee

I had a request to create UI action for attachments instead of using the lil paper clip icon.

I just grabbed the onclick script (using firebug, firebug is cool!!)


saveAttachment('incident', '');


and created a new client UI action with the same onclick code on the incident table.

Nothing special, but thought some of you could use that to truly make something cool...

16 REPLIES 16

Karthik,



Are you looking to save one attachment to multiple records?   Please advise.


Community Alums
Not applicable

Yes, exactly, save attachment to multiple selected records..


in that case, you would need to change the onClick to a custom function:



onclick: doThis()



then create that function in your script area:



function doThis () {


        //first turn the getChecked into an array


        var csv = g_list.getChecked();


        var array = csv.split(",");



        //then iterate through the array adding the attachment to each record


        for(x=0; x<array.length; x++) {


                  saveAttachment('incident', array[x]);


        }


}


Community Alums
Not applicable

Thanks much for the response!!


I have tried this and it works for one record only, even if i select multiple records.


for ex, 3 records i selected, and only one record gets the attachment and not all.


then it is probably attaching it to the first record in the list.   In that case you can adjust the function to the following:



onclick: doThis()



then create that function in your script area:



function doThis () {


        //first turn the getChecked into an array


        var csv = g_list.getChecked();


        var array = csv.split(",");



        //attach to first elemment in the array


        saveAttachment('incident', array[0]);



        //find the new attachment


        var attGR = new GlideRecord('sys_attachment');


        attGR.addQuery('table_sys_id', array[0]);


        attGR.orderByDesc('sys_created_on');


        attGR.query();



        if (attGR.next()) {


                  var originalID = attGR.sys_id;



                  //then iterate through the remainder of the array copying the attachment to each record


                  for(x=1; x<array.length; x++) {


                            attGR.table_sys_id = array[x];


                            var newID = attGR.insert();



                            var attDocGR = new GlideRecord('sys_attachment_doc');


                            attDocGR.addQuery('sys_attachment', originalID);


                            attDocGR.query();



                            while (attDocGR.next()) {


                                      attDocGR.sys_attachment = newID;


                                      attDocGR.insert();


                            }


                  }


        }


}




Depending on how many records are selected and the size of the file, you might be better off doing this in a script include and passing the array over after the first attachment.   How this helps