ArrayUtil() contains and indexOf are not working in include script

Sagaya1
Giga Expert

Hi ,

In my scripted REST API include script i use ArrayUtil to compare the string which is available in  the array, but both indexOf / contains  always return false even provided the valid input . 

Could you please help to fix the issue ?

Code :

var arrayUtil = new ArrayUtil();
var e_custName = []; 
var e_serNumber = [];

var ic_gr = new GlideRecord("u_ilist");
ic_gr.addQuery('parent', incident_sys_id);
ic_gr.query();

while (ic_gr.next()) {
e_custName.push(ic_gr.getDisplayValue('u_customer_name'));
e_serNumber.push (ic_gr.getDisplayValue('u_service_number.u_service_identifier'));
}

 

var jsonObj = JSON.parse(c_list);
for (var i = 0; i < jsonObj.length; i++) {

var customerName = jsonObj[i].customerName;
var serviceNumber = jsonObj[i].serviceNumber;

 

if((arrayUtil .contains(e_custName,customerName)) &&(arrayUtil .contains(e_serNumber,serviceNumber)) ){
// Do update

} else{

// Do insert 

}

}

I have tried many possibilities and all are always return false

 

1.  if(arrayUtil.indexOf(e_custName,customerName) && arrayUtil.indexOf(e_serNumber,serviceNumber) ){
2. if(arrayUtil.contains(e_custName,customerName) && arrayUtil.contains(e_serNumber,serviceNumber) ){
3. if((new ArrayUtil().contains(e_custName,customerName)) &&(new ArrayUtil().contains(e_serNumber,serviceNumber)) ){
 

 

 

Regards

Sagaya

}

1 ACCEPTED SOLUTION

@Sagaya 

that's because the update is outside the while and hence it won't know which record to update

you should store the values into array when you parse the JSON and then check inside the while if the array contains the value from the record

use this and test

var arr1 = [];
var arr2 = [];

var jsonObj = JSON.parse(impacted_customer_list);

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

var customerName = jsonObj[i].customerName.toString();
var serviceNumber = jsonObj[i].serviceNumber.toString();

arr1.push(customerName);
arr2.push(serviceNumber);

}

var ic_gr = new GlideRecord("u_list");
ic_gr.addQuery('parent', incident_sys_id);
ic_gr.query();
var impact_custlist_flag= ic_gr.getRowCount();

while (ic_gr.next()) {  // ---> read the record through ic_gr

var custname_value = ic_gr.getDisplayValue('u_customer_name').toString();
var serNumber_value = ic_gr.getDisplayValue('u_service_number.u_service_identifier').toString();

var index1 = arr1.indexOf(customerName);
var index2 = arr2.indexOf(serviceNumber)
if((index1 > -1) &&(index2 > -1) ){

ic_gr.u_customer_name = arr1[index1];
ic_gr.u_service_number = arr2[index2];
ic_gr.update();  

}

}





Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

13 REPLIES 13

Hi,

I ran this and it worked fine

var e_serNumber = ['MCWPL0290'];
var e_custName = ['GENTING MY TEST'];

var json = '[{"customerName":"CITIBANK BHD","serviceNumber":"TEST"},{"customerName": "GENTING MY TEST","serviceNumber": "MCWPL0290"}]';
var arrayUtil = new global.ArrayUtil();

var jsonObj = JSON.parse(json);
for (var i = 0; i < jsonObj.length; i++) {
	var customerName = jsonObj[i].customerName.toString();
	var serviceNumber = jsonObj[i].serviceNumber.toString();

	if((arrayUtil.contains(e_custName,customerName)) &&(arrayUtil.contains(e_serNumber,serviceNumber)) ){
		gs.info('Inside if for->' + customerName + ' and ' + serviceNumber);
	}

}

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


Script execution history and recovery available here


*** Script: Inside if for->GENTING MY TEST and MCWPL0290

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi Ankur,

Thank  you for your help , the condition is working . But still insert instead of update ! .. 

 

As i already executed the while loop and read the record form u_list , does not update while use the "ic_gr" which is inside the "arrayUtil.contains" condition ? could you please help to fix this ?

 

var ic_gr = new GlideRecord("u_list");
ic_gr.addQuery('parent', incident_sys_id);
ic_gr.query();
var impact_custlist_flag= ic_gr.getRowCount();


while (ic_gr.next()) {  // ---> read the record through ic_gr

var custname_value = ic_gr.getDisplayValue('u_customer_name').toString();
var serNumber_value = ic_gr.getDisplayValue('u_service_number.u_service_identifier').toString();
e_custName.push(custname_value);
e_serNumber.push (serNumber_value);
}


var jsonObj = JSON.parse(impacted_customer_list);

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

var customerName = jsonObj[i].customerName.toString();
var serviceNumber = jsonObj[i].serviceNumber.toString();


if((arrayUtil.contains(e_custName,customerName)) &&(arrayUtil.contains(e_serNumber,serviceNumber)) ){
gs.info('UPDATE INCIDENT - Imapacted list is already available -customerName= '+customerName +' & serviceNumber = ' +serviceNumber);
ic_gr.u_customer_name = customerName;
ic_gr.u_service_number = serviceNumber;
ic_gr.update();  // --> trying to update outside the while loop
}
}

 

find_real_file.png

@Sagaya 

that's because the update is outside the while and hence it won't know which record to update

you should store the values into array when you parse the JSON and then check inside the while if the array contains the value from the record

use this and test

var arr1 = [];
var arr2 = [];

var jsonObj = JSON.parse(impacted_customer_list);

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

var customerName = jsonObj[i].customerName.toString();
var serviceNumber = jsonObj[i].serviceNumber.toString();

arr1.push(customerName);
arr2.push(serviceNumber);

}

var ic_gr = new GlideRecord("u_list");
ic_gr.addQuery('parent', incident_sys_id);
ic_gr.query();
var impact_custlist_flag= ic_gr.getRowCount();

while (ic_gr.next()) {  // ---> read the record through ic_gr

var custname_value = ic_gr.getDisplayValue('u_customer_name').toString();
var serNumber_value = ic_gr.getDisplayValue('u_service_number.u_service_identifier').toString();

var index1 = arr1.indexOf(customerName);
var index2 = arr2.indexOf(serviceNumber)
if((index1 > -1) &&(index2 > -1) ){

ic_gr.u_customer_name = arr1[index1];
ic_gr.u_service_number = arr2[index2];
ic_gr.update();  

}

}





Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi Ankur,

Thank you for your help and support . Issue has resolved