You need to sign in to do that
Don't have an account?
DSchild-PTEC
Trigger to send email to multiple contact lookups
Hi,
I am completely new to triggers and I'm hoping someone could provide me some sample code to show me how to do this.
I have a custom object with 4 contact lookup fields. On change of a field called Status I want to send an email to all 4 contacts and the record owner.
QA_Engineer__c
Release_Engineer__c
Release_Manager__c
Support_Engineer__c
Product_Release_Request__c
Thanks!
Have you looked in the new Force.com Workbook? There's a section on triggers. There's also a Dreamforce webcast "Force.com Code for Business Logic, Triggers, and Controllers" The Apex Code Developer's Guide has a ton of reference material on triggers but it's hard to learn from that kind of format - not enough examples or explanations.
Also - have you considered creating a join object to link your custom object and Contacts instead of using separate contact lookup fields? If you have to add more separate contact lookup fields in the future, you'd have to modify your trigger code, whereas with a join table, a select statement will always get all the contacts regardless of how many you have.
David
I'm not following what you mean by join object.
Andyeah, I've looked at the cookbooks and Dev site, but I'm still lost on this.
A "join" object links other objects together. For example, using "CustomToContact__c" as the object, you might have 3 fields:
Master-Detail(CustomObject)
Master-Detail(Contact)
Relationship(Picklist)
At that point, you can simply query this "join" object for all records that might be related, and handle it from there. Simpler queries, less coding, and greater versatility.
I've got almost this same exact problem. I tried to access the DF slides (http://www.salesforce.com/dreamforce/DF09/pdfs/BGDV002_Spraetz.pdf) and got a page not found error. Could you post a snippet of code and I am sure I can take it from there.
Thanks
Scott
If your custom object is My_Object__c with a custom field Status__c, here is a basic trigger. The code depends on the use of Trigger.new and Trigger.old, which are both maps. Because of this, you can use the get method with the ID to retrieve the "before change" record that corresponds to the current "after change" record in the for-loop.
trigger testTrigger on My_Object__c (after update) {
for(My_Object__c myObj : Trigger.new) {
if(myObj.Status__c != Trigger.oldMap.get(myObj.Id).Status__c) {
//code to send email here.
}
}
}
Ideally, you would want to take advantage of the parent-child relationship via a SOQL statement to retrieve those Contact's email addresses. (See the SOQL documentation.)
However, since you have multiple lookup references to Contact, Salesforce cannot resolve which relationship to use for which particular engineer field.
The best solution is to use a join table with master-detail relationships linking to Contact and your custom object. I don't have any code for you because I don't know the exact SOQL syntax or overall code structure off the top of my head.
If you're not familiar with join table concepts, google for "SQL join table" and there should be some good explanations on the web.
The problem I see with creating the master detail relationship is that I can only assign one contact via that relationship.
The purpose of this custom object is to be able to notify a list of people when something occurs. This list is comprised of 4 individuals. All four need an email if the status field of the custom object changes from A to B.
I think I need to stick with the lookup fields, which brings me back to my original issue. :) I don't know what the code should look like to do this.
Thanks
Creating a Many-to-Many Relationship
Thanks for the help. I had not considered this relationship type before.
A join table allows you to establish many-to-many relationships, so that one contact can be related to several My_Object__c records, and one My_Object__c record can be related to several contacts.
If you use a lookup field for each contact, you will need to do a specific select statement for each contact field in the My_Object__c, and you have a limit of 20 SOQL executions in a trigger. The only way to avoid the governor limit is to ignore the bulk trigger best practice, i.e. the resulting trigger code will not contain for-loops. But that means if you ever modify more than one record at the same time (data loader, excel connector, etc) the trigger will act on only one record and not the rest.