defining dates in array elements giving error

vass2
Mega Expert

Hi Friends ,

Below i am trying to fetch some changes and update those actual start and end dates using arrays.

But its giving the following error while using arrays. Not sure what is the exact code to write here. Can you please help me on this.

//var arrayUtil = new ArrayUtil();
var stDate = new Array[ {date: "19/02/2018 09:15:33"}, {date: "06/02/2018 18:47:12"}, {date: "30/11/2017 13:28:24"}, {date: "15/02/2018 23:36:37"}];
var edDate = new Array[ {date: "19/02/2018 09:28:32"}, {date: "07/02/2018 01:34:43"}, {date: "05/02/2018 08:11:44"}, {date: "15/02/2018 23:37:38"}];
var gr = new GlideRecord('change_request');
gr.addQuery('number','IN','CHG0030526,CHG0030951,CHG0031396,CHG0031848');

gr.query();

while (gr.next() ) {

//gs.print ('Change Number ' + gr.number);

}
gs.print('Total records ' + gr.getRowCount());

--------------------------------------------------------------------------

[0:00:00.006] Script completed in scope global: script


Evaluator: org.mozilla.javascript.EcmaError: undefined is not a function.
   Caused by error in script at line 2

      1: //var arrayUtil = new ArrayUtil();
==>   2: var stDate = new Array[ {date: "19/02/2018 09:15:33"}, {date: "06/02/2018 18:47:12"}, {date: "30/11/2017 13:28:24"},   {date: "15/02/2018 23:36:37"}];
      3: var edDate = new Array[ {date: "19/02/2018 09:28:32"}, {date: "07/02/2018 01:34:43"}, {date: "05/02/2018 08:11:44"},   {date: "15/02/2018 23:37:38"}];
      4: var gr = new GlideRecord('change_request');
      5: gr.addQuery('number','IN','CHG0030526,CHG0030951,CHG0031396,CHG0031848');
1 ACCEPTED SOLUTION

Array value should be as string, in array you are not having the values in quotes. either you can have the array defines like below

 

var stDate = [
	["date: 19/02/2018 09:15:33"], 
	["date: 06/02/2018 18:47:12"], 
	["date: 30/11/2017 13:28:24"], 
	["date: 15/02/2018 23:36:37"]
];

var edDate = [
	["date: 19/02/2018 09:28:32"], 
	["date: 07/02/2018 01:34:43"], 
	["date: 05/02/2018 08:11:44"], 
	["date: 15/02/2018 23:37:38"]
];

var i= 0; // declare the i as var
var gr = new GlideRecord('change_request');
gr.addQuery('number','IN','CHG0030526,CHG0030951,CHG0031396,CHG0031848');
gr.query();
gs.print('Total records ' + gr.getRowCount());
while(gr.next()) {
	gs.print ('Change Number : ' + gr.number + 'Actual Start Date : '+ stDate[i] + 'Actual End Date : ' + edDate[i]);
	i++;
}

 

OR like below,

 

var stDate = [
	["date", "19/02/2018 09:15:33"], 
	["date", "06/02/2018 18:47:12"], 
	["date", "30/11/2017 13:28:24"], 
	["date", "15/02/2018 23:36:37"]
];

var edDate = [
	["date", "19/02/2018 09:28:32"], 
	["date", "07/02/2018 01:34:43"], 
	["date", "05/02/2018 08:11:44"], 
	["date", "15/02/2018 23:37:38"]
];

var i= 0;
var gr = new GlideRecord('change_request');
gr.addQuery('number','IN','CHG0030526,CHG0030951,CHG0031396,CHG0031848');
gr.query();
gs.print('Total records ' + gr.getRowCount());
while(gr.next()) {
	gs.print ('Change Number : ' + gr.number + ',   Actual Start Date : '+ stDate[i][1] + ',   Actual End Date : ' + edDate[i][1]);
	i++;
}

 

Another way of using is JSON, there also you need to keep the date keyword as string: you can get the JOSN Array info here: https://www.w3schools.com/js/js_json_arrays.asp

https://www.w3schools.com/js/js_json_parse.asp

View solution in original post

5 REPLIES 5

Tushar Sharma2
Kilo Guru

Hello Vass,

 

Try with below aproach and let me know if you face any challenge.

var stDate =[];
stDate.push({date: "19/02/2018 09:15:33"}, {date: "06/02/2018 18:47:12"}, {date: "30/11/2017 13:28:24"}, {date: "15/02/2018 23:36:37"});

 

Regards,

Tushar

Accept as answer if i resolved your queries

vass2
Mega Expert

Hi Tushar,

Thanks for quick response. I have did as you said, It is accepting the values into the array into one element it seems. But i want them 

to get arranged n stDate[4] elements so that i can take one on one in the loop and update the Actual start date of the corresponding 

change ticket.

Below you can find the result i got. Please can you guide me further on this.

stDate = [];
edDate = [];
stDate.push([ {date: "19/02/2018 09:15:33"}, {date: "06/02/2018 18:47:12"}, {date: "30/11/2017 13:28:24"}, {date: "15/02/2018 23:36:37"}]);
edDate.push([ {date: "19/02/2018 09:28:32"}, {date: "07/02/2018 01:34:43"}, {date: "05/02/2018 08:11:44"}, {date: "15/02/2018 23:37:38"}]);

var gr = new GlideRecord('change_request');
gr.addQuery('number','IN','CHG0030526,CHG0030951,CHG0031396,CHG0031848');

gr.query();

i= 0;
while (gr.next() ) {

gs.print ('Change Number : ' + gr.number + 'Actual Start Date : '+ stDate[i] + 'Actual End Date : ' + edDate[i]);

i++;

}
gs.print('Total records ' + gr.getRowCount());

-----------------------------------------------------------------

Result: 

*** Script: Change Number : CHG0030526Actual Start Date : [object Object],[object Object],[object Object],[object Object]Actual End Date : [object Object],[object Object],[object Object],[object Object]
*** Script: Change Number : CHG0030951Actual Start Date : undefinedActual End Date : undefined
*** Script: Change Number : CHG0031396Actual Start Date : undefinedActual End Date : undefined
*** Script: Change Number : CHG0031848Actual Start Date : undefinedActual End Date : undefined
*** Script: Total records 4

Array value should be as string, in array you are not having the values in quotes. either you can have the array defines like below

 

var stDate = [
	["date: 19/02/2018 09:15:33"], 
	["date: 06/02/2018 18:47:12"], 
	["date: 30/11/2017 13:28:24"], 
	["date: 15/02/2018 23:36:37"]
];

var edDate = [
	["date: 19/02/2018 09:28:32"], 
	["date: 07/02/2018 01:34:43"], 
	["date: 05/02/2018 08:11:44"], 
	["date: 15/02/2018 23:37:38"]
];

var i= 0; // declare the i as var
var gr = new GlideRecord('change_request');
gr.addQuery('number','IN','CHG0030526,CHG0030951,CHG0031396,CHG0031848');
gr.query();
gs.print('Total records ' + gr.getRowCount());
while(gr.next()) {
	gs.print ('Change Number : ' + gr.number + 'Actual Start Date : '+ stDate[i] + 'Actual End Date : ' + edDate[i]);
	i++;
}

 

OR like below,

 

var stDate = [
	["date", "19/02/2018 09:15:33"], 
	["date", "06/02/2018 18:47:12"], 
	["date", "30/11/2017 13:28:24"], 
	["date", "15/02/2018 23:36:37"]
];

var edDate = [
	["date", "19/02/2018 09:28:32"], 
	["date", "07/02/2018 01:34:43"], 
	["date", "05/02/2018 08:11:44"], 
	["date", "15/02/2018 23:37:38"]
];

var i= 0;
var gr = new GlideRecord('change_request');
gr.addQuery('number','IN','CHG0030526,CHG0030951,CHG0031396,CHG0031848');
gr.query();
gs.print('Total records ' + gr.getRowCount());
while(gr.next()) {
	gs.print ('Change Number : ' + gr.number + ',   Actual Start Date : '+ stDate[i][1] + ',   Actual End Date : ' + edDate[i][1]);
	i++;
}

 

Another way of using is JSON, there also you need to keep the date keyword as string: you can get the JOSN Array info here: https://www.w3schools.com/js/js_json_arrays.asp

https://www.w3schools.com/js/js_json_parse.asp

One more thing, this will not guarantee you if the start date/end date belongs to the respective change, since there is no linking with the change # you are gliding for with the start and end array dates. So, to fix the array issue you may refer the solution which i have suggested. But to make sure if the start/end date belongs to same Change Request, you may need to have some more validation to get the right value for right record.