- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-07-2023 09:15 PM - edited 08-07-2023 09:17 PM
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-
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-07-2023 10:33 PM - edited 08-07-2023 10:56 PM
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);
}
Thanks and Regards,
Rahul
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-07-2023 10:33 PM - edited 08-07-2023 10:56 PM
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);
}
Thanks and Regards,
Rahul
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-07-2023 11:37 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-07-2023 10:42 PM
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);
}
Best Regards,
Nayan Dhamane
ServiceNow Community Rising Star 2023.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-07-2023 11:09 PM
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
Harish