
- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on ‎04-15-2020 12:04 AM
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.
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.
1. ADD A UI SCRIPT.
- In the Left Navigator Bar, go to System UI > UI Scripts
- 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.
- Catalog Item - "Your Required Cat_Item"
- 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
- In the Left Navigator Bar, go to Service Portal > Portals
- Click the Portal you want to adjust. It maybe is the one with URL suffix of "sp".
- Click the "info" button for the Theme. The standard theme is "Stock"
- Add your JS Include there
Create New JS Theme
Display Name: GlobalCatalogItemFunctions
UI Script: GlobalCatalogItemFunctions
-------
Kind regards,
Vaibhav Chaudhari
- 11,338 Views

- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hi there,
Nice 1on1 copy of the last sentences I use in articles 🙂
Serious question though. How is this different from what out-of-the-box, no code is already available? What makes coding this better or more useful? Because this is about Service Portal correct? So why code, instead of using the out-of-the-box no code options?
Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP
---

- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Yeah, I saw this and thought - this is OOTB now, why customize?

- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hi Paul Morris,
Yeah, you are right, with OOTB "Mandatory Attachment" field we can do that easily.
but here I was trying to do that based on the value of any catalog variables
Unfortunately, I failed to include this before. I have updated the article now with appropriate details. Please have a look again and thanks to correct me.

- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hi Mark,
This article is made attachment mandatory based on the value of any catalog variables.
I have updated the article now with appropriate details. Please have a look again.
Thanks.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Have you seen this? https://serviceportal.io/downloads/require-attachments/
This widget uses the OOB "mandatory attachments" fields on the catalog item, but also allows the attachments to be added inline as part of the form instead of using the paperclip.
See video:

- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Thank you very much Nathan.
This is really amazing.

- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
I'm currently looking for a way to make the attachments mandatory, rather than just alerting if the attachment count is zero.
I'll post if I work it out.
Thanks
P.S. I've worked out how to make it mandatory via the console
// Make SP check for attachment on submit
angular.element("#sc_cat_item").scope().c.data.sc_cat_item.mandatory_attachment = true;
// Show the decoration on the NewRocket widget
angular.element('.v7a8213c0dba24f00431b9c27db9619a2').scope().mandatory_attachment = true;

- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Feature request:
Add a hidden variable called 'toggle mandatory' so others can make it dynamically mandatory from Client Scripts in an OOTB way.
That would be cool.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hi,
I tried this today(2021) and it doesn't work.
an error occurs in the portal saying that getSCAttachmentCount () has not been defined.

- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
I've seen this issue previously. It occurred because I had not added the UI Script to the Service Portal Theme, as per what is documented in the blog.
If you still are not having any luck, I have come up with an alternative approach, in which the attachment widget is made mandatory by code, rather than doing a count at the end. It makes for a better UX.
I outline that approach and others in my blog Making Attachments Mandatory in SP: The Ultimate Guide

- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hi,
This was really helpful, but I have one more thing to check is the file extension getting attached. Could you please help me with that of how to check extension of attachment.

- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
This is available via the Attachment variable in Paris.
I would suggest waiting for this feature rather than creating custom code.

- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Can you help me with the attachment variable which is present in Paris version. I have my instance on same version and want to check extension of file being attached.

- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hi there,
Detailed examples are metioned on the Docs:
https://docs.servicenow.com/bundle/paris-servicenow-platform/page/product/service-catalog-management...
https://docs.servicenow.com/bundle/paris-servicenow-platform/page/product/service-catalog-management...
For example:
If my answer helped you in any way, please then mark it as helpful.
Kind regards,
Mark
2020, 2021 ServiceNow Community MVP
2020, 2021 ServiceNow Developer MVP
---

- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
can I use it in UI Script as mentioned here for getting the attachment count ?
function getSCAttachmentCount() {
var length;
try {
length = angular.element("#sc_cat_item").scope().attachments.length;
} catch (e) {
length = -1;
}
return length;
}
Something similar to check extension of file ?

- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
I created a blog on this - Making Attachments Mandatory in SP: The Ultimate Guide (Including Paris)
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
HI everyone,
I have a requirement in which we need to check first check the mandatory attachment in serviceportal, which could be done OOB now. Along with that we need to check whether the attachment is excel or not , and if it is excel we need to count the filled rows in it. Any help will be appreciated. I have the code for checking whether the attachment is an excel or not, but its not working in portal as we are using Dom manipulation there . Below is the working code in the Service-now console and also i have the code which counts the excel rows but not able to achieve in portal.. Below are both the codes.
Excel mandatory working in console but not in portal:
Client script
function onSubmit() {
var cat_id = sysparm_item_guid.value;
var attObj = new GlideRecord('sys_attachment');
attObj.addQuery('table_name', 'sc_cart_item');
attObj.addQuery('table_sys_id', cat_id);
attObj.query();
if (attObj.hasNext()) {
while (attObj.next()) {
var typeMatch = 'ms-excel,spreadsheetml';
var typeStr = attObj.content_type.toString();
var typeChk = (typeStr.indexOf(typeMatch[0]) > -1 || typeStr.indexOf(typeMatch[1]) > -1);
var fName = attObj.file_name.toString();
var regex = /\.xlsx$/g;
var extChk = regex.test(fName);
if(!typeChk || (typeChk && !extChk)) {
alert("You must use an Excel spreadsheet for uploading, please remove the existing file and re-attach");
return false;
}
}
}
}
Counting nunmber of rows
Background script
var count=0;
var parser = new sn_impex.GlideExcelParser();
var attachment = new GlideSysAttachment();
// use attachment sys id of an excel file attachment
var attachmentStream = attachment.getContentStream('6e9c7aad1b772850c1534159cc4bcb5d'); //sys_id of record from sys_attachment table
parser.parse(attachmentStream);
//retrieve the column headers
var headers = parser.getColumnHeaders();
gs.print("Apoorva"+headers );
var key = headers[0];
var value = headers[1];
while(parser.next())
{
count++;
var row = parser.getRow();
//print row value for both columns
gs.print(row[value]) ; //Uncomment this to get actual data.
}
gs.print('Number of Rows in excel attached are '+count);

- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
What version are you on?
I would hold off on this until you have upgraded to Paris, which supports an Attachment Variable type, that you can make conditionally mandatory and enforce file type.
No point creating custom code when this feature is just around the corner.
https://docs.servicenow.com/bundle/quebec-servicenow-platform/page/product/service-catalog-management/reference/r_VariableTypes.html
Consider Bookmarking my blog that I keep up to date on Attachments, where I detail this approach.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Do you also have something in which we can calculate the rows in the attached excel in the service portal.

- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
It would require significant custom code development to achieve this.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
OOB attachment variable allows you to attach multiples as such without user realising they attached multiple attachments and when you delete the one that is visible as such and submit previous attachments are visible on the backend, behaviour is very flakey and not user friendly thats why OOB though seems like a nice clean option it dont work as it says on the tin
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
The OOTB functionality makes attachments mandatory under any circumstances while the scripted solution allows for attachments to only be made mandatory under certain conditions (line 4 of the onSubmit function)

- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hi all,
Great info here and appreciate all the work put in. I'm curious if the approaches and code used through out apply to Employee Service Center as well?
Daniel
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hi Friends
I want to hide SYSTEM STATUS icon from service portal but admin can see that system status icon how to do kindly suggest.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hi All,
How to make "Attachments" mandatory on a catalog item in the Service Portal (SP) based on a specific variable value.
if request type is- dhcp_scope_creation then give alert to add the attachment, after adding attachment allow to submit the form., i tried using onsubmit client script but it is not working, please suggest on it.