Pre-attached attachments for the ServiceNow email client

General Add comments
by:

Inject here please!
For some customers it could be useful to pre-attach attachments of the record to mails sent from the email client. Or perhaps automatically attach a document like terms of service. The email client has the wonderful ability to do this by preloading the attachments of the email record. Imagine the following scenario: A client needs to be able to automatically send all existing attachments of the record with every email. This includes the notifications sent through via the email client. You can make that happen.

Adding a business rule

Every time the notification icon is pressed (the little envelop in the top form bar ), and the email client is loaded, a record in the sys_email table is created. To attach any documents from the attachment table to any email from the client, ServiceNow will create an attachment record for that particular sys_email record and make a connection between them. Knowing this, you can use this feature by creating a before business rule that adds attachments to the sys_email record on insertion. So create the following business rule on sys_email:

// get the hasmap from the GlideURI
try{
var map = gs.action.getGlideURI().getMap();
var table = map.get(‘sysparm_table’);
var sid = map.get(‘sysparm_sys_id’);

if (!table.nil() && !sid.nil()) {
    // copy the attachments from the “source” incident
    Packages.com.glide.ui.SysAttachment.copy(table, sid, current.getTableName(), current.sys_id);
}
} catch (err){}

With some gratitude towards a contributor on the community forums (http://community.service-now.com/forum/8662), you are able to attach stuff quite easily. The package method being called copies existing attachments to records. The GlideURI is a way of sending parameters and thereby actions through the system. In this case, it contains enough data to copy attachments to email client emails.

When you now add attachments to the record and open the email client, the email will automatically contain the attachments. Sounds easy enough, but…..

Not there yet

You are not completely done yet. Even though the functionality works, you still have a cosmetic issue. Removing attachments from the client via the ‘Attach File’ button will remove attachments from the sys_email record, but it will still show them in the client. You need to adjust the UI page that performs the removal for us.

The reason it occurs is the way the rows are created in the email client itself. They are not consistent with newly attached items. Look for the function deletedAttachments in the ‘attachment’ UI page. This controls the deletion of attachments in records, so not just for the records created by the email client.

Find the part of the script that says:

      e = gel(“attachment_” + id);
       if (e)
          rel(e);
    }

The rel is the opposite of the gel function you might be familiar with. It will remove elements from the page, but requires an element as parameter. In this case, the usual parameter is found by using gel(“attachment_”+ id). But in this case it doesn’t find an element as these attachments act differently.

Adding another method to remove these kind of elements

You can (backup and) change the script to start doCheckAdded (or any other name for that matter) if the element was not found, and pass the id.

      if (e){
          rel(e);
       } else {
         doCheckAdded(id);
       }
    }

If ServiceNow can’t find the element this way, you will have to help it a bit. All you need to know if there is a consistent and unique way of spotting these elements so that you can device something to remove it consistently. And luckily there is…..

Every attachment placed on the screen of the email client is placed in an element with ‘header_attachment_list’ as id. Within that element, attachments are stored in separate <SPAN> tags that have one unique way of identifying them. When clicked on they will link to the attachment, which means you have a unique identifier: their link. To find this link, you need to scan the innerHTML of each <SPAN> to find a string that looks like this:

var str = ‘sys_attachment.do?sys_id=’ + sys_id;

 

Now you know what to look for you add the following function to the UI page:

function doCheckAdded(sys_id){
       var str = ‘sys_attachment.do?sys_id=’ + sys_id;
       var e = gel(‘header_attachment_list’);
       e = e.getElementsByTagName(‘SPAN’);
       for(var x = 0; x < e.length; x++){          var text = e[x].innerHTML.toString();          if(text.indexOf(str) != -1){            e[x].parentNode.removeChild(e[x]);             }          }          return; }

As you can see it scans the elements to find the link, and if it finds one – it removes it.

Go ahead, save your work and try it again. Old attachments are now removed as well.

Do note that depending on what release you are running, the attachment functionality can work differently.

If you have any questions, please don’t hesitate to contact me on wesley.bouwer@2e2.nl

10 Responses to “Pre-attached attachments for the ServiceNow email client”

  1. kalai Says:

    Is this working now ? I am not really sure if this still holds good… I tried this but dint seem to work

  2. Fred Says:

    I do not think this works anymore, the logs throw this error:
    “java.lang.SecurityException: Illegal attempt to access class ‘com.glide.ui.SysAttachment’ via script”

    Not sure how to get around it at the moment, but I’m looking into it.

  3. Fred Says:

    The solution was actually quite easy, replace:
    Packages.com.glide.ui.SysAttachment.copy(table, sid, current.getTableName(), current.sys_id);

    With:
    GlideSysAttachment.copy(table, sid, current.getTableName(), current.sys_id);

    The patckages call is old and that’s why it doesn’t work.

  4. Marc Says:

    the delete part doesnt seem to work any more in fuji.. any workaround ?

  5. Peter Says:

    Hi,

    my demo instance is running on Geneva and the solution works fine.

    I only had to replace the Package call with “GlideSysAttachment” and fix the quotes in the scripts (mean replace ’ with ‘).

    Wesley, thanks for sharing that!

  6. Friv 360 Says:

    It’s really a cool and helpful piece of info. I’m happy that you just shared this
    useful information with us. Please keep us up to date like this.
    Thanks for sharing.

  7. voucher new look Says:

    Nice post. I used to be checking constantly this blog and I am inspired!
    Very helpful info particularly the last section 🙂 I handle such information a lot.

    I used to be looking for this particular info for a long time.
    Thank you and good luck.

  8. Sachin Bhasin Says:

    Thanks this helped a lot and still works on Jakarta

  9. RB Says:

    Hi,
    Don’t seem to get it to work that part when you are in the email client and want to remove attachment, it’s still showing to the right of the field Attachment, but when you click the attachment isn’t there, but still showing.

    Wondering about the “var str = ‘sys_attachment.do?sys_id=’ + sys_id; ” part. UI Page = Attachment. Change the Client Script in there. Can the function be added anywhere?

  10. Herbart Azure Says:

    You need to adjust the UI page that performs the removal for us.

Leave a Reply