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,

you didn't share what came in logs for that arrays and json

var cust_list = [];
var custDtl = {};
var arrayUtil = new global.ArrayUtil();
var e_custName = [];
var e_serNumber =[];

var ic_gr = new GlideRecord("c_list");
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'));
}


gs.info('e_custName array' + e_custName.toString());
gs.info('e_serNumber array' + e_serNumber.toString());

gs.info('JSON is' + impacted_customer_list);

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

    if((arrayUtil.contains(e_custName,customerName)) &&(arrayUtil.contains(e_serNumber,serviceNumber)) ){
        ic_gr.u_customer_name = account_sysid;
        ic_gr.u_service_number = installbase_sysid;
        ic_gr.update();

    } else {
        ic_gr.initialize();
        ic_gr.setValue('u_customer_name', account_sysid);
        ic_gr.setValue('u_service_number', installbase_sysid);
        ic_gr.insert();
    }
}

Regards
Ankur

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

HI Ankur,

Please find the below log. JSON has 2 entry with different name but array has the same name with 2 times.

e_serNumber arrayMCWPL0290,MCWPL0290

e_custName arrayGENTING MY TEST,GENTING MY TEST

JSON is[{"customerName":"CITIBANK BHD","serviceNumber":"TEST"},{"customerName": "GENTING MY TEST","serviceNumber": "MCWPL0290"}]

 

Regards,

Sagaya.

Hi,

So definitely array doesn't any of the exact json values so it would go in the else condition

Also are you sure the while loop where you are pushing the array is iterating correct number of times as it is pushing the same value again

can you try to add unique for array

var cust_list = [];
var custDtl = {};
var arrayUtil = new global.ArrayUtil();
var e_custName = [];
var e_serNumber =[];

var ic_gr = new GlideRecord("c_list");
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'));
}

e_custName = new global.ArrayUtil().unique(e_custName);

e_serNumber = new global.ArrayUtil().unique(e_serNumber);

gs.info('e_custName array' + e_custName.toString());
gs.info('e_serNumber array' + e_serNumber.toString());

gs.info('JSON is' + impacted_customer_list);

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

    if((arrayUtil.contains(e_custName,customerName)) &&(arrayUtil.contains(e_serNumber,serviceNumber)) ){
        ic_gr.u_customer_name = account_sysid;
        ic_gr.u_service_number = installbase_sysid;
        ic_gr.update();

    } else {
        ic_gr.initialize();
        ic_gr.setValue('u_customer_name', account_sysid);
        ic_gr.setValue('u_service_number', installbase_sysid);
        ic_gr.insert();
    }
}

Regards
Ankur

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

HI Ankur,

After include unique , getting 

e_serNumber arrayMCWPL0290

e_custName arrayGENTING MY TEST

JSON is[{"customerName":"CITIBANK BHD","serviceNumber":"TEST"},{"customerName": "GENTING MY TEST","serviceNumber": "MCWPL0290"}]

 

So the below condition should be true for "customerName": "GENTING MY TEST","serviceNumber": "MCWPL0290" as both array and json has the same value . But the condition has failed !.

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

}

Regards

Sagaya

 

 

Hi,

Can you try running this in background script

did you check any space is present in the values

try this to remove the spaces

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

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

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

Regards
Ankur

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