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

Help APEX: Count Number of Notes in Parent Object

Hi All,

Please bear with me since I am a complete newbie with Apex. I need assistance building APEX to count the number of Notes (Standard Object) in Parent object (Custom Object).

I would appreciate if you can share your code.
Vijay Kumar Rebbala 11Vijay Kumar Rebbala 11
public class Testctrl {
    public Integer notecount{get;set;}
    public Testctrl(){
        notecount = [SELECT Count() From Note WHERE ParentId =: ApexPages.currentPage().getParameters().get('id') ];
Shalagha GumberShalagha Gumber
You can try this code:

AggregateResult ar=[SELECT Count(Id) num, ParentId FROM Note WHERE ParentId=:recordId GROUP BY ParentId].get(0);
system.debug('Number of notes on parent record is '+ar.get('num'));

Here, recordId is the Id of the custom object record.
Developer Console > Query Editor >
SELECT count() FROM Note WHERE ParentID IN (SELECT id FROM Account)
Change the parent object accordingly. This might result 'No response from server' when you hit query limit.