You need to sign in to do that
Don't have an account?
Send email to all related contacts on contract
Hi,
Hoping this is a relatively easy query. I'm hoping to make a trigger that will send an email to all related Contacts (related on Account) on a Contract.
Essentially, it is an anniversary catch up: Contract Start Date + X days/months/years = designated milestone then send email based on email template to all Contacts.
Can anyone help me out on this?
Thanks,
Hoping this is a relatively easy query. I'm hoping to make a trigger that will send an email to all related Contacts (related on Account) on a Contract.
Essentially, it is an anniversary catch up: Contract Start Date + X days/months/years = designated milestone then send email based on email template to all Contacts.
Can anyone help me out on this?
Thanks,
Since a trigger can only be trggered through a DML operation on a record, you will have to create a custom field on Contract which will get updated on the day of anniversary(time based workflow - field update)
The field update will trigger the trigger, which will fire only when the value of that custom field changes(you can capture the change in value using ISCHANGED and then send out the maisl to contacts : sampe code of trigger to send out emails to contacts below:
trigger Sendannivemail on Contract (after update) {
// you need to insert a check to capture the change is the custom field here
Contract cont = [SELECT AccountId FROM Contract where Id=: trigger.new[0].id]; //get the ACCOUNT form Contract
List<Contact> conlist = [SELECT FirstName, email from Contact where AccountID =:cont.AccountId]; // Get Contact list related to ACCOUNT
List<String> emails = new List<String>();
for(Contact c : conlist)
{
emails.add(c.email); //Get the email addresses of Contacts
}
//Create a message and send accross
Messaging.SingleEmailMessage mail=new Messaging.SingleEmailMessage();
//mail.setTargetObjectId(c.id);
mail.setSaveAsActivity(true);
mail.setSubject('Meeting testing contract email');
mail.setHtmlBody('email body');
mail.setToAddresses(emails) ; //list of emails
//mail.setPlainTextBody(plainTxtBody);
//mail.setTemplateId(template.id);
Messaging.sendEmail(new Messaging.SingleEmailMessage[]{mail});
}
My code has altered slightly:
I've commented out the template ID.
Also, I think I will have to replace if(LastNPSDate.SendNPS__c == date.today()) with trigger.old/new as I don't want it to send if someone edits and closes the record. If you could help with that, it would be wonderful!
When working from Account, you can update a record. When you select as a field to update from the field list Contract (it will say it is a string), it then gives you the ability to further update fields on the Contact screen. I am assuming that this will update all related Contacts when running.
What it doesn't appear to do is allow any logic here - i.e. send to all contacts where Field X = True, but you should be able to layer it with another Process Builder flow that runs off a checkbox on the Contact field being checked.