How to compare array element in GlideRecord?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-03-2023 10:01 PM
Hi Team,
I am trying to call array element in the GlideRecord,
var caseArray = {
"arr": [{
'number': '0213634',
},
{
'number': '0213536',
},
{
'number': '0213512',
},
{
'number': '0460810',
},
{
'number': '0203475',
},
{
'number': '0203467'
}
]
};
var jsonObject = global.JSON.stringify(caseArray);
var parser = global.JSON.parse(jsonObject);
var arrayObject = parser.arr;
for (var i = 0; i < arrayObject.length; i++) {
var arrayElementObject = global.JSON.stringify(arrayObject[i]);
var arrayElement = global.JSON.parse(arrayElementObject);
var grObject = new GlideRecord('Table_name1');
grObject.addQuery('number', arrayElement.number);
grObject.query();
gs.info("test");
while (grObject.next()) {
var sum = 0;
var durationSum = new GlideAggregate('table_name2');
durationSum.addEncodedQuery('u_emp_number',arrayElement.number);
durationSum.addAggregate('SUM', 'duration');
durationSum.setGroup(false);
durationSum.query();
while (durationSum.next()) {
gs.info("test1");
sum = durationSum.getAggregate('SUM', 'duration');
gs.info("test3"+sum);
if (Math.floor(sum)) {
gs.info("test2"); grObject.setValue('note_duration', parseInt(sum).toFixed(0));
} else {
grObject.setValue('note_duration', '');
}
}
How can i call number in the listed on the top to two GlideRecord tables.
FYI:table1 is parent of table 2
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-03-2023 10:28 PM
HI @sasi ,
I trust you are doing great.
Here's a refined version of your code to achieve this:
var caseArray = {
"arr": [
{'number': '0213634'},
{'number': '0213536'},
{'number': '0213512'},
{'number': '0460810'},
{'number': '0203475'},
{'number': '0203467'}
]
};
// Loop through each number in the array
for (var i = 0; i < caseArray.arr.length; i++) {
var currentNumber = caseArray.arr[i].number;
// Query the parent table (Table_name1) using the current number
var grParent = new GlideRecord('Table_name1');
grParent.addQuery('number', currentNumber);
grParent.query();
while (grParent.next()) {
gs.info("Found record in Table_name1 with number: " + currentNumber);
// Query the child table (table_name2) using the current number
var sum = 0;
var durationSum = new GlideAggregate('table_name2');
durationSum.addEncodedQuery('u_emp_number=' + currentNumber);
durationSum.addAggregate('SUM', 'duration');
durationSum.query();
while (durationSum.next()) {
sum = durationSum.getAggregate('SUM', 'duration');
gs.info("Total duration for number " + currentNumber + ": " + sum);
if (Math.floor(sum)) {
grParent.setValue('note_duration', parseInt(sum).toFixed(0));
} else {
grParent.setValue('note_duration', '');
}
grParent.update(); // Update the parent record with the aggregated value
}
}
}
Was this answer helpful?
Please consider marking it correct or helpful.
Your feedback helps us improve!
Thank you!
Regards,
Amit Gujrathi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-04-2023 12:33 AM
it is not working, it is giving empty value.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-04-2023 12:51 AM
Hi @sasi
Are you getting below values dynamically...?? may be as JSON object ..??
var caseArray = {
"arr": [{
'number': '0213634',
},
{
'number': '0213536',
},
{
'number': '0213512',
},
{
'number': '0460810',
},
{
'number': '0203475',
},
{
'number': '0203467'
}
]
};
ServiceNow Developer
I know one thing, and that is that I know nothing.
- Socrates