public with sharing class inspectionsDashboard {
public List<AggregateResult> Result {get;set;}
public List<Summary> SummaryList {get;set;}
public List<Account> AcctList;AcctList {get;set;}
public Map<Id, Account> IdAccount;IdAccount {get;set;}
List<Id> Ids;Ids {get;set;}
private String soql {get;set;}
public String sortDir {
get { if (sortDir == null) { sortDir = 'asc'; } return sortDir; }
set;
}
// the current field to sort by. defaults to last name
public String sortField {
get { if (sortField == null) {sortField = 'Name'; } return sortField; }
set;
}
// format the soql for display on the visualforce page
public String debugSoql {
get { return soql + ' order by ' + sortField + ' ' + sortDir + ' limit 20'; }
set;
}
public void showtoggleSort() {
// simply toggle the direction
sortDir = sortDir.equals('asc') ? 'desc' : 'asc';
// run the query again
runQuery();
}
public inspectionsDashboard() {
SummaryList = new List<Summary>();
Result = new List<AggregateResult>();
Ids = new List<Id>();
AcctList = new List<Account>();
IdAccount = new Map<Id, Account>();
soql = 'SELECT Name,BillingState FROM Account WHERE Id IN : Ids';
Result = [SELECT Count(Inspection_Date__c), Count(Franchise_Billing_State__c), Count(Id) Total, Sum(Home_Inspection_dollars__c) Dollars, Franchise__c
FROM Inspection__c
WHERE Month_Since_Launch__c > 0 AND Status__c = 'Completed'
GROUP BY Franchise__c];
for(AggregateResult a : Result) {
Ids.add((Id)a.get('Franchise__c'));
}
AcctList = [SELECT Name FROM Account WHERE Id IN : Ids];runQuery();
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('Franchise__c')));
system.debug('Account Name is ' + TempAcct.Name);
SummaryList.add(new Summary(a, TempAcct.Name, TempAcct.BillingState));
}
}
public void runQuery() {
try {
AcctList = Database.query(soql + ' order by ' + sortField + ' ' + sortDir + ' limit 20');
} catch (Exception e) {
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Ooops!'));
}
}
public class Summary {
public Integer Total {get;set;}
public Decimal Dollars {get;set;}
public String AcctName {get;set;}
public String StateProvince {get;set;}
public Summary(AggregateResult a, String AccountName, String State) {
Total = (Integer)a.get('Total');
Dollars = (Decimal)a.get('Dollars');
AcctName = AccountName;
StateProvince = State;
}
}
public PageReference runSearch() {
String AccountName = Apexpages.currentPage().getParameters().get('AccountName');
String State = Apexpages.currentPage().getParameters().get('State');
soql = 'SELECT Name,BillingState FROM Account WHERE Id IN : Ids';
List<String> li = new List<String>();
if (!AccountName.equals(''))
li.add(' Name LIKE \''+String.escapeSingleQuotes(AccountName)+'%\'');
if (!State.equals(''))
li.add(' BillingState LIKE \''+String.escapeSingleQuotes(State)+'%\'');
String c = String.join( li, ' or ');
if (c.length() > 0)
c = ' and (' + c + ')';
system.debug('condition: ' + c);
soql += c;
// run the query again
runQuery();
return null;
}
<apex:page controller="inspectionsDashboard" sidebar="false"sidebar="false">
<apex:form>
<apex:pageMessages id="errors" />
<apex:pageBlock title="Find Me A Franchisee!" mode="edit">
<table width="100%" border="0">
<tr>
<td width="200" valign="top">
<apex:pageBlock title="Parameters" mode="edit" id="criteria">
<script type="text/javascript">
function doSearch() {
searchServer(
document.getElementById("AccountName").value,
document.getElementById("State").value
);
}
</script>
<apex:actionFunction name="searchServer" action="{!showrunSearch}">" rerender="results,debug,errors">
<apex:pagemessagesparam name="AccountName" value="" />
<apex:formparam name="State" value="" />
<apex </apex:pageBlockactionFunction>
<table cellpadding="2" cellspacing="2">
<tr>
<td style="font-weight:bold;">Account Name<br/>
<input type="text" id="AccountName" onkeyup="doSearch();"/>
</td>
</tr>
<tr>
<td style="font-weight:bold;">State/Province<br/>
<input type="text" id="State" onkeyup="doSearch();"/>
</td>
</tr>
</table>
</apex:pageBlock>
</td>
<apex:pageBlock >
<apex:pageblockTable value="{!SummaryList}" var="r">
<apex:column >
<apex:columnfacet headerValue="Accountname="header">
<apex:commandLink value="Account Name" action="{!toggleSort}" rerender="results,debug">
<apex:param name="sortField" value="AccountName" assignTo="{!sortField}"/>
</apex:commandLink>
</apex:facet>
<apex:outputText value="{!r.AcctName}"/>
</apex:column>
<apex:column >
<apex:facet name="header">
<apex:commandLink value="State/Province" action="{!toggleSort}" rerender="results,debug">
<apex:param name="sortField" value="State" assignTo="{!sortField}"/>
</apex:commandLink>
</apex:facet>
<apex:outputText value="{!r.StateProvince}"/>
</apex:column>
<apex:column headerValue="Number of Home Inspections" value="{!r.Total}" id="Total"/>
<apex:column headerValue="Home Inspection $" value="{!r.Dollars}" id="Dollars"/>
</apex:pageblockTable>
<apex:pageBlock title="Debug - SOQL" id="debug">
<apex:outputText value="{!debugSoql}" />
</apex:pageBlock>
</apex:form>pageBlock>
</tr>
</table>
</apex:page>pageBlock>
</apex:form>
</apex:page>