Skip to main content

Service Portal friendly onsubmit client script to validate attachment is added

[note more simple approach but which only works in portal (may be fixed in later versions):
set 'mandatory attachment' box to true on the catalog item ]

(Courtesy of:

in case the attachment is mandatory dependant on a field value / selection (otherwise you'd just tick the attachment mandatory box)

create a UI script - note, global can be set to false should still work

function getSCAttachmentCount() {
var length;
try {
length = angular.element("#sc_cat_item").scope().attachments.length;
} catch(e) {
length = -1;
return length;

reference the UI script in service portal

This script needs to be included in your Service Portal via the Theme. Go to Service Portal > Portals and select your portal. Then click the reference icon next to the Theme field to go to its record. There is a related list called JS Includes. Create a new one (Source: UI Script) and set the UI Script to GlobalCatalogItemFunctions

Service Portal and classic UI compatible version

Note: isolated script flag must be set to false to prevent an error

function onSubmit() { try { var errMsg="You must attach the Application Discovery Form to submit."; if (typeof spModal != 'undefined') { //--loaded from Service Portal //--For Service Portal var icount = getSCAttachmentCount(); if (icount <= 0) { spModal.alert(errMsg); //return false;//not required if the 'mandatory attachment' checkbox is ticked on the catalog item } } else { //--loaded from classic UI //alert(bPortal); var cat_id =g_form.getParameter('sysparm_item_guid'); //alert('hello2:' + cat_id); //--using a gliderecord in a client script not great, but not much worse than getXMLWait() for catalog client script onSubmit... /*var gr = new GlideRecord("sys_attachment"); gr.addQuery("table_name", "sc_cart_item"); gr.addQuery("table_sys_id", cat_id); gr.query(); if (! { var gm = new GlideModal(); gm.setTitle("Warning"); gm.renderWithContent(errMsg); return false; }*/ var nAttachments = document.querySelectorAll('.attachment_list_items>span').length; //alert("Found " + nAttachments); //--note: Isolate Script must be set to false or an error may be thrown if (nAttachments<1) { var gm = new GlideModal(); gm.setTitle("Warning"); gm.renderWithContent(errMsg); return false; } } } catch (e) { //-- (catch-all) alert('error-pls check [' + e.toString() + ']'); } }

(experiment with setting the 'isolate script' flag to false if there are issues, note this is not displayed on the catalog client script form by default)


Popular posts from this blog

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

Get URL Parameter - server side script (portal or classic UI)

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