+ Start a Discussion
Phuc Nguyen 18Phuc Nguyen 18 

Apex Class to Update Parent when child record created

Hello all,
I have a trigger to update a field on the Account when a note is created but I having trouble converting my trigger into a Class.
trigger UpdateAccWithLastNote on Note (before insert) {

  /*  List<Account> actLstToUpdate=new List<Account>();
    if(Trigger.isInsert){
        for(Note nt : Trigger.new){
            if(String.valueOf(nt.parentId).startsWith('001')){
                Account acc = new Account(Id=nt.parentId,Last_Note__c=nt.Body); 
               actLstToUpdate.add(acc);
           }   
        }
    }
 
    if(!actLstToUpdate.isEmpty()){
        try{
            update actLstToUpdate;
        }catch(DmlException de ){
            System.debug(de);
        }
    } */
}

 
Best Answer chosen by Phuc Nguyen 18
SarvaniSarvani
Hi,

Please try below code:

Trigger code:
trigger UpdateAccWithLastNote on Note (before insert) {

        UpdateAccwithLastNoteClass.Method(Trigger.new);
       
   }

Apex class code:
public class UpdateAccwithLastNoteClass {
    
    public static void Method(list<Note> Newnotes){
       
        set<id> Accountids=new set<id>();
        list<Account> Accountstobeupdated=new list<Account>();
    
  // Gathering all the Account Ids to which notes are attached 
         for(Note nt : Newnotes){
            if(String.valueOf(nt.parentId).startsWith('001')){
			Accountids.add(nt.parentId);
           }   
        }
    
        if(Accountids.size()>0){
        Accountstobeupdated=[Select id,Last_Note__c from Account where id in :Accountids];
        }
        
   // Comparing with note id and Account id and updating the Last_Note__c field wit note body
        for(Note nt : Newnotes){
	       for(integer i=0;i< Accountstobeupdated.size();i++)
	        {
                if(nt.parentId==Accountstobeupdated.get(i).id)
                {
                Accountstobeupdated.get(i).Last_Note__c=nt.Body;
				}
			}
	  }

 
    if(!Accountstobeupdated.isEmpty()){
        try{
            update Accountstobeupdated;
        }catch(DmlException de ){
            System.debug(de);
        }
    } 
        
    }

}

Hope this helps! Please mark as best if it does.

​​​​​​​Thanks

All Answers

SarvaniSarvani
Hi,

Please try below code:

Trigger code:
trigger UpdateAccWithLastNote on Note (before insert) {

        UpdateAccwithLastNoteClass.Method(Trigger.new);
       
   }

Apex class code:
public class UpdateAccwithLastNoteClass {
    
    public static void Method(list<Note> Newnotes){
       
        set<id> Accountids=new set<id>();
        list<Account> Accountstobeupdated=new list<Account>();
    
  // Gathering all the Account Ids to which notes are attached 
         for(Note nt : Newnotes){
            if(String.valueOf(nt.parentId).startsWith('001')){
			Accountids.add(nt.parentId);
           }   
        }
    
        if(Accountids.size()>0){
        Accountstobeupdated=[Select id,Last_Note__c from Account where id in :Accountids];
        }
        
   // Comparing with note id and Account id and updating the Last_Note__c field wit note body
        for(Note nt : Newnotes){
	       for(integer i=0;i< Accountstobeupdated.size();i++)
	        {
                if(nt.parentId==Accountstobeupdated.get(i).id)
                {
                Accountstobeupdated.get(i).Last_Note__c=nt.Body;
				}
			}
	  }

 
    if(!Accountstobeupdated.isEmpty()){
        try{
            update Accountstobeupdated;
        }catch(DmlException de ){
            System.debug(de);
        }
    } 
        
    }

}

Hope this helps! Please mark as best if it does.

​​​​​​​Thanks
This was selected as the best answer
Phuc Nguyen 18Phuc Nguyen 18
Hey Sarvani,
Thank you, this is spot on and I and probably others appreciate the comments within the class. Could the class be expanded for Updates?  So for example what if I need to get a count of all of the notes associated to an Account?  Or should this be done in a seperate Class? 
Thanks,
P  
SarvaniSarvani
Hi Phuc,

The same trigger works in before update case and might need slight changes for after update case. And to count all notes associated to an account you might after insert context. This can be done by adding context variables in same trigger and different function call within sameclass.

This might look like :
trigger UpdateAccWithLastNote on Note (before insert, after insert ) {
   if(Trigger.isBefore){
       if (Trigger.isInsert) {
        UpdateAccwithLastNoteClass.Method(Trigger.new);
   }
  }

  if(Trigger.isAfter){
      if (Trigger.isInsert) { 
      UpdateAccwithLastNoteClass.CountNotesAttached(Trigger.new);
      }
   } 
       
 }

So you will add method CountNotesAttached  for existing class and update the field in which you are keeping track 
 
public class UpdateAccwithLastNoteClass {
    
    public static void Method(list<Note> Newnotes){
       
        }

public static void CountNotesAttached(list<Note> Newnotes){
// Your code goes in here.
}
}

Hope this helps!

Thanks
Phuc Nguyen 18Phuc Nguyen 18
Thanks Sarvani,
There iseems to be plenty of examples of Triggers with roll up to parents but not classes.  Interesting since I thought I trigger per object was best practice, I would assume there would be more Apex Class examples. 
Cheers,
P