+ Start a Discussion
David PiperatoDavid Piperato 

update date/time field when note is created

I have created a date/time field that I would like to be updated to the date/time any time a note is created on an opportunity. I have no experience with Apex Triggers, so I wanted to use process builder, but it doesn't allow processes to be created for the notes object. After reading a few similar posts, I wrote the following trigger, but it doesn't seem to work. If anybody can explain why and how to fix it, I would greatly appreciate it. Thank you in advance. Trigger:

trigger SetOppDate on Note (after insert) {

List<Opportunity> oppsToUpdate = new List<Opportunity>();
    for (Note nRecord : trigger.new) {
        //Check if the note is related to an opportunity
        String parentIdString = String.valueof(nRecord.parentid);
        if (parentIdString.substring(0,3) == '006'){
            //Set the Last_Note__c field on the opportunity
            Opportunity opp = new Opportunity(Id = nRecord.ParentId, Last_Note__c= Date.TODAY());
            oppsToUpdate.add(opp);
       }
    }
    update oppsToUpdate;

}
SarvaniSarvani
Hi David,

I have tested your code and it's working as expected and Updating the field "Last_Note__c" on opportunity record. Where exactly are you trying to add your note to the record. As shown in the image below If you are trying to use the Notes& Attachments (Hightlighted in red) for testing it will not Update your field as it will consider as attachment. Try creating new note using the New Note Button in hightlights panel (Hightlighted with green Button in image) which will tag your note in Notes & attachment section. 

User-added image

Additionally, verify in first place if your note is attached to the Opportunity record using the below query replacing your opportunity record id.
SELECT ID, createddate, parentid, title, body from Note where parentId='006f400000CWVUnXYZ'

Hope this helps! Please mark as best if it does

Thanks
SarvaniSarvani
David,

Also as your field is Date/Time field I recommend using System.now() in line 9 for updating the last_Note__c field which will capture theDate/ time at which last note was created on that record. Try changing your line in the code
Opportunity opp = new Opportunity(Id = nRecord.ParentId, Last_Note__c= System.now());
Thanks
David PiperatoDavid Piperato
Thank you so much for your responses Sarvani. Unfortunately, even when I create the note using the new note button, it's not working. I updated the code to system.now() as well. I don't know how to use the query you suggested, could you possibly explain how to use it? Again, I really appreciate all your help. I attached a screenshot of the trigger because I maybe have put it in wrong or maybe I'm using the wrong kind of trigger? I don't know anything about this stuff, so I'm really in the dark here. User-added image
SarvaniSarvani
Hi David, 

No worries. You can verify from UI too if the note is attached to the Opportunity record navigating to view all on notes & attachments of the record. However, to execute the query Goto Setup->Developer Console -> After a window opens ->  Look for Query editor in bottom section and paste the query and click Execute which will render the notes attached to record. Please find the below link for Developer console query editor
https://help.salesforce.com/articleView?id=code_dev_console_tab_query_editor.htm&type=5 (https://help.salesforce.com/articleView?id=code_dev_console_tab_query_editor.htm&type=5)

I am not sure why the field is not updating on your end, May be look for any other processes which are updating the field.

Thanks