Script include not working for onchange catalog client script

chandan15
Tera Contributor

Hi, 

We have a catalog item where we have 3 variable, first variable 'Find' getValue(find) and will search all the "KB_KNOWLEDGE" body, and fetch all kb number, and the second one will replace all the KB body with new texts.

I have write below script include and client script which fetching the empty data.

Script Include:

 

var findKB = Class.create();
findKB.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    
    getGEMSKB: function() {
        var fvar = current.variables.find;
        var gr = new GlideRecord('kb_knowledge');
        gr.addQuery('text', 'CONTAINS', fvar);
        gr.query();
        var kbnum = ''; // Declare kbnum outside the if block

        if (gr.next()) {
            kbnum = gr.number; // Assign the value inside the if block
        }
        return kbnum;
    },
    
    type: 'findKB'
});

 

Client script-

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var fvar = g_form.getValue('find');
    var ga = new GlideAjax('findKB');
    ga.addParam('sysparm_name', 'getGEMSKB');
    ga.getXML(getValue);
}

function getValue(response, fvar) {
    var answer = response.responseXML.documentElement.getAttribute("answer");
    console.log('Response String: ' + answer);
	g_form.addInfoMessage(answer);
    g_form.setValue('kb_articles_with_find_term', answer);
}

 

Passing empty addInfoMsg-

chandan15_0-1691468074474.png

 

1 ACCEPTED SOLUTION

Rahul Talreja
Mega Sage
Mega Sage

Hi @chandan15 ,
Try with :

 

var findKB = Class.create();
findKB.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    
    getGEMSKB: function() {
        var fvar = this.getParameter('sysparm_fvariable');
        var gr = new GlideRecord('kb_knowledge');
        gr.addQuery('text', 'CONTAINS', fvar);
        gr.query();
        var kbnum = ''; // Declare kbnum outside the if block

        if (gr.next()) {
            kbnum = gr.number; // Assign the value inside the if block
        }
        return kbnum;
    },
    
    type: 'findKB'
});
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    //var fvar = g_form.getValue('find');
   // Hope you wanted to pass the new value of the on change field every time.Use below line
   var fvar = newValue;
    var ga = new GlideAjax('findKB');
    ga.addParam('sysparm_name', 'getGEMSKB');
    ga.addParam('sysparm_fvariable',fvar);
    ga.getXML(response);
}

function response(res) {
    var answer = res.responseXML.documentElement.getAttribute("answer");
    console.log('Response String: ' + answer);
	g_form.addInfoMessage(answer);
    g_form.setValue('kb_articles_with_find_term', answer);
}

 

Please mark my response correct/helpful as applicable!
Thanks and Regards,
Rahul

View solution in original post

4 REPLIES 4

Rahul Talreja
Mega Sage
Mega Sage

Hi @chandan15 ,
Try with :

 

var findKB = Class.create();
findKB.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    
    getGEMSKB: function() {
        var fvar = this.getParameter('sysparm_fvariable');
        var gr = new GlideRecord('kb_knowledge');
        gr.addQuery('text', 'CONTAINS', fvar);
        gr.query();
        var kbnum = ''; // Declare kbnum outside the if block

        if (gr.next()) {
            kbnum = gr.number; // Assign the value inside the if block
        }
        return kbnum;
    },
    
    type: 'findKB'
});
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    //var fvar = g_form.getValue('find');
   // Hope you wanted to pass the new value of the on change field every time.Use below line
   var fvar = newValue;
    var ga = new GlideAjax('findKB');
    ga.addParam('sysparm_name', 'getGEMSKB');
    ga.addParam('sysparm_fvariable',fvar);
    ga.getXML(response);
}

function response(res) {
    var answer = res.responseXML.documentElement.getAttribute("answer");
    console.log('Response String: ' + answer);
	g_form.addInfoMessage(answer);
    g_form.setValue('kb_articles_with_find_term', answer);
}

 

Please mark my response correct/helpful as applicable!
Thanks and Regards,
Rahul

Awesome, it started working, Since a long time I am working in multiple script, but still I am need help for debug. Thank you very much.

Nayan  Dhamane
Kilo Sage
Kilo Sage

Hello @chandan15 ,

The main issue in the script is you have not passed the find value parameter from client script to script inlcude and then call the same in the script inlcude.

Please use the below script snippets:

var findKB = Class.create();
findKB.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    
    getGEMSKB: function() {
        var fvar = this.getParameter('sysparm_var');
        var gr = new GlideRecord('kb_knowledge');
        gr.addQuery('text', 'CONTAINS', fvar);
        gr.query();
        var kbnum = ''; // Declare kbnum outside the if block

        if (gr.next()) {
            kbnum = gr.number; // Assign the value inside the if block
        }
        return kbnum;
    },
    
    type: 'findKB'
});
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var fvar = g_form.getValue('find');
    var ga = new GlideAjax('findKB');
    ga.addParam('sysparm_name', 'getGEMSKB');
    ga.addParam('sysparm_var',fvar);
    ga.getXML(response);
}

function response(res) {
    var answer = res.responseXML.documentElement.getAttribute("answer");
    console.log('Response String: ' + answer);
	g_form.addInfoMessage(answer);
    g_form.setValue('kb_articles_with_find_term', answer);
}
If my answer solved your issue, please mark my answer as Correct & Helpful based on the Impact

Best Regards,
Nayan Dhamane
ServiceNow Community Rising Star 2023.

Harish KM
Kilo Patron
Kilo Patron

Hello in your script include replace the below line

 var fvar = current.variables.find;
to 
var fvar = this.getParameter('sysparm_var'); //this one needs to be passed from your client script which you have
Regards
Harish