logo

NJP

SERVICE PORTAL: Make Attachments Mandatory on Service Portal

Import · Apr 15, 2020 · article

Hi All,

How to make "Attachments" mandatory on a catalog item in the Service Portal (SP) based on a specific variable value.

e.g. If Hard Drive is selected "250 GB" then makes attachment mandatory.

We know the challenges with making Attachments mandatory on a catalog item in the Service Portal (SP).

Unfortunately, you can't just use the Client Script you used in the old CMS site. The old DOM manipulation scripts used in CMS often won't work in the SP as it is a different architecture than CMS.

Below are the steps to be followed :

1. ADD A UI SCRIPT.

  1. In the Left Navigator Bar, go to System UI > UI Scripts
  2. Click New

UI SCRIPT: GlobalCatalogItemFunctions

function getSCAttachmentCount() {

   var length;

   try {

       length = angular.element("#sc_cat_item").scope().attachments.length;

       } catch(e) {

   length = -1;

   }

   return length;

}

2. CREATE A CATALOG CLIENT SCRIPT

For your catalog item, if you want to make attachment mandatory then use this client script.

  1. Catalog Item - "Your Required Cat_Item"
  2. Type - onSubmit
function onSubmit() {
    //Works in non-portal ui header_attachment_list_label
    var HDD = g_form.getValue('hard_drive'); // get required variable value
    if(HDD == 250){
        try { //Works in non-portal ui header_attachment_list_label
            var attachments = document.getElementById('add_attachment_button');
            if (attachments.style.visibility == 'hidden' || attachments.style.display == 'none' ) {
                alert('Please attach atleast one attachment to proceed');
                return false;
            }
        }
        catch(e) { //For Service Portal
            var count = getSCAttachmentCount();
            if(count <= 0) {
                alert('Please attach atleast one attachment to proceed');
                return false;
            }
        }
    }
}

3.ADD A JS THEME

  1. In the Left Navigator Bar, go to Service Portal > Portals
  2. Click the Portal you want to adjust. It maybe is the one with URL suffix of "sp".
  3. Click the "info" button for the Theme. The standard theme is "Stock"
  4. Add your JS Include there

Create New JS Theme

Display Name: GlobalCatalogItemFunctions

UI Script: GlobalCatalogItemFunctions

image

-------

And that is it. Hope you like it. If you have any questions or remarks, do let me know in the comments below!

If this article helped you in any way, please do bookmark it and mark it as helpful.

Kind regards,

Vaibhav Chaudhari

View original source

https://www.servicenow.com/community/developer-articles/service-portal-make-attachments-mandatory-on-service-portal/ta-p/2324655