Array.push not working correctly

Alex Becker
Tera Contributor

Hello, 


I created a MRVS on a catalog item,  created a client script to pass the MRVS over to a script inlcude to create a unique name from that MRVS Selections.  I have created the client script to pass the data just fine, created the Script Include and got it to push all the names out.  BUT when i try to array the names i have received it doesn't seem to do that correctly.

Client Script Code:

 

 

 

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

var name = g_form.getValue('application_service_name');
var prod = g_form.getValue('are_there_multiple_production_instances_of_this_application_service');//Yes/No
var nonProd = g_form.getValue('does_this_application_have_lower_environments');//Yes/No

if (prod == 'No'){
	alert("Service Name: " + name + "- Prod");
}
if (prod == 'Yes'){
	var prodMrvs = g_form.getDisplayValue('application_instance_market_entry');
	var gr = new GlideAjax('global.GFCatalogItemUtils');
	gr.addParam('sysparm_name', 'parseServiceName');
	gr.addParam('sysparm_mrvs', prodMrvs);
	gr.addParam('sysparm_appName', name);
	gr.getXML(getData);

	} 
	function getData(response){
		var answer = JSON.parse(response.responseXML.documentElement.getAttribute('answer'));
        if (answer) {
			var nameArray = answer.nameArray
			alert('nameArray: ' + nameArray);
	}
	}
}

 

 

 

 Script Include Code 

 

 

 

	parseServiceName: function(){
		var serviceName = {};
		var mrvs = this.getParameter('sysparm_mrvs');
		var name = this.getParameter('sysparm_appName');
		var nameArray = [];
		var data = JSON.parse(mrvs);
		for (var i = 0; i < data.length; i++){
			gs.log("AB - location: " + data[i].instance_location);
			var completeName = name + " - " + data[i].instance_location;
			gs.log("AB - complete Name: " + completeName);
			nameArray.push(completeName.toString());
			gs.log("AB : nameArray: " + nameArray);
		}
		gs.log("AB - Anything here?");
		gs.log("AB: Name Array: " + nameArray);
		serviceName.completeName = nameArray;
return JSON.stringify(serviceName);

	},

 

 

 

The log saying "AB: Name Array: " + nameArray is what i am trying to get to populate in an array.  I am not getting any sort of error at all, the log isnt even logging.  When I test this in a fix script it works as intended, just not here.

Any help on this would be great.

1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

Hi Alex,

A difference between a Fix Script and SI is that you are passing values from the client.  Are you getting either of the previous logs with the expected value?  If not, add logs earlier to confirm this function is indeed running, and the values passed in from the client.  I suspect you'll want to change your mrvs variable assignment in the client script

var prodMrvs = g_form.getValue('application_instance_market_entry');

And it's best to force values to string the first time it's used, so when assigning to a script variable rather than when pushing that script variable to an array in this case, or skip the script variable all together once you confirm the script is processing correctly.

nameArray.push(name + " - " + data[i].instance_location.toString());

 

View solution in original post

4 REPLIES 4

Craig Gruwell
Mega Sage

Hi Alex,

 

Is your script include configured as Client callable?

It is.  This script include uses other calls and works successful 

Vishal Birajdar
Giga Sage

Hi @Alex Becker 

 

In script include, in JSON object do like this:

 

var serviceName = {
completeName:'',
};

 

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

Brad Bowman
Kilo Patron
Kilo Patron

Hi Alex,

A difference between a Fix Script and SI is that you are passing values from the client.  Are you getting either of the previous logs with the expected value?  If not, add logs earlier to confirm this function is indeed running, and the values passed in from the client.  I suspect you'll want to change your mrvs variable assignment in the client script

var prodMrvs = g_form.getValue('application_instance_market_entry');

And it's best to force values to string the first time it's used, so when assigning to a script variable rather than when pushing that script variable to an array in this case, or skip the script variable all together once you confirm the script is processing correctly.

nameArray.push(name + " - " + data[i].instance_location.toString());