You need to sign in to do that
Don't have an account?
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
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
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...
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?
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.
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
In your select Query add this field dummyforemail__c
That fixed the error message, but didn't make my note counter work, unfortunately.
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 ;
}
Still nothing, I'm afraid, even with that update.
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.
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...
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