function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sangam vvsangam vv 

Guys i stuck in query

How to count activities drom query


example:Hp is a account under this i did 30 activity in visual force page i want to see
account        activities
Hp     -          30


iin table format how to do pls help me?
Best Answer chosen by sangam vv
mritzimritzi
Try this and let me know, if it works

Apex:
 
public class TestClass2 {
    public List<Account> acct{get; set;}
    //stores value from 0 to n, used to iterate over two lists in VF
    public List<Integer> itr{get;set;}
    //stores no of activities(tasks+events) for each account
    public List<integer> count{get;set;}
    
    public TestClass2(){
        acct=new List<Account>();
        count = new List<Integer>();
        itr = new List<Integer>();
        acct=[select id,name from account ORDER BY Name];
        
        getActivities();
        
    }
    public void getActivities(){
        List<Id> ids = new List<Id>();
        List<AggregateResult> arTask= new List<AggregateResult>();
        List<AggregateResult> arEvent= new List<AggregateResult>();
        
        
        Integer i=0;
        //ids List stores ids of all accounts
        for(Account a : acct){
            ids.add(a.id);
            itr.add(i++);
        }
        
        arTask = [Select Count(id), AccountId FROM Task Where AccountId IN:ids GROUP BY AccountId];
        arEvent = [Select Count(id),AccountId FROM Event Where AccountId IN:ids GROUP BY AccountId];
        
        Integer sum;
        for(i=0;i<itr.size();i++){
            //expr0 holds first expression of Soql i.e Count(id)
            for(AggregateResult ar : arTask){
                if(acct[i].id==ar.get('AccountId')){
                    sum=Integer.valueOf(ar.get('expr0'));
                    break;
                }
                else
                    sum=0;
            }
            
            for(AggregateResult ar :arEvent){
                if(acct[i].id==ar.get('AccountId')){
                    sum+=Integer.valueOf(ar.get('expr0'));
                    break;
                }
                else
                    sum+=0;
            }
                        
            count.add(sum);
        }
    }
}

 

All Answers

mritzimritzi
Have a look at the following link. It Talks about Number of contacts associated with Account.
Change it as per your requirement.

http://www.infallibletechie.com/2013/07/aggregateresult-in-salesforce.html

Do let me know, if you get moving with it.
mritzimritzi
Here is the code that can possibly help you out:

Apex
public class TestClass2 {
    public List<Account> acct{get; set;}
    //stores value from 0 to n, used to iterate over two lists in VF
    public List<Integer> itr{get;set;}
    //stores no of activities(tasks+events) for each account
    public List<integer> count{get;set;}
    
    public TestClass2(){
        acct=new List<Account>();
        count = new List<Integer>();
        itr = new List<Integer>();
        acct=[select id,name from account ORDER BY Name];
        
        getActivities();
        
    }
    public void getActivities(){
        List<Id> ids = new List<Id>();
        List<AggregateResult> arTask= new List<AggregateResult>();
        List<AggregateResult> arEvent= new List<AggregateResult>();
        Integer i=0;
        for(Account a : acct){
            ids.add(a.id);
            itr.add(i++);
        }
        
        arTask = [Select Count(id), WhoId FROM Task Where WhoId IN:ids GROUP BY WhoId];
        arEvent = [Select Count(id),WhoId FROM Event Where WhoId IN:ids GROUP BY WhoId];
                
        Integer sum;
        for(i=0;i<itr.size();i++){
            //expr0 holds first expression of Soql i.e Count(id)
            if(i<arTask.size())
                 sum=Integer.valueOf(arTask[i].get('expr0'));
            else
                sum=0;
            if(i<arEvent.size())
                sum+=Integer.valueOf(arEvent[i].get('expr0'));
            else
                sum+=0;
            count.add(sum);
        }
    }
}


VF
<apex:page id="pgId" controller="TestClass2" showheader="false">
    <Apex:form >
        <apex:pageBlock >
            <Apex:pageblockSection title="Accounts">
                <apex:pageBlockTable value="{!itr}" var="i">
                    <Apex:column headervalue="Account Name">
                        <Apex:outputPanel >
                            <Apex:outputText value="{!acct[i].Name}" />
                        </apex:outputPanel>
                    </apex:column>
                    <Apex:column headervalue="No of Activities">
                        {!count[i]}
                    </apex:column>
                </apex:pageBlockTable>
            </Apex:pageblockSection>
        </apex:pageBlock>
       

    </apex:form>
</apex:page>



Hit LIKE, if this helps.
sangam vvsangam vv
@ mritzi i am not getting still i am getting 0
User-added image
sangam vvsangam vv
Guys now i am gettin value in only 1 account eg: if i add task in hp account that will be updating in Burlington Textiles Corp of America accontUser-added image
mritzimritzi
Try this and let me know, if it works

Apex:
 
public class TestClass2 {
    public List<Account> acct{get; set;}
    //stores value from 0 to n, used to iterate over two lists in VF
    public List<Integer> itr{get;set;}
    //stores no of activities(tasks+events) for each account
    public List<integer> count{get;set;}
    
    public TestClass2(){
        acct=new List<Account>();
        count = new List<Integer>();
        itr = new List<Integer>();
        acct=[select id,name from account ORDER BY Name];
        
        getActivities();
        
    }
    public void getActivities(){
        List<Id> ids = new List<Id>();
        List<AggregateResult> arTask= new List<AggregateResult>();
        List<AggregateResult> arEvent= new List<AggregateResult>();
        
        
        Integer i=0;
        //ids List stores ids of all accounts
        for(Account a : acct){
            ids.add(a.id);
            itr.add(i++);
        }
        
        arTask = [Select Count(id), AccountId FROM Task Where AccountId IN:ids GROUP BY AccountId];
        arEvent = [Select Count(id),AccountId FROM Event Where AccountId IN:ids GROUP BY AccountId];
        
        Integer sum;
        for(i=0;i<itr.size();i++){
            //expr0 holds first expression of Soql i.e Count(id)
            for(AggregateResult ar : arTask){
                if(acct[i].id==ar.get('AccountId')){
                    sum=Integer.valueOf(ar.get('expr0'));
                    break;
                }
                else
                    sum=0;
            }
            
            for(AggregateResult ar :arEvent){
                if(acct[i].id==ar.get('AccountId')){
                    sum+=Integer.valueOf(ar.get('expr0'));
                    break;
                }
                else
                    sum+=0;
            }
                        
            count.add(sum);
        }
    }
}

 
This was selected as the best answer
sangam vvsangam vv
thank you Rizwan.I got OutPut.
sangam vvsangam vv
VF page:
<apex:page id="pgId" controller="TestClass2" showheader="false">
    <Apex:form >
        <apex:pageBlock >
            <Apex:pageblockSection title="Accounts">
                <apex:pageBlockTable value="{!itr}" var="i">
                    <Apex:column headervalue="Account Name">
                        <Apex:outputPanel >
                            <Apex:outputText value="{!acct[i].Name}" />
                        </apex:outputPanel>
                    </apex:column>
                    <Apex:column headervalue="No of Activities">
                        {!count[i]}
                    </apex:column>
                </apex:pageBlockTable>
            </Apex:pageblockSection>
        </apex:pageBlock>
       

    </apex:form>
</apex:page>
 
Apex class:

public class TestClass2 {
    public List<Account> acct{get; set;}
    //stores value from 0 to n, used to iterate over two lists in VF
    public List<Integer> itr{get;set;}
    //stores no of activities(tasks+events) for each account
    public List<integer> count{get;set;}
    
    public TestClass2(){
        acct=new List<Account>();
        count = new List<Integer>();
        itr = new List<Integer>();
        acct=[select id,name from account ORDER BY Name];
        
        getActivities();
        
    }
    public void getActivities(){
        List<Id> ids = new List<Id>();
        List<AggregateResult> arTask= new List<AggregateResult>();
        List<AggregateResult> arEvent= new List<AggregateResult>();
        
        
        Integer i=0;
        //ids List stores ids of all accounts
        for(Account a : acct){
            ids.add(a.id);
            itr.add(i++);
        }
        
        arTask = [Select Count(id), AccountId FROM Task Where AccountId IN:ids GROUP BY AccountId];
        arEvent = [Select Count(id),AccountId FROM Event Where AccountId IN:ids GROUP BY AccountId];
        
        Integer sum;
        for(i=0;i<itr.size();i++){
            //expr0 holds first expression of Soql i.e Count(id)
            for(AggregateResult ar : arTask){
                if(acct[i].id==ar.get('AccountId')){
                    sum=Integer.valueOf(ar.get('expr0'));
                    break;
                }
                else
                    sum=0;
            }
            
            for(AggregateResult ar :arEvent){
                if(acct[i].id==ar.get('AccountId')){
                    sum+=Integer.valueOf(ar.get('expr0'));
                    break;
                }
                else
                    sum+=0;
            }
                        
            count.add(sum);
        }
    }
}

This code is working Fine..Thank  You Guys @mritzi