0

I have the following code but the aggregate-result doesn't work. I pretty much want to return back the number of active users in the system. How do I return the Count(*) of all the active users? Class

public with sharing class ActiveAgents {

public List<User> ActiveUsers {get; private set;}
public List<User> NumberOfAgents {get; private set;}
public  AgentsNumber[] AgentsNumbers;
   public Integer Quantity { get; private set; }
public ActiveAgents() {
   // User U = new User();
    ActiveUsers = [SELECT Name , Email, LastName , UserRole.Name , Profile.Name,
                        Phone , Extension 
                 FROM User
                 WHERE IsActive = true];  AggregateResult[] agentListAggregated = [Select count(Id) Quantity
                                       FROM User
                                       WHERE IsActive = true ];       AgentsNumbers = new List<AgentsNumber>();       for (AggregateResult ar :

agentListAggregated){

           AgentsNumbers.add( new AgentsNumber(ar));

      }


}
 public class AgentsNumber {

        public Product_Lookup__c(AggregateResult ar) {
        Quantity = (Integer) ar.get('Quantity');

    }
}
 }

VF

          <table width="100%" border="0" height="200px">
              <apex:repeat value="{!AgentsNumbers}" var="AgentsNumber">
                  <apex:outputText value="{!AgentsNumber.Quantity}" />
              </apex:repeat>
              <apex:repeat value="{!ActiveUsers}" var="activeUser">

                       <thead>
                                <tr>
                                    <th colspan="5">
                                      <apex:pageBlockSectionItem>
                                          <h2>AgentRole:</h2>
                                          <apex:outputText value="{!activeUser.UserRole.Name}" />
                                      </apex:pageBlockSectionItem>
                                  </th>
                                </tr>
                        </thead>
                      <apex:pageBlockSectionItem>
                          <h2>FirstName:</h2>
                           <apex:outputText value="{!activeUser.Name}" />
                      </apex:pageBlockSectionItem>
                      <apex:pageBlockSectionItem>
                          <h2>LastName:</h2>
                           <apex:outputText value="{!activeUser.LastName}" /> 
                      </apex:pageBlockSectionItem>
                      <apex:pageBlockSectionItem>
                          <h2>Email</h2>
                           <apex:outputText value="{!activeUser.Email}" />
                      </apex:pageBlockSectionItem>

                      <apex:pageBlockSectionItem>
                          <h2>ProfileName:</h2>
                           <apex:outputText value="{!activeUser.Profile.Name}" />
                      </apex:pageBlockSectionItem>
                      <apex:pageBlockSectionItem>
                          <h2>PhoneNumber:</h2>
                           <apex:outputText value="{!activeUser.Phone}" />
                      </apex:pageBlockSectionItem>
                      <apex:pageBlockSectionItem>
                          <h2>Extension:</h2>
                           <apex:outputText value="{!activeUser.Extension}" />
                      </apex:pageBlockSectionItem>


                 <br/>
                 <hr/>
                </apex:repeat>
          </table>
                   <style>    hr { 
  display: block;
  margin-top: 0.5em;
  margin-bottom: 0.5em;
  margin-left: auto;
  margin-right: auto;
  border-style: inset;
  border-width: 2px;
  width:100%;
  background-color: red;  }  </style>             <!-- <apex:outputpanel>
                    <table width="100%" border="0" height="200px">
                        <thead>
                                <tr>
                                    <th colspan="2">Filters</th>
                                </tr>
                        </thead>
                        <tr>
                            <td colspan="2"><hr /></td>
                        </tr>
                    </table>
            </apex:outputpanel> -->
              </apex:pageBlockSection>    </apex:pageBlock>

1 Answer 1

0

Here is an example of getting the value out of AggregatedResult

List<AggregateResult> aggrResult = [select count(id) activeUsers from user where IsActive = true];
Integer activeUsers = (Integer) aggrResult.get(0).get('activeUsers');

System.debug(activeUsers);
8
  • hey, thanks for the respond, how do I get that in the VF page?
    – Metawaa
    Commented Feb 26, 2015 at 22:29
  • Also why do you have activeUser in [select count(id) activeUsers ..] here
    – Metawaa
    Commented Feb 26, 2015 at 22:30
  • Create a Controller field, perhaps like this public Integer activeUsersCount {get;set;} and assign that count. Than later you can refer the field activeUsersCount in your VF page as {!activeUsersCount} Commented Feb 26, 2015 at 22:30
  • That is called field aliasing that allows to specify a friendly label for the aggregated result. See this [salesforce.com/us/developer/docs/dbcom_soql_sosl/Content/… Commented Feb 26, 2015 at 22:32
  • thank you, I think I may have done it wrong, that I get so many errors here
    – Metawaa
    Commented Feb 26, 2015 at 23:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .