4

I am observing a very odd issue. It does not happen consistently, but I have seen it in both scratch orgs and sandbox orgs. I have a custom Note object (Note__c) with a Rich Text Area field Notes__c. I have a trigger that fires on update to take action when the field value changes. This normally works, but sometimes it does not. While trying to track down the issue, I added debug logging to my code that shows that both Trigger.New and Trigger.Old contain the same value for the edited field. However, the edits ARE being saved to the record.

trigger NoteTrigger on Note__c (after insert, after update, after undelete) {
    if (Trigger.isAfter) {
        if (Trigger.isInsert) {
            NoteService.createRevisions(Trigger.new);
        }
        else if (Trigger.isUpdate) {
            NoteService.createRevisions(Trigger.new, Trigger.oldMap);
        }
    }
}

and from the NoticeService class:

public static void createRevisions(List<Note__c> newList, Map<Id, Note__c> oldMap) {
    System.debug('createRevisions called on Update');
    List<Note__c> forRevision = new List<Note__c>();
    for (Note__c newNote : newList) {
        Note__c oldNote = oldMap.get(newNote.Id);
        if (oldNote.Notes__c != newNote.Notes__c) {
            System.debug('createRevisions: change found for note ' + newNote.id);
            forRevision.add(newNote);
        }
        else {
            System.debug('createRevisions: change NOT found for note ' + newNote.id);
            System.debug('old: ' + JSON.serialize(oldNote));
            System.debug('new: ' + JSON.serialize(newNote));
        }
    }
    if (forRevision.size() > 0) {
        createRevisions(forRevision);
    }
}

Note the debugging output. When the issue occurs, the debug log clearly shows that the trigger ran, and that it saw no difference between Notes__c field values in the Trigger.New and Trigger.Old records. In the log below I changed the last line of the text from "Updated Content." to "Modified Content.", and that change was saved to the record, but the Trigger didn't see that:

09:34:15.0 (199056785)|USER_DEBUG|[26]|DEBUG|createRevisions called on Update
09:34:15.0 (216612036)|USER_DEBUG|[35]|DEBUG|createRevisions: change NOT found for note a0D19000004l2nhEAA
09:34:15.0 (216995176)|USER_DEBUG|[36]|DEBUG|old: {"attributes":{"type":"Note__c","url":"/services/data/v43.0/sobjects/Note__c/a0D19000004l2nhEAA"},"LastModifiedDate":"2018-08-14T16:32:08.000+0000","IsDeleted":false,"Current_Revision__c":2,"OwnerId":"005190000020ytyAAA","CreatedById":"005190000020ytyAAA","Notes__c":"<p><br></p><h3><span style=\"font-family: Arial; font-size: 14pt;\">Meeting Notes Reminder Email</span></h3><p><span style=\"font-family: Arial; font-size: 11pt;\">For users who wish to receive them, the system will send a reminder email to the meeting owner prior to the meeting. To preserve system resources, the system will run an automated batch job once per hour to send any emails for the upcoming period (configurable). This email will include a unique identifier for the meeting to be used by the system for email-to-notes (see next section). If the meeting already has notes, the email will also include a unique identifier for the current revision, as well as the current text of the notes. Scheduled jobs will only send reminder for users who have opted in. A trigger will watch for meetings that have been scheduled/rescheduled such that the reminder window has passed, and send the reminder directly.</span></p><p><br></p><p><span style=\"font-size: 0.8125rem;\">Updated Content. </span></p>","CreatedDate":"2018-08-12T14:07:15.000+0000","Id":"a0D19000004l2nhEAA","LastModifiedById":"005190000020ytyAAA","Name":"NT-0001","SystemModstamp":"2018-08-14T16:32:08.000+0000"}
09:34:15.0 (217208780)|USER_DEBUG|[37]|DEBUG|new: {"attributes":{"type":"Note__c","url":"/services/data/v43.0/sobjects/Note__c/a0D19000004l2nhEAA"},"LastModifiedDate":"2018-08-14T16:34:15.000+0000","IsDeleted":false,"Current_Revision__c":2,"OwnerId":"005190000020ytyAAA","CreatedById":"005190000020ytyAAA","Notes__c":"<p><br></p><h3><span style=\"font-family: Arial; font-size: 14pt;\">Meeting Notes Reminder Email</span></h3><p><span style=\"font-family: Arial; font-size: 11pt;\">For users who wish to receive them, the system will send a reminder email to the meeting owner prior to the meeting. To preserve system resources, the system will run an automated batch job once per hour to send any emails for the upcoming period (configurable). This email will include a unique identifier for the meeting to be used by the system for email-to-notes (see next section). If the meeting already has notes, the email will also include a unique identifier for the current revision, as well as the current text of the notes. Scheduled jobs will only send reminder for users who have opted in. A trigger will watch for meetings that have been scheduled/rescheduled such that the reminder window has passed, and send the reminder directly.</span></p><p><br></p><p><span style=\"font-size: 0.8125rem;\">Updated Content. </span></p>","CreatedDate":"2018-08-12T14:07:15.000+0000","Id":"a0D19000004l2nhEAA","LastModifiedById":"005190000020ytyAAA","Name":"NT-0001","SystemModstamp":"2018-08-14T16:34:15.000+0000"}

Is this a known issue? I could find no documented limitation about using triggers to evaluate changes in a Rich Text Area field.

Note I am aware of the OOB Notes functionality including the new Content Notes; it is not suitable in this case for several reason. I'm only concerned with the observed faulty trigger behavior.

  • I think you may be experiencing this known issue. – Jayant Das Aug 14 '18 at 17:45
  • @JayantDas Wow, I could not find that. Though I first looked last week; since then the issue stopped but recurred today. If you will make this an Answer I will accept it. Thanks! – Jason Clark Aug 14 '18 at 17:47
  • 1
    I knew this because it made to known issue only after we reported it :) – Jayant Das Aug 14 '18 at 18:05
3

As your operations are in the "after" trigger on a text area field, this is a known issue where trigger values are impacted.

Referring the old value after an "operation" on the new values actually worked. So if you were to compare the old value with the new value, and that you access the "new" value first in the comparison, it will work. Refer to the below excerpt from the issue:

everything works as expected only when referencing the new value FIRST, before making any reference to old.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.