You need to sign in to do that
Don't have an account?
David 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;
}
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;
}
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.
Additionally, verify in first place if your note is attached to the Opportunity record using the below query replacing your opportunity record id.
Hope this helps! Please mark as best if it does
Thanks
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 Thanks
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