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 (!gr.next()) { 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)

Comments

Popular posts from this blog

GlideRecord setValue

variable advanced reference qualifier example

Running transform maps asynchronously