The CreatorCon Call for Content is officially open! Get started here.

Glideaggregate object returning empty values

Tapish Sharma1
Kilo Sage

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

}

}

1 ACCEPTED SOLUTION

Mohith Devatte
Tera Sage
Tera Sage

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

 

 

View solution in original post

7 REPLIES 7

Chetan Mahajan
Kilo Sage

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

Hi Chetan , Thanks for your reply , but this script gives the same result , empty sys_id

 

 

 

Mohith Devatte
Tera Sage
Tera Sage

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

 

 

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 !