- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-18-2021 09:22 PM
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
}
Solved! Go to Solution.
- Labels:
-
Integrations
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2021 08:33 AM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-19-2021 06:17 AM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-20-2021 09:51 PM
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
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2021 08:33 AM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-22-2021 06:11 PM
Hi Ankur,
Thank you for your help and support . Issue has resolved