Unable to make mandatory in catalog item Attachment

rajeshraji1
Tera Contributor

Unable to make mandatory in catalog item Attachment

I have tried to change attachment mandatory in catalog item this logic is working up to false attachment but after upload also i could not able to submit the catalog  item kindly suggest .

 

function onSubmit() {
  // If the user selects "2" in the select_choice variable
 var chc= g_form.getValue('select_choice');
    // Ask user explicitly if they have uploaded a file
    if (chc == '2'){
        g_form.setValue('mandatory_attachment',true);
      g_form.addErrorMessage('Please confirm you have attached a file.');
      return false;
    } return true;
  }
 

 

4 REPLIES 4

Abbas_5
Tera Sage
Tera Sage

Hello @rajeshraji1,

 

To make attachments mandatory for a ServiceNow catalogue item, you can use a catalogue client script that checks for the presence of attachments before submissionThis script can be configured to trigger when a checkbox or other variable indicates that an attachment is required. Additionally, you can utilise UI policies to control the visibility and mandatory status of the attachment field based on other variables. 
 
Steps to implement mandatory attachments:
  1. Create a Catalog Client Script:
    • Navigate to "Service Catalog" > "Catalog Items".
    • Select the catalog item and go to "Related Links" > "Catalog Client Scripts".
    • Create a new script named something like "Check for Required Attachment".
    • Set the script type to "onsubmit" and ensure "Isolate Script" is disabled (if using DOM manipulation).
  2. Write the Script Code: 
    function onSubmit() {      var condition = g_form.getValue('your_checkbox_variable'); // Replace with your condition variable      if (condition == true) {        var attachments = this.document.getElementsByClassName('get-attachment');        if (attachments.length == 0) {          g_form.addErrorMessage('Please attach a file.');          return false; // Prevent form submission        }      }    }
  • Replace 'your_checkbox_variable' With the actual name of the checkbox or other variable used to trigger the mandatory attachment requirement. 
     
  • The code checks if the condition is met and then verifies if any attachments are present.
  • If no attachments are found, an error message is displayed, and the submission is prevented. 
     
  1. 1. Add the Script to the Catalog Item:
    • In the catalog item, go to the "Related Links" > "Catalog Client Scripts" and add the script you created. 
       
  2. 2. Consider UI Policies:
    • If you need to control the visibility or mandatory status of the attachment field based on other variables, you can use UI policies. 
       
    • Create a UI policy that sets the visibility or mandatory status of the attachment field based on the value of a trigger field. 
       
Alternative approach (for Service Portal):
  • If you're using the Service Portal, you can also use the "Mandatory Attachment" checkbox in the catalog item's portal settings, as shown in the ServiceNow documentation.
  • This setting automatically handles the mandatory requirement within the portal's UI. 
     
Key considerations:
  • Isolate Script:
    When using client scripts that manipulate the DOM (e.g., by checking for the presence of attachments using getElementsByClassNameEnsure that "Isolate Script" is set to "false" to allow the script to interact with the form's elements. 
     
  • Service Portal:
    If you're using the Service Portal, leverage the "Mandatory Attachment" checkbox in the portal settings for simpler implementation. 
     
  • Error Messages:
    Provide clear and informative error messages to users to indicate when an attachment is required. 
     
  • Testing:
    Thoroughly test the implementation to ensure that the mandatory attachment functionality works as expected. 
     
    If this is helpful, please hit the thumbs up button and accept the correct solution by referring to this solution in the future. It will be helpful to them.
     
    Thanks & Regards,
    Abbas Shaik

hi@Abbas_5 Your script is working fine with service portal but in test catalog view environment i  am getting this error it make any impact in future 

?

@Abbas_5 This script not working in catalog view making popup but working in SP view 

Deepak Shaerma
Kilo Sage

Hi @rajeshraji1 

Please use the below onChange catalog client script: 

var Dropdown_value = g_form.getValue('field_name');

    if (Dropdown_value == '2') {
        g_form.setVisible('attachment_field', true);
        g_form.setMandatory('attachment_field', true);
    } else {
        g_form.setMandatory('attachment_field', false);
        g_form.setVisible('attachment_field, false);
    }

 Note: Return true or false better works on onSubmit Client Scripts:

DeepakShaerma_0-1748841249116.png

Please mark my answer as helpful/correct if it resolves your query. This will help other community members as well.

Regards,
Deepak Sharma