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 (!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

URL link in addInfoMessage

var ga=new GlideAjax('gld_HR_ajax'); ga.addParam('sysparm_name', 'checkEmployeeNumber_hrProfile'); ga.addParam('sysparm_hrprofilenumber', g_form.getValue('number')); ga.addParam('sysparm_employeenumber', newValue); ga.getXMLAnswer(function(answer) { if (answer!='undefined' && answer!=''){ var navURL="<a style='text-decoration:underline;color:blue' href=hr_profile.do?sysparm_query=number=" + answer + ">" + answer + "</a><img width='3' src='images/s.gif'/>"; var sMsg='The employee number entered already exists on another HR Profile ' + navURL; //alert(sMsg); g_form.showErrorBox('employee_number', 'error - please check'); g_form.addInfoMessage(sMsg); } });

GlideRecord setValue

setValue(String name, Object value) Sets the specified field to the specified value. Normally a script would do a direct assignment, for example,  gr.category = value . However, if in a script the element name is a variable, then  gr.setValue(elementName, value)  can be used. When setting a value, ensure the data type of the field matches the data type of the value you enter. This method cannot be used on journal fields. If the value parameter is null, the record is not updated, and an error is not thrown https://developer.servicenow.com/app.do#!/api_doc?v=madrid&id=r_GlideRecord-setValue_String_Object