We're reclaiming inactive PDIs to keep them available for active builders. Learn what's changing, who's affected, and how to protect your work. Read More

OnChange script is not working for HTML type variable in the catalog item

Samiksha2
Mega Sage

Hi All,

I have a variable set in which there is a HTML type variable. 

I have to restrict the characters limit to 32,767.


This the script:

alert("OnChange client script ") ;
 var desc= g_form.getValue('description');
      if (desc.length > 32767) {
		alert("You cannot enter more than 32767 characters.");
        return false; 
    }


I just added alert, but onChange script in not working.

Thanks,
Sam

24 REPLIES 24

I have checked adding script in both variable set and in the catalog form. Im not sure why it is not working. It is weird. 

akash2kumar
Tera Contributor

I have same question, i was doing a count of words, but after the Yokohama upgrade its not working, 
anyone of you find out solution for this, 
onChnage on HTML variable triggers only one time(as onload then it wont trigger again)

HI @akash2kumar ,
I changed the client script type from Onchange to OnSubmit. Then it worked.

Thanks,
Sam

Hi, Found a solution for this 100% working in Yokohama upload this xml to create a new UI Scripts spTinymceEditor.xml
 
1. Navigate to the Service Portal Configuration by going to Service Portal Themes.
5. Select the portal theme 
6. Under the JS Includes section, include a new JS include., in which, search for the spTinymceEditor UI Script that that was created earlier(spTinymceEditor).

 

Select it and save the changes.
7. Boom , check the catalog/record producer which you have issue with onchange HTML Variable. 

Hope this help everyone.

_ukasz Rybicki
Giga Guru

Problem Name: Enforcing Minimum Character Length in Client Script


General Solution Proposal:

To ensure users input a minimum number of characters in a field (e.g., 50 characters in 'additional_comments'), implement a combination of onChange and onSubmit client scripts. The onChange script provides immediate feedback as the user types, while the onSubmit script prevents form submission if the requirement isn't met. This dual approach enhances user experience and data integrity.


Detailed Step-by-Step Solution:

  1. Implement an onChange Client Script:

    • Purpose: Provide real-time feedback when the user inputs data.

    • Script:

      function onChange(control, oldValue, newValue, isLoading) {
          if (isLoading || newValue == '') {
              return;
          }
          var minLength = 50;
          if (newValue.length < minLength) {
              g_form.showFieldMsg('additional_comments', 'Please enter at least 50 characters.', 'error');
          } else {
              g_form.hideFieldMsg('additional_comments', true);
          }
      }
    • Explanation: This script checks the length of the input in the 'additional_comments' field. If it's less than the required minimum, it displays an error message; otherwise, it clears any existing error messages.

  2. Implement an onSubmit Client Script:

    • Purpose: Prevent form submission if the minimum character requirement isn't met.

    • Script:

      function onSubmit() {
          var minLength = 50;
          var comments = g_form.getValue('additional_comments');
          if (comments.length < minLength) {
              g_form.showFieldMsg('additional_comments', 'Please enter at least 50 characters.', 'error');
              return false;
          }
          return true;
      }
    • Explanation: Before the form is submitted, this script checks the length of the 'additional_comments' field. If it's below the minimum, it displays an error message and prevents submission.

  3. Testing the Solution:

    • Procedure:

      • Navigate to the form containing the 'additional_comments' field.

      • Input fewer than 50 characters and observe the immediate error message from the onChange script.

      • Attempt to submit the form and confirm that submission is blocked with an appropriate error message.

      • Input 50 or more characters and verify that the error message disappears.

      • Submit the form and ensure it proceeds without issues.

    • Expected Outcome: The form should only be submittable when the 'additional_comments' field contains at least 50 characters.


Example Use Case:

Scenario: In an Incident Management module, it's essential to have detailed descriptions for effective issue resolution. By enforcing a minimum character limit on the 'additional_comments' field, users are prompted to provide comprehensive information, aiding support teams in understanding and addressing incidents promptly.


Preparation for Discussion:

This solution leverages standard ServiceNow client scripting practices to enforce data quality. By combining onChange and onSubmit scripts, it ensures both real-time user feedback and data validation upon submission. This approach aligns with best practices for enhancing user experience and maintaining data integrity.


Sources:

  1. ServiceNow Community Discussion:

    • Title: Minimum character onChange for Client Script

    • Link: ServiceNow Community

    • Summary: Discusses implementing minimum character validation using client scripts.

  2. ServiceNow Community Discussion:

    • Title: How to set minimum character length for a multi-line text variable in a catalog item

    • Link: ServiceNow Community

    • Summary: Explores setting minimum character limits on multi-line text fields using client scripts.

  3. ServiceNow Community Discussion:

    • Title: Is it possible to add minimum character required for 'Multi-line text' variable type in Service Catalog?

    • Link: ServiceNow Community

    • Summary: Addresses enforcing minimum character requirements on multi-line text variables in the Service Catalog.


If you need further assistance or have specific requirements, feel free to ask!