Skip to main content

ServiceNow UI Action to allow a user to select attachments on an incident and auto-email these to 3rd party

 this solution could be extended for APIs too


UI Action:

 

 

script:

function copyAttachments() { var gdw = new GlideDialogWindow('Email_Client_Attachments'); gdw.setTitle("Please be aware that by clicking 'OK' your attachments will be emailed to 'xxxx'"); gdw.setSize(450, 300); gdw.setPreference('sysparm_sys_id', g_form.getUniqueValue()); gdw.render(); } 



UI Page:

html:

 

<?xml version="1.0" encoding="utf-8" ?> <j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null"> <j:set var="jvar_sysid" value="${sysparm_sys_id}"/> <g:evaluate var="jvar_att" jelly="true" object="true"> var arr = []; var records = []; var att = new GlideRecord("sys_attachment"); att.addEncodedQuery("table_sys_id=" + jelly.jvar_sysid); att.orderBy("file_name"); att.query(); while(att.next()) { arr.push(att.file_name.toString()); records.push(att.sys_id.toString()); } var arr1 = ''; var records1 = ''; for(var i=0;i&lt;arr.length;i++) { if(i == 0) records1 = records[i]; else if(arr[i] != arr[i-1]) { records1 = records1 + ',' + records[i]; } } var att1 = new GlideRecord("sys_attachment"); att1.addEncodedQuery("sys_idIN" + records1); att1.query(); att1; </g:evaluate> <j2:if test="${jvar_att.hasNext()}"> <j:while test="${jvar_att.next()}"> <g:ui_checkbox name='related_attachment:${jvar_att.getValue("sys_id")}' value='${jvar_att.getValue("file_name")}' class='attachment_checkbox'>${jvar_att.getValue("file_name")}</g:ui_checkbox><br/> </j:while> </j2:if> <br/> <g:evaluate var="jvar_sysid" expression ="RP.getParameterValue('sys_id')" /> <button onclick="myFunctionSubmit('${jvar_sysid}')">Ok</button> <button onclick="myFunctionCancel()">Cancel</button> </j:jelly>




client script


 

function myFunctionSubmit(incSysID) { var x = $$('.attachment_checkbox[value=true]'); //array of the checkbox elements if (x.length == 0) { alert("Please select an attachment"); return false; } else 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]; } var ajax = new GlideAjax("getAttachmentLists"); ajax.addParam("sysparm_name", "updateAttachmentList"); ajax.addParam("sysparm_attachmentsysid", list_of_attchs_ids); ajax.addParam("sysparm_incidentsysid", incSysID); ajax.getXML(updateWorknotes); } function updateWorknotes(response) { var answer = response.responseXML.documentElement.getAttribute("answer"); if (answer) { g_form.setValue("work_notes", answer); g_form.save(); } } GlideDialogWindow.get().destroy(); } function myFunctionCancel() { GlideDialogWindow.get().destroy(); }



New fields on sys_attachment table




Notification

 

 

(complete the event name)


 

 

Script include - getAttachmentLists

create event registry entry where highlighted (give it the name of your choice) and reference it in the notification above

var getAttachmentLists = Class.create(); getAttachmentLists.prototype = Object.extendsObject(AbstractAjaxProcessor, { updateAttachmentList: function() { var attachmentNames = ""; var incidentSysID, incidentNumber; var attach_sys_id = this.getParameter("sysparm_attachmentsysid"); var att = new GlideRecord("sys_attachment"); att.addEncodedQuery("sys_idIN" + attach_sys_id); att.query(); while (att.next()) { incidentSysID = att.table_sys_id; attachmentNames += " Attachments " + att.file_name + " have been emailed to HOTH" + "\n"; att.u_send_to_email_client = true; att.update(); } var rec = new GlideRecord("incident"); rec.addEncodedQuery("sys_id=" + incidentSysID); rec.query(); gs.log('JN rec'+rec.number); if (rec.next()) { incidentNumber = rec.correlation_id; } var grSysAtt = new GlideRecord('sys_attachment'); //grSysAtt.addEncodedQuery("sys_idIN" + attach_sys_id); grSysAtt.addEncodedQuery('u_send_to_email_client=true'); grSysAtt.query(); if (grSysAtt.next()) { var attachment = new GlideSysAttachment(); var grEmailAtt = new GlideRecord("u_tp_email_attachment"); grEmailAtt.initialize(); grEmailAtt.u_third_party = 'HOTH'; grEmailAtt.u_source_incident = rec.number; grEmailAtt.u_correlation_id = incidentNumber; grEmailAtt.insert(); } var tAtt = new GlideRecord("sys_attachment"); tAtt.addEncodedQuery("sys_idIN" + attach_sys_id); tAtt.query(); while (tAtt.next()) { var ssID = tAtt.getValue('sys_id'); var filename = tAtt.getValue('file_name'); var conT = tAtt.getValue('content_type'); var tarG = new GlideRecord('u_tp_email_attachment'); var tarObj2 = tarG.get(grEmailAtt.sys_id); var gAtt = new GlideSysAttachment(); var gAttObj = gAtt.getContentStream(ssID); var pgAtt = new GlideSysAttachment().writeContentStream(tarG, filename, conT, gAttObj); } var upAtt = new GlideRecord("sys_attachment"); upAtt.addEncodedQuery("sys_idIN" + attach_sys_id); upAtt.query(); while (upAtt.next()) { incidentSysID = att.table_sys_id; upAtt.u_send_to_email_client = false; upAtt.update(); } gs.eventQueue('HOTH.Email', current, attach_sys_id, incidentNumber); return attachmentNames; }, type: 'getAttachmentLists' });







script include 2 - CheckAttachment


var CheckAttachment = Class.create(); CheckAttachment.prototype = { attachment: function() { var gr = new GlideRecord("sys_attachment"); gr.addQuery("table_name", "incident"); gr.addQuery("table_sys_id", current.sys_id); gr.query(); if (gr.next()) { return true; } else { return false; } } };
 
 








 

Custom Table (extended from DL_Matcher)

 

 

 

















 

(ServiceNow )









 

 


 

 



 






Comments

Popular posts from this blog

ServiceNow timeout settings for virtual agent chat

  After connecting to live agent User is connected to live agent and not responding to live agent chat or distracted. The below properties controls the timeout settings, the OOB settings for reminder is [180 sec] and for cancelling the chat is [360 sec]. The job is default configured to 2 min so I believe no tweaking is required here.  Property -   com.glide.cs.idle_chat_reminder_timeout                  com.glide.cs.idle_chat_cancel_timeout Scheduled job   - Idle Chat Timer Task   https://community.servicenow.com/community?id=community_article&sys_id=1453b03bdbaad0109e691ea668961929   (ServiceNow ) 

ServiceNow check for null or nil or empty (or not)

Haven't tested these all recently within global/local scopes, so feel free to have a play! option 1 use an encoded query embedded in the GlideRecord , e.g.  var grProf = new GlideRecord ( 'x_cls_clear_skye_i_profile' ); grProf . addQuery ( 'status=1^ owner=NULL ' ); grProf . query (); even better use the glideRecord  addNotNullQuery or addNullQuery option 2 JSUtil.nil / notNil (this might be the most powerful. See this link ) example: if ( current . operation () == 'insert' && JSUtil . notNil ( current . parent ) && ! current . work_effort . nil ())  option 3 there might be times when you need to get inside the GlideRecord and perform the check there, for example if the code goes down 2 optional routes depending on null / not null can use gs.nil : var grAppr = new GlideRecord ( 'sysapproval_approver' ); var grUser = new GlideRecord ( 'sys_user' ); if ( grUser . get ( 'sys_id' , current . approver )){...