AggregateResult in Salesforce is used to find Sum, Min, Max and Avg. It is similar to Aggregate function in SQL.
Using Aggregate Result, we cannot fetch data. It is mainly used to find Sum, Min, Max and Avg.
Sample Code:
Visualforce page:
<apex:page controller="Sample" sidebar="false" action="{!show}">
<apex:pagemessages />
<apex:form >
<apex:pageBlock >
<apex:pageblockTable value="{!SummaryList}" var="r">
<apex:column headerValue="Account Name" value="{!r.AcctName}"/>
<apex:column headerValue="Number of Contacts" value="{!r.Total}"/>
</apex:pageblockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
Apex Controller:
public with sharing class Sample {
public List<AggregateResult> Result {get;set;}
public List<Summary> SummaryList {get;set;}
public List<Account> AcctList;
public Map<Id, Account> IdAccount;
List<Id> Ids;
public void show() {
SummaryList = new List<Summary>();
Result = new List<AggregateResult>();
Ids = new List<Id>();
AcctList = new List<Account>();
IdAccount = new Map<Id, Account>();
Result = [SELECT Count(Id) Total , AccountId FROM Contact GROUP BY AccountId];
for(AggregateResult a : Result) {
Ids.add((Id)a.get('AccountId'));
}
AcctList = [SELECT Name FROM Account WHERE Id IN : Ids];
System.debug('Account List' + AcctList);
for(Account a : AcctList) {
IdAccount.put(a.Id, a);
}
System.debug('Ids and Accounts are ' + IdAccount);
for(AggregateResult a : Result) {
Account TempAcct = new Account();
TempAcct = IdAccount.get((Id)(a.get('AccountId')));
system.debug('Account Name is ' + TempAcct.Name);
SummaryList.add(new Summary(a, TempAcct.Name));
}
}
public class Summary {
public Integer Total {get;set;}
public String AcctName {get;set;}
public Summary(AggregateResult a, String AccountName) {
Total = (Integer)a.get('Total');
AcctName = AccountName;
}
}
}
Using Aggregate Result, we cannot fetch data. It is mainly used to find Sum, Min, Max and Avg.
Sample Code:
Visualforce page:
<apex:page controller="Sample" sidebar="false" action="{!show}">
<apex:pagemessages />
<apex:form >
<apex:pageBlock >
<apex:pageblockTable value="{!SummaryList}" var="r">
<apex:column headerValue="Account Name" value="{!r.AcctName}"/>
<apex:column headerValue="Number of Contacts" value="{!r.Total}"/>
</apex:pageblockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
Apex Controller:
public with sharing class Sample {
public List<AggregateResult> Result {get;set;}
public List<Summary> SummaryList {get;set;}
public List<Account> AcctList;
public Map<Id, Account> IdAccount;
List<Id> Ids;
public void show() {
SummaryList = new List<Summary>();
Result = new List<AggregateResult>();
Ids = new List<Id>();
AcctList = new List<Account>();
IdAccount = new Map<Id, Account>();
Result = [SELECT Count(Id) Total , AccountId FROM Contact GROUP BY AccountId];
for(AggregateResult a : Result) {
Ids.add((Id)a.get('AccountId'));
}
AcctList = [SELECT Name FROM Account WHERE Id IN : Ids];
System.debug('Account List' + AcctList);
for(Account a : AcctList) {
IdAccount.put(a.Id, a);
}
System.debug('Ids and Accounts are ' + IdAccount);
for(AggregateResult a : Result) {
Account TempAcct = new Account();
TempAcct = IdAccount.get((Id)(a.get('AccountId')));
system.debug('Account Name is ' + TempAcct.Name);
SummaryList.add(new Summary(a, TempAcct.Name));
}
}
public class Summary {
public Integer Total {get;set;}
public String AcctName {get;set;}
public Summary(AggregateResult a, String AccountName) {
Total = (Integer)a.get('Total');
AcctName = AccountName;
}
}
}
output:
Thanks for posting these kinds of post its very helpful and very good content a really appreciable post apart from that if anyone looking for Python training institute in delhi so contact here +91-9311002620 visit https://www.htsindia.com/Courses/python/python-training-institute-in-south-delhi
ReplyDeleteYour post is really good thanks for sharing this kind of content i hope you will share these kinds of stuff in near future apart from that if anyone is looking for a training institute for C++ training.Contact Here-+91-9311002620 Or Visit post Website-
ReplyDeletehttps://www.htsindia.com/Courses/modular-courses/c-plus-plus-training-course
A big thank you for sharing this post its really awesome apart from that if anyone looking for e accounting institute in delhi so Contact Here-+91-9311002620 Or Visit Website- https://www.htsindia.com/Courses/Tally/e-accounting-training-course
ReplyDeleteA very big thank you for sharing this post apart from that if anyone looking for best Advanced Excel training institute in delhi so contact here +91-9311002620 visit https://www.htsindia.com/Courses/business-analytics/adv-excel-training-course
ReplyDeleteA very big thank you for sharing this post apart from that if anyone looking for best Advanced Excel training institute in delhi so contact here +91-9311002620 visit https://www.htsindia.com/Courses/business-analytics/adv-excel-training-course
ReplyDeleteThanks for sharing this amazing post this is the content i really looking for, its very helpful i hope you will continue your blogging anyway if anyone looking for C++ training institute in delhi contact us +91-9311002620 visit-https://www.htsindia.com/Courses/modular-courses/c-plus-plus-training-course
ReplyDeleteThanks for sharing this content its really a great post and very helpful thanks for sharing this knowledgeable content and if anyone looking for best tally institute in delhi so contact here +91-9311002620 visit https://www.htsindia.com/Courses/tally/tally-training-course
ReplyDeleteI really appreciate your hard work you put into your blog and detailed information you provide. Further More Information About C++ training institute in Delhi Contact Here-+91-9311002620 Or Visit Website- https://www.htsindia.com/Courses/modular-courses/c-plus-plus-training-course
ReplyDeleteI really appreciate your hard work you put into your blog and detailed information you provide. Further More Information About MIS training institute in Delhi Contact Here-+91-9311002620 Or Visit Website- https://www.htsindia.com/Courses/business-analytics/mis-training-instiute-in-delhi
ReplyDeleteCoin Casino | No Deposit Bonus Codes | December 2021
ReplyDeleteIf you do have หารายได้เสริม a gambling problem, worrione please help us resolve the problem by 인카지노 contacting the casino with the best offer in our online casino reviews.
pendik samsung klima servisi
ReplyDeleteataşehir mitsubishi klima servisi
maltepe vestel klima servisi
kadıköy vestel klima servisi
maltepe bosch klima servisi
kadıköy bosch klima servisi
kadıköy arçelik klima servisi
pendik mitsubishi klima servisi
tuzla toshiba klima servisi