The CreatorCon Call for Content is officially open! Get started here.

Make attachment icon mandatory with red asterisk based on condition like choice values

Shantanu_99
Tera Contributor

Hi All,

 

On Catalog item, we have attachment icon, I want to make attachment icon mandatory with red asterisk based on choice value of select box.

Currently we have two scripts for native and portal view and we are displaying error msg and avoiding form submission on the catalog item.

 

Do we have any solution for this make attachment icon mandatory with red asterisk based on choice value of select box.

 

If we use attachment field type, user can upload one file only.

 

Thanks in Advance,

Shantanu B

2 REPLIES 2

SyamPrasanM
Tera Expert

Hi @Shantanu_99 
Hope this answere is useful to your question, 

 

To make the attachment icon mandatory with a red asterisk based on a choice value in a ServiceNow catalog item, you can use client scripts to validate the attachment requirement.

 

Steps:

  1. Identify the Choice Field: Determine the field where the user makes a selection that will dictate whether the attachment is required.

  2. Client Script Creation: Create a client script that checks the value of the choice field and adds the mandatory requirement for attachments.

  3. Form Submission Handling: Prevent form submission if the attachment is required but not provided.

You can create a "Catalog Client Script" in ServiceNow with the following script.Assume you have a choice field named u_choice_field and that you want to require an attachment when a specific value (e.g., "Require Attachment") is selected.

 

Create Client Script:

  1. Navigate to Service Catalog > Catalog Definitions > Maintain Items.
  2. Open the catalog item where you want to enforce this rule.
  3. Scroll to the Catalog Client Scripts related list and click New.
  4. Choose the Type as "onSubmit".

 

function onSubmit() {
// Get the value of the choice field
var choiceValue = g_form.getValue('u_choice_field');

// Check if the specific choice value is selected
if (choiceValue === 'Require Attachment') {
// Check if there are any attachments
var attachmentCount = g_form.getAttachmentCount();

// If no attachments, show an error message and prevent submission
if (attachmentCount === 0) {
g_form.showFieldMsg('u_attachment_field', 'An attachment is required when "Require Attachment" is selected.', 'error');
return false; 
} else {
g_form.hideFieldMsg('u_attachment_field');
}
}
return true; 
}

 

Regards,

Mule Syam Prasanna

PRAVAL-LOGO.png

Amit Verma
Kilo Patron
Kilo Patron

Hi @Shantanu_99 

 

I don't think it is possible to mark the attachment icon in red but you can at least check if the user has attached at least 1 attachment or not. Refer https://www.servicenow.com/community/developer-blog/verify-mandatory-attachments-count-on-catalog-it...

 

You can customize your client script to include/exclude the check based on your select box variable value.

 

Thanks and Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.