logo

NJP

Possible Ways for Making an Attachment Mandatory : Service Portal/Native UI

Import · Aug 24, 2019 · article

Problem Statement: Need to check if attachment is added or not when user submit a request through service catalog.

I know, there are lots of post regarding making attachments mandatory on Native UI and Service Portal. But very few of them are on working state.

I am tried to summarized all possible ways for making attachment mandatory on different UI types, ServiceNow Versions.

So, Let’s get started…….

Option 1: ServiceNow Madrid Release Feature: Mandatory Attachment with No scripting

If you are using ServiceNow Madrid or later release, then with the help of checkbox available on catalog item you can make attachment mandatory. Also, you can hide a paperclip i.e. attachment icon. Follow the steps as below:

  1. Search Maintain items in Filter navigator and click on Maintain items
  2. Select a any catalog item for that you want to make attachment/ hide attachment
  3. In portal Setting Tab / Section, you will find out two checkbox fields named as : Mandatory Attachment
  4. You just need to Mark them true/checked as below.
  5. If you want to make it mandatory, then click on Attachment Mandatory: TRUE/Checked.

image

You can go to Portal/Native UI and check if attachment is made mandatory/hidden.

Option 2: Using UI Script, JS Theme, and Client script
Here is other ways that you may have a look as well.

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

UI Script: GlobalCatalogItemFunctions

Global: true

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

return length*;*
}

2. ADD A JS THEME:

  1. In the Left Navigator Bar, go to Service Portal > Portals
  2. Click the Portal you want to adjust. It may be the one with URL suffix of "sp".
  3. Click the "info" button for the Theme Field. The standard theme is "Stock"
  4. Add your JS Include there
  5. Create New JS Theme
    Display Name: GlobalCatalogItemFunctions
    UI Script: GlobalCatalogItemFunctions

3. CREATE A CATALOG CLIENT SCRIPT: For your catalog item you want to require attachments, use this client script.

function onSubmit() {

//Works in non-portal ui

try {

var attachments = document.getElementById('header_attachment_list_label');

if (attachments.style.visibility == 'hidden' || attachments.style.display == 'none' ) {

alert('You must attach the completed form before submitting this request.');

return false;

}

}

//For Service Portal

catch(e) {

var count = getSCAttachmentCount();

if(count <= 0) {

alert('You must attach the completed form before submitting this request.');

return false;

}

}

}

Option 3: Using DOM Manipulation in Client script:

Well with the introduction of London came a new field called 'isolate script'. New client-scripts are run in strict mode, with direct DOM access disabled. Access to jQuery, prototype and the window object are likewise disabled. To disable this on a per-script basis, configure this form and add the "Isolate script" field. To disable this feature for all new globally-scoped client-side scripts set the system property "glide.script.block.client.globals" to false.

Before running this script, Make an Isolate script: False on client script / catalog client script.

1. This script will run UI Types: Native and Service Portal

function onSubmit() {

if(this.document.getElementsByClassName('get-attachment').length == 0) {

alert(''You must add an attachment before submitting this request.');

return false;

}

}

2.This script will run UI Types: Native UI

function onSubmit() {

var attachments = document.getElementById('header_attachment_list_label');

if (attachments.style.visibility == 'hidden' || attachments.style.display == 'none' ) {

alert('You must add an attachment before submitting this request.');

return false;

}

}

Option 4: OnSubmit Client Script with the help of GlideRecord:

GlideRecord and g_form.getReference() are also available for retrieving server information. However, these methods are no longer recommended due to their performance impact in client script. Both methods retrieve all fields in the requested GlideRecord when most cases only require one field.

This script will run on both Native and portal UI as well.

function onSubmit() {

var cat_id = g_form.getValue('sysparm_item_guid');

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

alert("You must add an attachment before submitting this request.");

return false;

}

}

Option 5: OnSubmit Client Script with the help of GlideAjax:

If you are not in Madrid release, then above option is not available for you. In that case, with the help of client

script, you can achieve the above functionality for making attachment mandatory.

Before running this script, Make an ISOLATED SCRIPT: FALSE on client script / catalog client script.

Below script will run on Native UI (Non portal UI).

Client script Code:

function onSubmit() {

// var formID = gel('sysparm_item_guid').value;

// var formID= document.getElementById('sysparm_item_guid').value;

var formID = g_form.getValue('sysparm_item_guid');

// alert("Form ID: "+formID);

var ab = new GlideAjax('global.hasAttachment');

ab.addParam('sysparm_name', 'attachment');

ab.addParam('sysparm_form_id', formID);

ab.getXMLWait();

var isTrue = ab.getAnswer();

if(isTrue == 'yes'){

alert("Kudos to you!! You have added attachment");

return true;

} else if(isTrue == 'no'){

alert("You must attach document before submitting the form.");

return false;

}

Script include Code:

Name: hasAttachment

Client callable: TRUE

Script:

var hasAttachment = Class.create();

hasAttachment.prototype = Object.extendsObject(AbstractAjaxProcessor, {

attachment: function(){

var formID = this.getParameter('sysparm_form_id');

//gs.log("AG formID"+ formID);

var gr = new GlideRecord("sys_attachment");

gr.addQuery('table_name', "sc_cart_item");

gr.addQuery('table_sys_id', dataCheck);

gr.query();

if (gr.next()) {

//gs.log("Kudos to you!! You have added attachment ");

return 'yes';

}else{

//gs.log("You must attach document before submitting the form.");

return 'no';

}

},

type: 'hasAttachment'

});

View original source

https://www.servicenow.com/community/developer-articles/possible-ways-for-making-an-attachment-mandatory-service-portal/ta-p/2325089