Activity Forums Salesforce® Discussions How can write the Aggregate query and display the result of Aggregate query in Salesforce ?

  • How can write the Aggregate query and display the result of Aggregate query in Salesforce ?

    Posted by Arun on January 16, 2020 at 1:45 pm

    How can write the Aggregate query to get the sum of a field (Assume field datatype is Number or Currency)  and How can display the result of Aggregate Query in Salesforce apex ?

    • This discussion was modified 4 years, 2 months ago by  Arun.
    Deepanshu Singh replied 4 years, 2 months ago 2 Members · 1 Reply
  • 1 Reply
  • Deepanshu Singh

    Member
    January 18, 2020 at 5:24 pm

    For the Aggregate Query you have AggregateResult class in apex

    Example

    for(AggregateResult ar : [select count(Id), AccountId
                              from Contact 
                              where AccountId in :accset
                              group by AccountId
                              having count(Id)>2 ])
    {
        contList.add(new Contact(Id = (ID)ar.get('AccountId')));
    }
    try{
        update contList;
    }catch(DMLException exc){
        system.debug("Exception => " + exc.getMessage());
    }

     

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos