logo

NJP

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

Import · Feb 01, 2022 · article

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" ?> 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<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 ${jvar_att.getValue("file_name")}/g:ui_checkbox
/j:while /j2:if
Ok Cancel

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();

(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' });

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 )

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 )){

Classic UI : var sURL_editparam = gs . action . getGlideURI (). getMap (). get ( ' sysparm_aparameter ' ); if ( sURL_editparam == 'true' ) { gs . addInfoMessage ( 'parameter passed ); } Portal : var sURL_editparam = $sp . getParameter ( " sysparm_aparameter " ); if ( sURL_editparam == 'true' ) { gs . addInfoMessage ( 'parameter passed ); }

Call a script include to apply a reference qualifier on a catalog item variable: - variable reference qualifier dependent on another variable selection, in this case a variable referencing sys_user (requested_for) On the catalog item form. variable name to apply ref qual filter : retail_equipment variable reference qualifier (on cmdb table ): javascript : new refqual_functions (). lostStolen_getAssignedCIs (); client-callable script include ( refqual_functions) function : lostStolen_getAssignedCIs : function (){ //--called from variable set client script, for lost/stolen request (service catalog) gs . log ( current . variables . requested_for , 'retail_lostStolen_getAssignedCIs' ); return ( 'install_statusNOT IN8,7owned_by=' + current . variables . requested_for ); //owned_by=1269b79937f1060041c5616043990e41install_statusNOT IN8,7 },

View original source

http://www.cloudminus89.com/2022/02/ui-action-to-allow-user-to-select.html