function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Lee_CampbellLee_Campbell 

A way to create an email notification when a note is added to a specific record?

Hi folks,

 

I'm looking for a way to email the custom object record owner when a note is added to that custom object record. My intial thoughts are that a) a trigger that operates on notes would cause an email to be sent to the record owner any time a note is added to any record, so... b) it might be a good idea to set up some kind of dummy variable that changes when a new note is added to a custom object record and from there, use a workflow to trigger this email alert. Question is... how can I query for new notes added to my custom object? Any ideas? I'd be eternally grateful. I'll buy you chocolate/beer/a similar value gift of your choosing...

 

Thanks,

Lee

Best Answer chosen by Admin (Salesforce Developers) 
neophyteneophyte

You are changing the field value of the parent record, but never performing a DML. Do an update on the list of parents, after the field value (counter value) is changed. In your case : update notelist ;

 

trigger NoteAddedNotification on Note (after insert) {
    Set<ID> noteids = new Set<ID>();
    for (Note n:Trigger.new){
        noteids.add(n.parentID);
    }
    List<Change_Request__c> notelist = new List<Change_Request__c>([Select id from Change_Request__c where ID in:noteids]);
    for(Change_Request__c c:notelist){
        c.dummyforemail__c+=1;
    }

update notelist ;
}

 

 

All Answers

SammyComesHereSammyComesHere
You can make use of a Workflow rule --------- which checks for change in Notes and as they are fired after update you can ensure that you get the updated value from the System. Work Action ---- Email
SammyComesHereSammyComesHere
If notes is like a child object create a roll up field which checks for increase in count and then invoke workflow based on that . Notes Updated - ISCHANGED( Pre_Call_Notes__c ) ----- If only one field as Notes If notes is a child object create a Roll up field for checking
Lee_CampbellLee_Campbell

The "Notes" functionality of Salesforce isn't available to choose within the wokflow criteria or the field multi-picklist you're presented with when making formulae, so I'm afraid that wouldn't work...

Lee_CampbellLee_Campbell
trigger NoteAddedNotification on Note (after insert) {
    Set<id> noteids = new Set<id>();
    for(Note n:Trigger.new){
        noteids.add(n.id);
    }
    List<Change_Request__c> notelist = new List<Change_Request__c>([Select id from Change_Request__c where id in :noteids]);

    for(Change_Request__c a:notelist){
        a.dummyforemail__c++;
    }
}

I tried the above, but it doesn't work. The idea was to create a dummy variable that just increases by one every time a new Note is added, so that I could simply say if the current value of the dummy variable is greater than the prior, fire off some emails, but the value doesn't change. Is my ID sharing wrong or something?

Lee_CampbellLee_Campbell

And the following code, which I think is a bit closer to the mark, resulted in the error message at the foot of this post upon adding a new note, rather than increasing my "dummyforemail" variable by 1.

 

trigger NoteAddedNotification on Note (after insert) {
    Set<ID> noteids = new Set<ID>();
    for (Note n:Trigger.new){
        noteids.add(n.parentID);
    }
    List<Change_Request__c> notelist = new List<Change_Request__c>([Select id from Change_Request__c where ID in:noteids]);
    for(Change_Request__c c:notelist){
        c.dummyforemail__c+=1;
    }
}

 Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger NoteAddedNotification caused an unexpected exception, contact your administrator: NoteAddedNotification: execution of AfterInsert caused by: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Change_Request__c.dummyforemail__c: Trigger.NoteAddedNotification: line 8, column 1

SammyComesHereSammyComesHere

In your select Query add this field dummyforemail__c

Lee_CampbellLee_Campbell

That fixed the error message, but didn't make my note counter work, unfortunately.

neophyteneophyte

You are changing the field value of the parent record, but never performing a DML. Do an update on the list of parents, after the field value (counter value) is changed. In your case : update notelist ;

 

trigger NoteAddedNotification on Note (after insert) {
    Set<ID> noteids = new Set<ID>();
    for (Note n:Trigger.new){
        noteids.add(n.parentID);
    }
    List<Change_Request__c> notelist = new List<Change_Request__c>([Select id from Change_Request__c where ID in:noteids]);
    for(Change_Request__c c:notelist){
        c.dummyforemail__c+=1;
    }

update notelist ;
}

 

 

This was selected as the best answer
Lee_CampbellLee_Campbell

Still nothing, I'm afraid, even with that update.

neophyteneophyte

Not sure why the counter is not working. Check if the field is really of number type. If yes, probably the default value is not set to 0. Try setting it to zero in the code itself if the counter field is null and then try incrementing it. 

 

if(c.dummyforemail__c == null)

c.dummyforemail__c = 0;

 

Also, add debug statement before and after incrementing the field.

for(Change_Request__c c: notelist){

System.debug('value before incrementing: ' +  c.dummyforemail__c);

c.dummyforemail__c = c.dummyforemail__c + 1;

System.debug('value after incrementing: ' +  c.dummyforemail__c);

}

update notelist;

 

Check the debug logs and see if the update is actually happening or if failing due to some reason.

Lee_CampbellLee_Campbell

I get 0 before and 1 after in the debug log...

 

Woe is me...

 

The parentID is the right thing to identify the Custom Object record to which the note is attached, right? Also, it is a number the "dummyforemail__c" field. I made it myself two days ago...

Lee_CampbellLee_Campbell

Another Trigger was blocking the update, it works fine now. I'm an idiot. I've marked the appropriate solution as the winner :)

 

Thanks very much for your help, guys. The good nature of the Developer Force community never ceases to amaze me! Enjoy your weekends.

 

 

Lee