- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-06-2023 12:38 PM
We are running a little behind the curve and our Prod instance has not yet been upgraded to Tokyo (or Utah) but that fix is coming fast.
Anyway, my issue is that I was fixing a syntax error in a Script Include that is now displaying another "error" flag, but only on our two sub-prod instances that are on Tokyo. The error states "JSON is not a function" and this is the script which, BTW, continues to work despite the error.
var AttachKB = Class.create();
AttachKB.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getAttachedKB : function(){
var wNotes = this.getParameter('sysparm_wNotes');
var kbNum = this.getParameter('sysparm_kbNum');
var gr = new GlideRecord('kb_knowledge');
gr.addQuery('sys_id', kbNum); //need to match the kb article
gr.addQuery('u_customer_facing', true);
gr.query();
if (gr.hasNext()) {
if (wNotes.includes('[code]<a title=')) { //* to fix "unexpected constant condition" error
var obj = {comments: wNotes};
var data = new JSON().encode(obj); //! this is giving an error "JSON is not a function" but the script works
return data;
}
}
},
type: 'AttachKB'
});
I don't like the fact that I'm fixing one issue and now seeing another, and unsure if I should just ignore it, seeing as it still works, or if this is now a "ticking" issue that will break sometime in the future.
Has anyone else seen this error, and/or should I be concerned?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-06-2023 01:42 PM
Overall your help worked and the script is now working but I do need to point out a minor issue with your second "fix".
Adding quotes to the wNotes part of the Obj had the effect of returning wNotes and not the expected value. Removing the quotes from this part returned the expected value.
So the final, and now fully working version is:
var AttachKB = Class.create();
AttachKB.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getAttachedKB : function(){
var wNotes = this.getParameter('sysparm_wNotes');
var kbNum = this.getParameter('sysparm_kbNum');
var gr = new GlideRecord('kb_knowledge');
gr.addQuery('sys_id', kbNum); //need to match the kb article
gr.addQuery('u_customer_facing', true);
gr.query();
if (gr.hasNext()) {
if (wNotes.includes('[code]<a title=')) { //* to fix "unexpected constant condition" error
var obj = {"comments": wNotes};
var data = JSON.stringify(obj);
return data;
}
}
},
type: 'AttachKB'
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-06-2023 01:05 PM - edited 07-06-2023 01:13 PM
Hi @Ian Mildon
Replace line - var data = new JSON().encode(obj);
With below line
var data = JSON.stringify(obj);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-06-2023 01:10 PM - edited 07-06-2023 01:11 PM
Also add quotes in the obj you created
var obj = {"comments": "wNotes"};
Check below example for reference
var str = '{"name":"George","lastname":"Washington"}';
var obj = JSON.parse(str);
gs.info('The first name is ' + obj.name);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-06-2023 01:42 PM
Overall your help worked and the script is now working but I do need to point out a minor issue with your second "fix".
Adding quotes to the wNotes part of the Obj had the effect of returning wNotes and not the expected value. Removing the quotes from this part returned the expected value.
So the final, and now fully working version is:
var AttachKB = Class.create();
AttachKB.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getAttachedKB : function(){
var wNotes = this.getParameter('sysparm_wNotes');
var kbNum = this.getParameter('sysparm_kbNum');
var gr = new GlideRecord('kb_knowledge');
gr.addQuery('sys_id', kbNum); //need to match the kb article
gr.addQuery('u_customer_facing', true);
gr.query();
if (gr.hasNext()) {
if (wNotes.includes('[code]<a title=')) { //* to fix "unexpected constant condition" error
var obj = {"comments": wNotes};
var data = JSON.stringify(obj);
return data;
}
}
},
type: 'AttachKB'
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-06-2023 01:46 PM