Should I be concerned with this error only in Tokyo instances

Ian Mildon
Tera Guru

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?

1 ACCEPTED SOLUTION

Ian Mildon
Tera Guru

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'
});

View solution in original post

5 REPLIES 5

Manmohan K
Tera Sage

Hi @Ian Mildon 

 

Replace line  - var data = new JSON().encode(obj);  

 

With below line

 

var data = JSON.stringify(obj);

 

 

Manmohan K
Tera Sage

@Ian Mildon 

 

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);

 

 

 

Ian Mildon
Tera Guru

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'
});

Manmohan K
Tera Sage

@Ian Mildon 

 

I did not pay attention that it was a variable,

anyways glad it worked