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

Ankur Bawiskar
Tera Patron
Tera Patron

@Vamsi26 

I agree with Brad here. Please use GlideAjax or getReference with callback

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Runjay Patel
Giga Sage

Hi @Vamsi26 ,

 

You need to make Ajax call to get the count.

Create script include and call that from your catalog client script.

Do like below.

Client script code

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || !newValue) {
        return;
    }

    var ga = new GlideAjax('KBWordCount');
    ga.addParam('sysparm_name', 'getWordCount');
    ga.addParam('sysparm_kbId', newValue);
    ga.getXMLAnswer(function (response) {
        var result = JSON.parse(response);
        if (result.error) {
            g_form.showFieldMsg('word_count', result.error, 'error');
        } else {
            g_form.setValue('word_count', result.wordCount);
            g_form.showFieldMsg('word_count', 'Word count: ' + result.wordCount, 'info');
        }
    });
}

 

Script include code:

getWordCount: function (kbId) {
        var result = {};
        var kbaRecord = new GlideRecord('kb_knowledge');
        if (kbaRecord.get(kbId)) {
            var title = kbaRecord.getValue('short_description') || '';
            var text = kbaRecord.getValue('text') || '';
            var combinedText = title + " " + text;
            result.wordCount = combinedText.trim().split(/\s+/).length;
        } else {
            result.error = 'Knowledge article not found.';
        }
        return JSON.stringify(result);
    },

 

Make sure script include is checked for client callable.

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------

Bhupender
Tera Expert

Hello @Vamsi26,

GlideRecord is a server-side script, and while it is possible to use it in a client script, doing so can lead to performance issues. Additionally, it is not considered a best practice to use GlideRecord in client scripts.

1. In your case, you can use GlideAjax along with a script include to address the problem and ensure your code executes faster.

2. Also, please make sure that you are using the correct field name, which is 'word_count':

  g_form.setValue('word_count', wordCount);

 

I hope this helps!