- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-11-2023 02:28 AM
Hi Folks,
My problem statement is -
If there is just one contract associated with the account I need to return that contract value , else return null.
I am trying to use Glideaggregate to count the number of records but unable to return the record sysid.
Below is my code snippet -
var ga = new GlideAggregate('ast_contract');
ga.addAggregate('COUNT');
ga.addQuery('account',sysIDofAccount);
ga.query();
if(ga.next()){
var count = ga.getAggregate('COUNT');
if(count==1){
return ga.sys_id; // Gives empty value
}
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-11-2023 02:50 AM
Hello @Tapish Sharma1 ,
Usually in GlideAggregate API field values cannot be accessed unless there are used in orderBy or sorted.You can only get count of the records with above script
try below script to get the sys_id
var ga = new GlideAggregate('ast_contract');
ga.orderBy('sys_id');
ga.addAggregate('COUNT');
ga.addQuery('account',sysIDofAccount);
ga.query();
if(ga.next()){
var count = ga.getAggregate('COUNT');
if(count==1){
return ga.sys_id; // Gives empty value
}
}
Hope this helps
Mark my answer correct if this helps you
Thanks

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-11-2023 02:45 AM
Hello @Tapish Sharma1 ,
you can try below script, there is some correction to retrieve sys_id
var ga = new GlideAggregate('ast_contract');
ga.addAggregate('COUNT');
ga.addQuery('account', sysIDofAccount);
ga.query();
if (ga.next()) {
var count = ga.getAggregate('COUNT');
if (count == 1) {
var contractSysId = ga.getValue('sys_id');
return contractSysId;
}
}
return null; // Return null if there's no contract or more than one contract
Kindly mark correct and helpful if applicable
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-11-2023 02:50 AM
Hi Chetan , Thanks for your reply , but this script gives the same result , empty sys_id
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-11-2023 02:50 AM
Hello @Tapish Sharma1 ,
Usually in GlideAggregate API field values cannot be accessed unless there are used in orderBy or sorted.You can only get count of the records with above script
try below script to get the sys_id
var ga = new GlideAggregate('ast_contract');
ga.orderBy('sys_id');
ga.addAggregate('COUNT');
ga.addQuery('account',sysIDofAccount);
ga.query();
if(ga.next()){
var count = ga.getAggregate('COUNT');
if(count==1){
return ga.sys_id; // Gives empty value
}
}
Hope this helps
Mark my answer correct if this helps you
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-11-2023 02:54 AM
Thanks for your reply
That is what I was thinking , getRowCount() can be used if we want to count the records and do something with it as an exception over Glideaggregate . Correct me if wrong !