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
Simon WhightSimon Whight 

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,
SonamSonam (Salesforce Developers) 
Following is how you can implment this :

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});

}
Simon WhightSimon Whight
Sonam - thank you for taking the time to get back to me, I'll have a go at this later today.
Simon WhightSimon Whight
Ok, my alternative thought didn't work, but I thought of a way that I can use this. Now, I can get a generic test email to send across but I can't get one to happen based on a template.

My code has altered slightly:
 
trigger NPSEmail on Contract (after update) {

// you need to insert a check to capture the change is the custom field here

System.debug('++++ NPSemail Trigger Starts' );

Contract LastNPSDate = [SELECT SendNPS__c FROM Contract where Id=: trigger.new[0].id];

System.debug('++++ NPSemail Last Email Date' );
System.debug(LastNPSDate);

if(LastNPSDate.SendNPS__c == date.today()){
System.debug('++++ Send Email Execution Starts' );
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>();

System.debug('++++ Email List' );
System.debug(emails);

for(Contact c : conlist)
{
emails.add(c.email); //Get the email addresses of Contacts
}

//Create a message and send accross
System.debug('++++ Email Sends' );
Messaging.SingleEmailMessage mail=new Messaging.SingleEmailMessage();
        //mail.setTargetObjectId(c.id);
        mail.setSaveAsActivity(true);
        mail.setSubject('NPS Survey Feedback');
        mail.setHtmlBody('email body');
        mail.setToAddresses(emails) ; //list of emails
        //mail.setPlainTextBody(plainTxtBody);
        //mail.setTemplateId('00X19000000DdQK');
        Messaging.sendEmail(new Messaging.SingleEmailMessage[]{mail});
}
}

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!
 
Simon WhightSimon Whight
I've just discovered something on this. I believe I am able to work this through Process Builder.

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.