Catalog Client Script is not working

Vamsi26
Tera Contributor

Hello Everyone

Below Catalog Client script is not working to Populate the Word Count. Could someone help me on this.

 

 

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || !newValue) {
        return;
    }
    var kbaRecord = new GlideRecord('kb_knowledge');

    if (kbaRecord.get(newValue)) {
        var title = kbaRecord.getValue('short_description');
        var text = kbaRecord.getValue('text');

        var combinedText = title + " " + text;

        var wordCount = combinedText.trim().split(/\s+/).length;

        g_form.setValue('word_count', wordCount);

        g_form.showFieldMsg('word_count', 'Word count: ' + wordCount, 'info');
    }
}

 

 

 

Thanks

7 REPLIES 7

Brad Bowman
Kilo Patron
Kilo Patron

GlideRecords in Client Scripts are not supported/recommended/best practice.  Alternatives, and good tools to have at your disposal are GlideAjax

https://www.servicenow.com/community/developer-articles/glideajax-example-cheat-sheet-updated/ta-p/2... 

or getReference with callback

https://www.servicenow.com/docs/bundle/xanadu-api-reference/page/app-store/dev_portal/API_reference/... 

 

Having said that, a GlideRecord still should work in this case.  Is the variable that this is triggered on a reference to the kb_knowledge table?  Add some alerts to see how far it is getting, and you will likely see where it is going wrong.

In this video I'll show you how to make your server side JavaScript easier to maintain. We've talked about Script Includes as a way to improve unit testing, abstracting and reusing logic, but there's another use for them to help remove hard-coded values, decrease maintenance, and improve ...

Ravi Chandra_K
Kilo Patron
Kilo Patron

Hello @Vamsi26 

Use the alert statement to see what you are getting at each step and debug further...

I would add at the beginning of if statement, wordcount.

 

Please mark the answer as helpful and correct if helped. 

Kind Regards,

Ravi Chandra  

 

 

Benjamin A
Mega Sage

Hi @Vamsi26,

 

What exactly are you trying to accomplish? What field are you using for the onChange?

 

You could accomplish all of this without using GlideAjax, which wouldn't work anyway since the updated text hasn't been saved to the database after just a field change. A save would be necessary to update the values in the database for an accurate calculation using GlideAjax.

 

 

If you are trying to display word count on the update of the Article body I'd use a script like this with the Client Script of Type onChange and Field name of Article body.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   // Check if the form is loading or if the new value is empty
   if (isLoading || newValue === '') {
      return; // Exit the function early to avoid unnecessary processing
   }
   
   // Combine the values of the 'text' field and 'short_description' field
   var combinedText = g_form.getValue('text') + g_form.getValue('short_description');
   
   // Calculate the word count by:
   // 1. Removing extra whitespace with `trim()`
   // 2. Splitting the text into words using a regular expression (`\s+` matches one or more whitespace characters)
   var wordCount = combinedText.trim().split(/\s+/).length;
   
   // Set the calculated word count in the 'word_count' field of the form
   g_form.setValue('word_count', wordCount);
}

If you simply need the word count of the body and not the short description combined with it, the rich text box for Knowledge has a built in word count in the bottom right corner.

 

Let me know if you have any follow up questions.

 

Thanks,

Ben

Sandeep Rajput
Tera Patron
Tera Patron

@Vamsi26 You are trying to use GlideRecord API in client script that too on an onChange script which could cause slowness on your form on the platform view and will not work at all on the Service Portal. Please update your script and use GlideAjax instead.