Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Getting "SyntaxError: Unexpected end of JSON input" on MRVS Client Script

Winston
Tera Guru

Hello everyone, 

I'm getting SyntaxError: Unexpected end of JSON input on the below client script. I'm filtering the asset MRVS by multiple locations, and trying to a tell the user they can't add more than 50 records to the MRVS. The onScript works okay but gives me that error when the first location is added. I'm guessing because when the location is added the MRVS doesn't have anything filled in so it's throwing up that error.

Is there anyway around this?

 

find_real_file.png

 

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

	var MRVS = JSON.parse(g_form.getValue('my_managed_assets'));
	var MRVSlength = MRVS.length;
	
	if (MRVSlength > 5) {
		g_form.addErrorMessage("You can not submit more than 50 assets with this form. Please remove a filter location or assets.");
		}	
	}

'

1 ACCEPTED SOLUTION

If you get the value first and then do the parse, does it make a difference?

var mrvsValue = g_form.getValue('my_managed_assets');
var MRVS = JSON.parse(mrvsValue);	
g_form.addInfoMessage(MRVS);

var MRVSlength = MRVS.length;
g_form.addInfoMessage(MMRVSlength);

 

If you run the code below, what is displayed?

var mrvsValue = g_form.getValue('my_managed_assets');
var MRVS = JSON.stringify(mrvsValue);	
g_form.addInfoMessage(MRVS);

//var MRVSlength = MRVS.length;
//g_form.addInfoMessage(MMRVSlength);

Best regards,

Isaac Vicentini
MVP 2025


If my answer was helpful, mark it as Helpful or Accept as Solution.

View solution in original post

6 REPLIES 6

These logs should be displayed on the screen, not on the console... If you just want to know the value of your 'my_managed_assets' field, is anything displayed? We need to make sure that this field has value to parse it.

var mrvsValue = g_form.getValue('my_managed_assets');
g_form.addInfoMessage(mrvsValue);

Best regards,

Isaac Vicentini
MVP 2025


If my answer was helpful, mark it as Helpful or Accept as Solution.

I'm dumb. The client script wasn't active when I was testing your suggestions, but your idea about splitting the variable up fixed it!

This is the script I used:

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
	
var mrvsValue = g_form.getValue('my_managed_assets');

if (mrvsValue != "") {
var MRVS = JSON.parse(mrvsValue);	
var MRVSlength = MRVS.length;

	if (MRVSlength > 5) {
		g_form.addErrorMessage("You can not submit more than 50 assets with this form. Please remove a filter location or assets.");
		}	
	}
}