Thursday, 14 November 2013

ROLLUP Using SOQL With Lookup Relationship

To get the Count of Child Records for the Particular Parent Record for the two different objects with (Master-Detail Relationship) - just the Standard Rollup Feature is Enough.

What if, we needs to achieve it for a Lookup-Relationship.
Definitely one should opt for Complicated Custom Apex Code to achieve it ,but we can do that just by using some simple SOQL function to get the Child count for the Particular Parent Record.

SOQL Function  :  GROUP BY ROLLUP

Example Code:

Parent : Account
Child : Opportunity

I am going to get the Number of Opportunity Records for the Particular Account Record.

  1. SELECT AccountId, COUNT(Name) NoOfCount FROM Opportunity GROUP BY ROLLUP(AccountId)


Result:



No comments:

Post a Comment