Select attachments from the underlying task record and send them via the email client

This tip comes handy especially if you are working with attachments all the time and communicate with other people outside the company on a regular basis.

Uploading attachments manually in ServiceNow is very easy, but sometimes you might want to select some of the existing attachments in a task related record and send them via the email client to an external email address or to a specific system user. But it is quite time and hard drive space consuming to always have to first download the attachments and then attach them in a second step to the email client window. So, you want a way to let the system attach those files automatically for you. A custom behavior of the email client is needed in order to implement this feature.

To achieve this, follow the 4 steps below:

1. Add a new field of type true/false to the Attachment table, e.g. “Send via email client”, and set False as a default value. This is needed in order to mark and then select in the background which files to attach to the email client record.

2.Because modifying the OOTB email client icon is not advisable, and also probably not possible, you need to create a new UI action on the target table, which opens a custom UI page described in the next step 3.

Script field of the UI action:

  function displayDialog(){

     //the UI page described in step 3
     var gdw = new GlideDialogWindow(‘show_attachments_of_ticket’);     
     gdw.setTitle(‘Email Client);
     gdw.setPreference(‘sysparm_sys_id’, g_form.getUniqueValue()); // Our task record ID
     gdw.render();

   }

3. Create a new UI page to invoke when clicking on the UI action explained in the previous step, which will display the list of the task record’s attachments, and before each attachment shows a checkbox, so that the user can select which attachments to copy.
The key point is to have in the HTML field of the UI page, in the loop that displays the attachments, the following element that will hold the name and the sys_id of the attachments.

<g:ui_checkbox name=”related_attachment:${gr.sys_id}” value=”${gr.file_name} ” class=”attachment_checkbox”/>

This is needed to identify those that the user selected. Upon pressing the “Ok” button, the “Client script” field of the UI page will run. Parts of this code are given below:

//Get the list of sys_ids of the selected attachments

var x = $$(“.attachment_checkbox[value=true]”); //array of the checkbox elements
if (x.length > 0) {
var list_of_attchs_ids = “”;
var attch_name  = “”;
for (var j=0; j< x.length; j++) {
//get the sys_id of the attachment from the checkbox element name
  attch_name  = x[j].name.split(“:”);
    if (list_of_attchs_ids==””)
        list_of_attchs_ids = attch_name[1];
else
       list_of_attchs_ids = list_of_attchs_ids + “,”+ attch_name[1];                    
   }

After getting the list of sys_ids of the selected attachments, make a synchronous Ajax call with the purpose to mark the  “Send via email client” field to True for all the selected attachments. After the Ajax call returns, open the email client window by invoking the following:

var newURL = “email_client.do?sysparm_table=incident&sysparm_sys_id=”+ ticket_id +
“&sysparm_target=incident&sys_target=incident&sys_uniqueValue=” + ticket_id +
“&sys_row=0&sysparm_encoded_record=”; //build the URL of the email client window

popupOpenEmailClient(newURL); //opens the email client

GlideDialogWindow.get().destroy(); //to automatically close the window

4. A business rule of before insert type on the sys_email table with the following condition: Type is send-ignored and Target table is empty. This BR will copy the selected attachments to the email record. The conditions of the BR are actually the attributes of the email record when it is first inserted/open via the email client button.

The code of the business rule:

     var ticket_number = current.subject.toString().substr(0,14);
      // The first 14 characters of the subject are the ticket number.
      // We need to subtract this number to get the sys_id of the task record.

     var gr = new GlideRecord(‘incident’);
     gr.addQuery(‘u_ticket_number’,ticket_number);
     gr.query();
     gr.next();

     var arrayAttachment = GlideSysAttachment.copy(‘incident’, gr.sys_id ,’sys_email’, current.sys_id);

     for (var i = 0; i< arrayAttachment.size(); i++) {
             var attachmentSidsPiece = String(arrayAttachment.get(i)).split(‘,’);
             var sourceAttachmentSysid = attachmentSidsPiece[0];
             var targetAttachmentSysid = attachmentSidsPiece[1];
             var sourceRec = new GlideRecord(“sys_attachment”);
             var targetRec = new GlideRecord(“sys_attachment”);

     if (sourceRec.get(“sys_id”,sourceAttachmentSysid) &&       
                               targetRec.get(“sys_id”,targetAttachmentSysid)) {

       if (sourceRec.u_send_via_email_client==false) {
            //delete all other attachments that are not marked to send via the email client        
            targetRec.deleteRecord();

     } else { //reset the flag
           sourceRec.u_send_via_email_client= false;
           //as soon as the email record is created, reset the flag to false again
           sourceRec.update();
    }
 }
}

I hope this will make your daily work much easier and save you some time.

mm
Rilinda joined the company in 2013 and works as a consultant in our office in Frankfurt. She is a certified System Administrator, Implementation Specialist and Application Developer and has implemented ServiceNow for several big and medium international customers. With a strong technical background and an eye on details, she focuses always on the best solution for our customers to help them achieve Service Excellence for all areas of their business.
Recent Posts

Leave a Comment

Start typing and press Enter to search