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
Mike RethageMike Rethage 

Send emails to multiple Contact Roles via Trigger

I've seen a lot of frustration around sending automated emails to various contact roles both in Contracts and Opportunities.  It seems like one of the leading soluions is to create a custom field and populate it via apex with the appropriate contact role.  But what about if you have multiple contact roles that fit the criteria?  For example, I may have multiple "Install Contacts" on a contract that I would want to get the same email.  How can I automate this?  Via a Trigger?

Thanks!
Boris BachovskiBoris Bachovski
You could use the Process Builder to flag specific contact roles that you want to send an email to, and then have a workflow that will send the email and also reset the flag. Alternatively you can use a trigger.
Mike RethageMike Rethage
Thank you.  I have attempted to use multiple record look-ups to send the email via workflow but can't seem to get the ticket.  Salesforce support recommending I post a link here.

https://cs13.salesforce.com/designer/designer.apexp#Id=301W00000004UnVIAU

It seems like a workflow may be capable of selecting the particular roles and sending the email and I can use the process builder to trigger it when a contract status is updated.  However my flow logic just doesn't seem to do anything.

Any help is appreciated.
Mike RethageMike Rethage
trigger Contract_CreateWebShow on Contract (after update){
    System.debug('Contract_CreateWebShow triggered');
    
    Set<Id> activatedContractIds = new Set<Id>();

    for (Contract each : trigger.new) {
        if (each.Status == 'In Progress' && trigger.oldMap.get(each.Id).Status != 'In Progress')
        {
            System.debug('Contract ' + each.Id + ' transitioned into IN PROGRESS status.');
            activatedContractIds.add(each.Id);
            
            if (each.Web_Shows__c != null) {
                if (each.Web_Shows__c > 0)                
                {                                   
                    Equipment__c newEQPT = new Equipment__c (
                        Account__c = each.AccountId,
                        Name = [Select Name From Account WHERE Id =: each.AccountId].Name,
                        RecordTypeId = [Select Id From RecordType WHERE Name = 'Web Shows'].Id,
                        Contract__c = each.Id,
                        Product_Type__c = 'Web Shows'
                    );
                    
                    insert newEQPT;
                    List<ContractContactRole> contactRoles = [select ContractId, ContactId from ContractContactRole where Role = 'Training Contact' and ContractId in :activatedContractIds];
    
    				for (ContractContactRole eachRole : contactRoles) {Contract eachContract = trigger.newMap.get(eachRole.ContractId);
                    
                    Junction_Equipment_Contact__c newJunct = new Junction_Equipment_Contact__c(
                        Equipment__c = newEQPT.Id,
                        Contact__c = eachRole.ContactId
                    );
                    
                    insert newJunct;
                    
                    System.debug('Added Web Show: ' + newEQPT.Id);
                }
            }            
        }
    }
}
}
sharing my finalized code in case someone else would like to adapt for their own purposes.