- 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-18-2021 10:00 PM
Hi,
if you are in scoped application then use this
global.ArrayUtil()
also syntax for contains is this 1st parameter is the array and 2nd is the element/value to search
so update as this
if((arrayUtil.contains(customerName,e_custName)) &&(arrayUtil.contains(serviceNumber,e_serNumber)) ){
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 12:46 AM
Hi Ankur,
Thank you for your prompt response and help . Tried with the advised approach and still getting false .
var arrayUtil = new global.ArrayUtil();
if((arrayUtil.contains(customerName,e_custName)) &&(arrayUtil.contains(serviceNumber,e_serNumber)) ){
}
So instead of update , new record has inserted . Could you please help to fix this issue?
Regards,
Sagaya.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-19-2021 12:50 AM
Hi,
Please ignore above message. I though you have not used the correct sequence
Did you print both the arrays and check if it contains that value or not
try few updates I did now in bold
var arrayUtil = new global.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'));
}
gs.info('Customer Array' + e_custName);
gs.info('Service Array' + e_serNumber);
var jsonObj = JSON.parse(c_list);
for (var i = 0; i < jsonObj.length; i++) {
var customerName = jsonObj[i].customerName.toString(); // use toString here
var serviceNumber = jsonObj[i].serviceNumber.toString(); // use toString here
if((arrayUtil.contains(e_custName,customerName,)) &&(arrayUtil.contains(e_serNumber,serviceNumber,)) ){
// Do update
} else{
// Do insert
}
}
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 04:09 AM
Hi Ankur,
Updated the code but still got the same output . Please check the below code and output
Code :
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'));
}
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();
}
}
}
}