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
Nagma KhanNagma Khan 

how to write a trigger on Opportunity object to count the no of notes and write it to a custom field.

hi
i am new so pelae help me about that trigger


Thanks
Nagma
Prithviraj_ChavanPrithviraj_Chavan
Hi,
can you explain a bit more??
Akshay_DhimanAkshay_Dhiman
Hi Nagma,

Try the code below it will help you.

Trigger:
trigger TriggerCountNote on Note (after insert) {
TriggerClassOnNote.Demo(trigger.new);
}

Trigger Class:
 
public class TriggerClassOnNote {
public static void Demo(list<Note> Notelist)
{
    list<Opportunity> updateOppList=new list<Opportunity>();
    list<Note> ntlist=new list<Note>();
    map<ID,list<ID>> mapopp=new map<ID,list<ID>>();
  	set<ID> oppId=new set<ID>();
    list<ID> NoteId =new list<ID>();
    
    for(Note n : Notelist)
    {
        oppId.add(n.ParentId);
    }
    
    if(oppId.size()>0)
    {
    ntlist=[ SELECT id , ParentId from Note where ParentId IN: oppId ];
    }
    
    if(ntlist.size()>0)
    {
        for(Note n1 : ntlist)
        {
            if(mapopp.get(n1.ParentId) == null)
            {
                mapopp.put(n1.ParentId,new list<ID>());
            }
            mapopp.get(n1.ParentId).add(n1.Id);
        }
    }
    else
    {
        for(Note n1 : Notelist)
        {
            if(mapopp.get(n1.ParentId) == null)
            {
                mapopp.put(n1.ParentId,new list<ID>());
            }
            mapopp.get(n1.ParentId).add(n1.Id);
        }
    }
    
    list<Opportunity> opplist=[ SELECT Total_Note__c from Opportunity WHERE Id IN: mapopp.keyset() ];
    
    for(Opportunity opp : opplist)
    {
       
       		NoteId=mapopp.get(opp.id);
            opp.Total_Note__c=NoteId.size();
        updateOppList.add(opp);
      
    }
    update updateOppList;
}
}

Note:- As Total_Note__c is a custom field in Opportunity and if someone enters manually a wrong value in the field then don't you worry because when the enter a note
the trigger will be fired and it will count the number of notes and  auto-corrected the value.

Regards,
Akshay
Please mark my answer as a solution if it was helpful so it is available to others as a proper solution.
Meggan Landis 11Meggan Landis 11
Hi Akshay, I know this post is very old, but I'm hoping you may be able to help. This post describes the exact requirement of what I am trying to do: When a new note is added, I want to count the total number of notes associated to an opportunity and populate that value in a custom (number) field on the opportunity. I created an Apex Trigger on Note and an Apex Class using the exact code you provided below and created a custom field exactly as you have it above (Total_Note__c) and it is not populating the custom field with a value. Do you have any suggestions on how I may change the code you provided to make this work? Thank you!

Meg