Trigger to update a custom field in the Account object when a Note is being created? - Answers - Salesforce Trailblazer Community
Trailblazer Community
Ask Search:
Deanna AaronDeanna Aaron 

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

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
Narender SinghNarender Singh
Hi,

Use this:
 
trigger updateAccountField on Note (after insert) {
    List<Account> accUpdate = new List<Account>();
    for(Note n : Trigger.new){
        if(n.ParentId != null && n.ParentId.getSObjectType() == Account.sObjectType){
            Account acc=new Account(Id=n.parentId,Most_recent_Note__c=n.CreatedDate);
            accUpdate.add(acc);

       } 
    }
    
    if(accUpdate.size()>0){
        update accUpdate;
    }
}

 

All Answers

Siddharth SehgalSiddharth Sehgal
Hi Deanna, 

I have developed trigger for you. You just need to try in your org

trigger updateAccountField 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,RecentNoteContent__c=nt.Body);
            accLstToUpdate.add(acc);

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

Note : Please replace api name of Account field at the place of "RecentNoteContent__c". 

Hope this helps!
Thanks!
Pierre Despatis-DupontPierre Despatis-Dupont
As far as I know, Notes are only available via FLOW. I am not an expert on flows but it looks like you could update an account field using the text in the note that triggered the update. You will want the flow to trigger only when the parent account is that of an account. 

There's a trailhead on flows that could help. As far as I know that is the "easiest" option. 

 
Prabhat SharmaPrabhat Sharma
Hey Deanna,

You can write a trigger on Note object on after insert event. The field Parent ID on Notes points to its parent. e.g in your case it's Account. You can query the fields that you want to update where Ids are the collection of parentIds, and then do an update operation on those Accounts.
 
Deanna AaronDeanna Aaron
Sid-- I tried using your trigger, but received an error. Thank you for your help.
See error below:

User-added image
Narender SinghNarender Singh
Hi,

Use this:
 
trigger updateAccountField on Note (after insert) {
    List<Account> accUpdate = new List<Account>();
    for(Note n : Trigger.new){
        if(n.ParentId != null && n.ParentId.getSObjectType() == Account.sObjectType){
            Account acc=new Account(Id=n.parentId,Most_recent_Note__c=n.CreatedDate);
            accUpdate.add(acc);

       } 
    }
    
    if(accUpdate.size()>0){
        update accUpdate;
    }
}

 
This was selected as the best answer
Narender SinghNarender Singh
I am assuming that Most_recent_Note__c is a date time field.
Narender SinghNarender Singh
In case it's a text field then, modify this:
Account acc=new Account(Id=n.parentId,Most_recent_Note__c=n.CreatedDate);

to this:
Account acc=new Account(Id=n.parentId,Most_recent_Note__c=string.valueOf(n.CreatedDate));
Deanna AaronDeanna Aaron
Narender-- I created a new field (date/time) - API NAME:  Most_Recent_Note_Date__c

I inserted the code with the date/time field and received this error:
User-added image

Thank you for your help.
Narender SinghNarender Singh
Change this: 
Account.sObjectType

to this:
schema.Account.sObjectType
Deanna AaronDeanna Aaron
Updated to "schema.Account.sObjectType". The error still remains.
User-added image
Narender SinghNarender Singh
Are you creating the trigger on Account??
Narender SinghNarender Singh
Make sure you're creating you're trigger on note object.
Deanna AaronDeanna Aaron
Sorry, Narender! I was trying to save the trigger on the "Account" object. When I type "Notes" in the setup menu, there is no ability to setup a trigger for this object. I also typed in "trigger" and didn't see a "Notes" area.
Narender SinghNarender Singh
You will have to create it from developer console. Open Dev console. Click on 'New' -> 'Apex Trigger'. Give it a name. Select note object from dropdown
Narender SinghNarender Singh
Don't forget to save it. And the trigger name should be 'updateAccountField'
Deanna AaronDeanna Aaron
Narender-- IT WORKS!! Is there a way to do this for the body content of the note as well?
Narender SinghNarender Singh
Yes. You can do this by creating a custom long text area field and then use it in your trigger. Update the line to this: 
Account acc=new Account(Id=n.parentId,Most_recent_Note__c=n.CreatedDate, customtextfield__c=n.Body);
Deanna AaronDeanna Aaron
Narender - You are seriously THE BEST! The body and date work PERFECTLY. Lastly, I want to add the note "title", but I couldn't get it to work. I made a new field: "Most Recent Note Title" a Text Area field called: Most_Recent_Note_Title__c

User-added image
Narender SinghNarender Singh
Use this line:
Account acc=new Account(Id=n.parentId,Most_recent_Note__c=n.CreatedDate, customtextfield__c=n.Body,Most_Recent_Note_Title__c=n.Title);

Your bracket was misplaced.
** Replace field names with their api names. I am on mobile so I just copy pasted the old  one with necessary changes.
Deanna AaronDeanna Aaron
I'm VERY sorry to bother you because you have been SO helpful and patient.
It appears that this trigger has failed to deploy:
User-added image

This is the Trigger that you helped me to develop:

trigger updateAccountField on Note (after insert) {
    List<Account> accUpdate = new List<Account>();
    for(Note n : Trigger.new){
        if(n.ParentId != null && n.ParentId.getSObjectType() == schema.Account.sObjectType){
Account acc=new Account(Id=n.parentId,Most_Recent_Note_Date__c=n.CreatedDate, Most_Recent_Note_Body__c=n.Body,Most_Recent_Note_Title__c=n.Title);
            accUpdate.add(acc);

       } 
    }
    
    if(accUpdate.size()>0){
        update accUpdate;
    }
}

 
Narender SinghNarender Singh
You will need to write a test class for the trigger. I will get back to you positively by tomorrow to help you with the test class. Right now I gotta go. It's 4 am here and am dead sleepy, lol. :D
Deanna AaronDeanna Aaron
Yes, please go and take care of yourself. Go to sleep feeling proud that you helped someone who really needed it! I am very thankful for your time.
Deanna AaronDeanna Aaron
Narender-- I got the Test Class to work this morning! I cannot thank you enough for helping us SO much!

@isTest
public class updateAccountFieldTest {

    @isTest static void triggerTest() {
         
        Account testAccount = new Account(Name = 'testAccount');
        insert testAccount;
         
        Note testNote = new Note(
            Title = 'Test Note',
            ParentId = testAccount.Id,
            Body = 'testing 1 2 3'
        );
        insert testNote;
         
        Note nt = [SELECT Title, Body, CreatedDate FROM Note LIMIT 1];
        Account acct = [SELECT Most_Recent_Note_Date__c, Most_Recent_Note_Body__c, Most_Recent_Note_Title__c FROM Account LIMIT 1];

        System.assertEquals(acct.Most_Recent_Note_Date__c, nt.CreatedDate);
        System.assertEquals(acct.Most_Recent_Note_Body__c, nt.Body);
        System.assertEquals(acct.Most_Recent_Note_Title__c, nt.Title);
    }  

}
Narender SinghNarender Singh
That's great!! Your test class is perfect!
I am glad I could help you. :D 
Deanna AaronDeanna Aaron
Hi Narender. I'm trying the replicate this process that you've helped me with. Instead of pushing the most recent note "Title","Body", and "Date" to a fields on the account object, I'm trying to accomplish this with tasks, but I'm getting this error. Do you have any idea how to overcome this? Thank you, again for your help!User-added image
Narender SinghNarender Singh
Ahh! I see what you are doing.
So the thing is there is no field called ParentId on the Task object.

Every standard object has a defined set of standard fields. And for tasks, you can see the list of those fields here:
https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_task.htm

In tasks, instead of parentId we have WhatId (Label: Name) and WhoId(Label: Related To).

-The WhoId represents a human such as a lead or a contact. WhoIds are polymorphic. Polymorphic means a WhoId is equivalent to a contact’s ID or a lead’s ID.

-The WhatId represents nonhuman objects such as accounts, opportunities, campaigns, cases, or custom objects. WhatIds are polymorphic too.

So in our case we want to relate the task to an Account record, so in our code, we need to use:
n.WhatId

I hope I was able to explain it to some extent at least.
Don't hesitate to ask anything.
Narender SinghNarender Singh
One more thing, I would like to add.

To see what all standard fields are present on the object, go to this URL:
https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_quickstart_intro.htm

And search the object you want to search.

The more you play with Apex, more familiar you will get the backend process.
Deanna AaronDeanna Aaron
Thank you SOOO much, Narender! You seriously rock! It's deployed and everything works perfectly!
Narender SinghNarender Singh
Am glad. :)
Narender SinghNarender Singh
Wait Wait! Did I hear deployed?
You don't really need the trigger for what you are doing on Task object.
You can do it with a process builder. I would advice you not to use trigger for this. Why? Because coding comes with lots of considerations. So try to avoid it where ever you can.
Dan MooreDan Moore
Hi Narender - I know this is an old thread but wondering you could help with exactly the above however using the "new" Notes feature in Salesforce which I believe requires calling ContentNote with a ContentDocumentLink?
We are looking to do the same as the above user (copy most recent note content and date) into a custom field, on Contact and Opportunity levels.
Thank you!
Evan AielloEvan Aiello
Dan - did you ever find a solution to this?
David GDavid G
Hi everyone!!

Does anyone know how to make a copy of a content note ? I have problems copying the content

Thanks in advance