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

Naveen Velagapu
Mega Guru

you need to convert the array to JSON object and pass that value as a parameter before you use JSON.decode. you can use stringify or someother api to convert.


larstange
Mega Sage

No problem - do report any relevant feedback from the HI incident back to this thread.


No reply yet from the HI ticket I put in, but I did manage to find a   work-around if HI comes back and says they can't do anything.



First, after a lot of testing I found out that I can push 1.33 megabytes of data to the Script Include param before it'll error out.



For my situation, this equates to my mainDataArray to contain no more than ~6100 elements, which equates to ~1,396,635 characters.



So what I did is break it up, like this:



var startIndex = 0;


var endIndex = 0;


for (var f = 0; f < mainDataArray.length; f++)


{


      if (f % 5000 == 0)


      {


              if (f > 1)


              {


                      startIndex = endIndex;


                      endIndex = f;                      


                      var slicedArray = mainDataArray.slice(startIndex, endIndex);                      


                      var aTest = JSON.stringify(slicedArray);


                      var ga = new GlideAjax('glideAjax_CheckRequest_processPayments');


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


                      ga.addParam('sysparm_array', aTest);


                      ga.getXML(_parseGlideAjaxAnswer_check_request_import_excel_payments);


              }


      }


}



if (f % 5000 > 0)


{


      var slicedArray = mainDataArray.slice(endIndex, f);


      var aTest = JSON.stringify(slicedArray);


      var ga = new GlideAjax('glideAjax_CheckRequest_processPayments');


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


      ga.addParam('sysparm_array', aTest);


      ga.getXML(_parseGlideAjaxAnswer_check_request_import_excel_payments);


}







The above code breaks up my mainDataArray into chuncks of 5000 elements and then processes the remainder.



This code tested wonderfully.   So, if HI comes back with being unable to perform my request, at least I'll have this to fall back on.


Please reference for further info on the POST limit: https://hi.service-now.com/kb_view.do?sysparm_article=KB0522725



Thanks!


find_real_file.png



At least this was stated "I have opened an enhancement request (FTASK29722) for the possibility to have the limit increased in a future release."



So perhaps in the future it'll be updated.



I'm glad to have a definitive answer though!