JSON.decode() multidimentional array variable passed to a Script Include via UI Page

xiaix
Tera Guru

UI Page (sample) data:   infoArray[tabName].length is 10590

var mainDataArray = [];

for (var i = 0; i < infoArray[tabName].length; i++)

{

      mainDataArray.push({

                              'Supplier Name':infoArray[tabName][i]['Supplier Name'],

                              'Invoice Number':infoArray[tabName][i]['Invoice Number'],

                              'Invoice Amount':infoArray[tabName][i]['Invoice Amount'],

                              'Payment Method':infoArray[tabName][i]['Payment Method'],

                              'Check Number':infoArray[tabName][i]['Check Number'],

                              'Check Date':infoArray[tabName][i]['Check Date'],

                              'Accounting Date':infoArray[tabName][i]['Accounting Date'],

                              'Check Amount':infoArray[tabName][i]['Check Amount'],

                      });

}

var ga = new GlideAjax('glideAjax_CheckRequest_processPayments');

ga.addParam('sysparm_name', 'processData');

ga.addParam('sysparm_array', mainDataArray);

ga.getXML(_parseGlideAjaxAnswer_check_request_import_excel_payments);

Cool, at this point, the mainDataArray array has a length of 10590... so far so good.

Script Include:

var glideAjax_CheckRequest_processPayments = Class.create();

glideAjax_CheckRequest_processPayments.prototype = Object.extendsObject(AbstractAjaxProcessor, {

      processData: function()

      {

              var importedArray = this.getParameter('sysparm_array');

              var ary = new JSON().decode(importedArray);

              gs.log("*** ary.length: " + ary.length);

              for (var i = 0; i < ary.length; i++)

              {

                      gs.log("*** in: " + ary[i]['Invoice Number']);

              }

              return ary.length;

      }

});

The gs.log("*** ary.length: " + ary.length); comes out to be 2

I've done some JSON.parse'ing and other gs.log stuff to determine that my issue is the fact this is a multidimentional array.

I'm not a JSON guy so can a JS/JSON gruru kindly instruct me what code I need to change to get the Script Include's for() loop to work the way I have it depicted?

Thanks!   (this proves I'm not the code-jedi I thought I was... lol)

1 ACCEPTED SOLUTION

larstange
Mega Sage

HI



I don't think you can pass your "mainDataArray" array directly as a parameter to the AJAX call.


You need to convert this to a json string first, so change it to



ga.addParam('sysparm_array', JSON.stringify(mainDataArray));



And then in your script include you convert it back again



var ary = JSON.parse(importedArray);


View solution in original post

9 REPLIES 9

larstange
Mega Sage

HI



I don't think you can pass your "mainDataArray" array directly as a parameter to the AJAX call.


You need to convert this to a json string first, so change it to



ga.addParam('sysparm_array', JSON.stringify(mainDataArray));



And then in your script include you convert it back again



var ary = JSON.parse(importedArray);


Well... it appears as if ServiceNow has a limit on the size of data you can send as an ajax parameter.



I get this error:


find_real_file.png



And when I click on it...


find_real_file.png



And when I click on the js_includes link:


find_real_file.png




For testing purposes, I limited the mainDataArray to a sample of 5 objects (instead of 10,800), and used your method which worked perfectly!   So, your answer is correct and I will mark it as such, but I need to know if this size limitation is on the ServiceNow end or my browser end?



I'm trying to pass this:


find_real_file.png



So trying to pass 2.4 million text characters as a string to a Script Include fails.



Again, I ask, is this size limitation is on the ServiceNow end or my browser end?


The POST limit depends on the webserver.



In apache tomcat this is 2 MB if ServiceNow has not configured it otherwise. tomcat - Is there a max size for POST parameter content? - Stack Overflow


Thank you so much.   I'll open an incident with HI and see what our instance server setting is, and if they can raise it.



Again, thank you Lars for all the excellent info!