How do I make the attachment field mandatory in Service Portal or Employee Center?

anamariatam
Tera Expert

 

Hello, I just wanted to contribute by sharing how to make an attachment mandatory in ServiceNow when, for some reason, you can’t use the default “Mandatory attachment” button on the catalog item. I found a very simple way to do it and wanted to share it since there isn’t a place in the Community that gets straight to the point.

1. Create a Catalog Client Script

2. Required things to fill in:

  • Type: OnSubmit
    (We do it in OnSubmit because this will prevent the user from submitting the form until they attach something.)

Only select Applies on a Catalog Item view. If you select Applies on Requested Item and Catalog Task, it will cause an error, especially if the user removes the attachment.

In the script, copy this:

 

 
function onSubmit() { if (this.document.getElementsByClassName('get-attachment').length == 0) { g_form.addErrorMessage('please attach something'); return false; } }
-----------------------------------------------------------------------------------------------------------------------------------------------
 

g_form.addErrorMessage('please attach something');

 

This part of the code will show a default ServiceNow alert, informing the user that they cannot submit anything until an attachment is added:

And that’s it — it’s that simple.

Greetings from Colombia

 

2 ACCEPTED SOLUTIONS

Ankur Bawiskar
Tera Patron

@anamariatam 

why you can't use "Mandatory Attachment" checkbox for portal?

Your requirement can be achieved without scripting

Simply enable that checkbox

AnkurBawiskar_0-1771213547351.png

 

💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 10x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

SohamTipnis
Mega Sage

Hi @anamariatam,

 

There is a mandatory attachment checkbox option in portal settings; you can select that.

 

If you find my answer useful, please mark it as Helpful and Correct ‌😊


Regards,
Soham Tipnis
ServiceNow Developer ||  Technical Consultant
LinkedIn: www.linkedin.com/in/sohamtipnis10

View solution in original post

4 REPLIES 4

Nayan ArchX
Tera Guru

Hi,

 

If it is a Catalog item

 - go to the maintain item --> Catalog item

- Create a UI policy 

- Make the variable field mandatory.

 

 

Ankur Bawiskar
Tera Patron

@anamariatam 

why you can't use "Mandatory Attachment" checkbox for portal?

Your requirement can be achieved without scripting

Simply enable that checkbox

AnkurBawiskar_0-1771213547351.png

 

💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 10x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

SohamTipnis
Mega Sage

Hi @anamariatam,

 

There is a mandatory attachment checkbox option in portal settings; you can select that.

 

If you find my answer useful, please mark it as Helpful and Correct ‌😊


Regards,
Soham Tipnis
ServiceNow Developer ||  Technical Consultant
LinkedIn: www.linkedin.com/in/sohamtipnis10

dwtaber
Tera Expert

I have no idea why two replies saying "just use Mandatory Attachment" are marked as accepted solutions when the poster specifically says they're addressing situations where that approach won't work.

 

In my case, I have a Catalog Item where attachments should only be mandatory under certain conditions, so the Mandatory Attachment checkbox doesn't do me any good. I could use attachment variables, but since there's not a specific number of required attachments, and since attachment variables can't be used in an MRVS, I would have to make some fixed number of variables and hope for the best.  @anamariatam, thanks for providing such a good alternative.