+ Start a Discussion
Deanna Aaron 11Deanna Aaron 11 

Trigger to update a custom field in the Account object when a Note is being created?

I want to have a field on the account object called “most recent note”. Basically, when a new note is created, the “note” create date populates here.
 
BONUS: If this is possible— I want to have a field on the account object called “Recent Note Content”. Basically, the most recent note’s content goes here.
 
How would I accomplish this? I sincerely appreciate your help!
Best Answer chosen by Deanna Aaron 11
kd Komalkd Komal
Hi Deanna,

Please try this and let me know if you still face any issue.

 trigger updateMost_Recent_Note on Note (after insert) {
    List<Account> accLstToUpdate = new List<Account>();
    for(Note nt : Trigger.new){
        if(nt.ParentId != null && nt.ParentId.getSObjectType() == Account.sObjectType){
           Account acc=new Account(Id=nt.parentId)
acc.Most_Recent_Note__c =nt.Body;
acc.Most_Recent_Note_Date__c= nt.createdDate;
accLstToUpdate.add(acc);

       } 
    }
    
    if(!accLstToUpdate.isEmpty()){
        update accLstToUpdate;
    }
}

All Answers

kd Komalkd Komal
Hi Deanna,

Could you please try this ?


trigger updateMost_Recent_Note on Note (after insert) {
    List<Account> accLstToUpdate = new List<Account>();
    for(Note nt : Trigger.new){
        if(nt.ParentId != null && nt.ParentId.getSObjectType() == Account.sObjectType){
            Account acc=new Account(Id=nt.parentId,Recent_Note_Content__c=nt.Body,most_recent_note__c = nt.createdDate);
            accLstToUpdate.add(acc);

       } 
    }
    
    if(!accLstToUpdate.isEmpty()){
        update accLstToUpdate;
    }
}
 
Deanna Aaron 11Deanna Aaron 11
Hi KD-- I received a dumb error.
Note: This is the API name for the field created on the account object: Most_Recent_Note__c

User-added image

User-added imageUser-added image
kd Komalkd Komal
Hi Deanna,

Please use this 

Account acc=new Account(Id=nt.parentId,Most_Recent_Note__c=nt.Body);

and if you are going to store the most recent note's createddate data, created a new datetime field and use it in the above code.
 
Deanna Aaron 11Deanna Aaron 11
Hi KD. We're getting closer. Yes, I created a new "date time" field: Most_Recent_Note_Date__c

In inserted the API name below

trigger updateMost_Recent_Note on Note (after insert) {
    List<Account> accLstToUpdate = new List<Account>();
    for(Note nt : Trigger.new){
        if(nt.ParentId != null && nt.ParentId.getSObjectType() == Account.sObjectType){
            Account acc=new Account(Id=nt.parentId,Recent_Note_Content__c=nt.Body,most_recent_note__c = nt.createdDate);
            accLstToUpdate.add(acc);

       } 
    }
    
    if(!accLstToUpdate.isEmpty()){
        update accLstToUpdate;
    }
}

RECEIVED THIS ERROR
Error: Compile Error: Illegal assignment from String to Datetime at line 5 column 25

I'm not sure where to insert this?
Account acc=new Account(Id=nt.parentId,Most_Recent_Note__c=nt.Body);

Thank you for being so patient.
kd Komalkd Komal
Hi Deanna,

Please try this and let me know if you still face any issue.

 trigger updateMost_Recent_Note on Note (after insert) {
    List<Account> accLstToUpdate = new List<Account>();
    for(Note nt : Trigger.new){
        if(nt.ParentId != null && nt.ParentId.getSObjectType() == Account.sObjectType){
           Account acc=new Account(Id=nt.parentId)
acc.Most_Recent_Note__c =nt.Body;
acc.Most_Recent_Note_Date__c= nt.createdDate;
accLstToUpdate.add(acc);

       } 
    }
    
    if(!accLstToUpdate.isEmpty()){
        update accLstToUpdate;
    }
}
This was selected as the best answer
Deanna Aaron 11Deanna Aaron 11
Different error this time-- We may be getting closer though:
User-added image
kd Komalkd Komal
It a silly mistake. i missed a semi-colon on line no. 5. Please add ";" (without quotes) at the end on line-5
Deanna Aaron 11Deanna Aaron 11
The new error I'm getting says: Error: Compile Error: Incorrect SObject type: Note should be Account at line -1 column -1
User-added image
Deanna Aaron 11Deanna Aaron 11
Hmmm... The Trigger is setup on the Account object. Is that an issue? Also, If it should be on the Notes object, I don't see "triggers" for notes on the setup menu.
kd Komalkd Komal
You will not be able to create the trigger from the Setup menu. Log into you developer Console and then create a Trigger from there on Note object.
Deanna Aaron 11Deanna Aaron 11
Thank you, KD! It worked. Is there a way to get the body of the note to update a field on the account object?

Thank you SO very much.
kd Komalkd Komal
I didn't get you. Doing acc.Most_Recent_Note__c =nt.Body; in the code, we are already getting the body of the note into a text /text area field on account object. Please correct me if i am wrong or help me in connecting the dots.
Deanna Aaron 11Deanna Aaron 11
Thank you, KD for everything. I made a minor error on my end. I am very grateful for your help. I hope you go to sleep tonight feeling like you really helped somebody who needed it :)
kd Komalkd Komal
I am glad i could help you out. Please mark the best answer so that the thread could be closed.