Service Portal Catalog Items: Hide Attachment / Ma... - ServiceNow Community
Mark Roethof
Tera Patron
Tera Patron

Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

 

Hi there,

 

A short article to share gained knowledge about hiding attachments and making attachments mandatory for Catalog Items on the Service Portal. Sharing this because numerous times threads with questions about hiding attachments or making attachments mandatory are coming by.

Pre-Madrid

Pre-Madrid, we could script this thru Catalog Client Scripts. Another option would be to customize the SC Catalog Item widget. Both not great, the Catalog Client Scripts are needed on every Catalog Item you wish to have this functionality on, customizing the SC Catalog Item would mean you would miss updates from patches/upgrades.

 

For if you are not yet on Madrid and do want to hide attachments or make attachments mandatory. Here is some example scripting.

 

Hide attachment - onLoad Catalog Client Script

function onLoad() {
	
	var attachmentButton = top.document.getElementsByTagName('sp-attachment-button');
	attachmentButton[0].parentNode.hidden = true;
	
}

 

Mandatory attachment - onSubmit Catalog Client Script

function onSubmit() {
  
	if(this.document.getElementsByClassName('get-attachment').length == 0) {
		g_form.addErrorMessage(getMessage('mandatory_attachment'));

		return false;
	}	
   
}

 

Madrid

With Madrid, some cool and super easy checkboxes are introduced! Checkboxes which can be configured for Catalog Items individually and making scripts like above obsolete.

Madrid checkboxes

find_real_file.png

---

And that's it actually. Hope you like it. If any questions or remarks, let me know!

 

C

If this content helped you, I would appreciate it if you hit bookmark or mark it as helpful.

 

Interested in more Articles, Blogs, Videos, Podcasts, Share projects I shared/participated in?
- Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

 

Kind regards,


Mark Roethof

ServiceNow Technical Consultant @ Paphos Group
---

LinkedIn

Comments
Mark Roethof
Tera Patron
Tera Patron

One addition. If you are using a clone of the SC Catalog Item widget [widget-sc-cat-item-v2], then, unfortunately, this will not work. The out-of-the-box SC Catalog Item has been updated with code like:

Body HTML Template

ng-if="c.showAttachments()"

Client Controller

c.showAttachments = function() {
return !$scope.submitted &&
!c.data.sc_cat_item.no_attachments &&
c.data.sc_cat_item.sys_class_name !== "std_change_record_producer";
}

So interested in this new addition from Madrid, update your cloned widget.

Kind regards,
Mark

---

LinkedIn

Brian Lancaster
Tera Sage

In my case for attachments and I'm sure other a check box with attachment mandatory would not work.  In my current environment attachments are mandatory base on variable choices. For example, on one of my forms it could have anywhere between 0 - 4 attachments mandatory.  It would be nice if they add a drop down with "Always Mandatory" and "Condition Base".  Then if you choose condition based you would choose under what conditions attachments are mandatory.  Although thinking about it that might not necessary meet number of attachments unless there was a filed for that as well. 

Scott Van Den E
Giga Explorer

I need conditional control of attachments based on field values, which I would ordinarily use a catalog client script to configure.

Can you advise if I need the attachments to be visible based on field selection?

Scott Van Den E
Giga Explorer

Brian, did you find any way to achieve your desired results?

Feddy
Tera Guru

Hide attachment check box is inactivated by system in our instance, what would be the issue.
I couldnt see the field in list or form even after making it active.

amol_joshi
Tera Contributor

How to achieve mandatory attachment before submitting a workspace form?

Tang
Tera Contributor

Hi Mark,

 

Your client script - onLoad is working. Thanks

function onLoad() {

var attachmentButton = top.document.getElementsByTagName('sp-attachment-button');
attachmentButton[0].parentNode.hidden = true;

 

I tried to use the same script for onChange catalog client script but it is not working as expected when I change a value of a field. Please advise. Thanks

Mark Roethof
Tera Patron
Tera Patron

@Tang Please share details, without no one can help you. Or better: create a new question on the community and provide as much as info that you can.

Tang
Tera Contributor

Hi Mark,

Here is the form

Tang_0-1725234718605.png

On the form itself, the field "Suspect an email ccontains MALICIOUS CONTENT?" only have two options i.e. either Yes or No. If a user select the option Yes, then the paper clip Add attachments should be hide. However, it is not working as expected.

Here is the onChange catalog client script. Please advise. Thanks

Tang_1-1725235031962.png

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
    var mal = g_form.geValue('malicious_content');
   
    if (mal == 'Yes') {
    var attachmentButton = top.document.getElementsByTagName('sp-attachment-button');
        attachmentButton[0].parentNode.hidden = true;}
}
Tang
Tera Contributor

Hi Mark,

Here is the catalog client scripts onChange

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
    var mal = g_form.geValue('malicious_content');
   
    if (mal == 'Yes') {
    var attachmentButton = top.document.getElementsByTagName('sp-attachment-button');
        attachmentButton[0].parentNode.hidden = true;}
 
When a user select the option Yes of the "malicios_content" field of the form, the Add attachments paperclip should be hide but it is not working as expected. There are only two values of the "malicious_content" field i.e. either Yes or No. Thanks  
Tang
Tera Contributor

Hi Mark,

 

I am not sure why it was not get uploaded. Let me try it again. Thanks

Tang
Tera Contributor

Please ignore my previous question. All sorted. Thanks

DeekshithK
Tera Contributor

Hi @Mark Roethof how to make attachment visible for some condtion

Version history
Last update:
‎08-10-2024 08:15 AM
Updated by:
Contributors