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
phiberoptikphiberoptik 

Trigger assistance to send email to specific Contact Role or update Lookup Field with Contact Role

I need to send a weekly email to every Contact Role related to an Account that has a specific Role value, Budget Owner.

 

I am not sure which is easier to accomplish for this.

 

1. Write a trigger that fires an email to any Contact Role with the Role = Budget Owner, when a checkbox on the Account is checked.

 

2. Write a trigger that populates the Contact ID of a Contact Role that has the Role = Budget Owner into a custom lookup field on the Acount and use the lookup field as the recipient for the email.

 

I am not a developer so please tell me which you think makes more sense from a code/management side, and if anyone has any code similar to what I am asking that I can tweak to make work, I appreciate it.

kriskkrisk

So are you intending to selectively send emails to Accounts on which you are checking the checkbox? Or any Account that has a Contact with Contact Role of 'Budget Owner'?

 

Batch Apex Scheduled job is the right approach due to the following reasons

 

1) It is at a Schedule that runs once a week

2) Firing off an email is not something you would want to do it in Trigger

 

If it is selective, then you would have a Checkbox on those Accounts that you want to consider for sending emails and those contacts that have contact role of Budget Owner willl be extracted through SOQL query in Scheduled Job and sent emails

phiberoptikphiberoptik

Let me better explain the process...

 

I will receive a list of external IDs each Monday morning for all Accounts which have new Invoices available. I need to alert the Budget Owner for each of these Accounts with an email template notifying them of their invoice. My intention was to use the data loader to mass update a checkbox field on those Accounts that was the evaluation criteria to trigger the email to the Contact Role defined as Budget Owner. To take it one step further, I need to be able to tell if the Contact has Email Opt Out checked or not.

phiberoptikphiberoptik

At this point, all I need is assistance with a trigger that will populate a custom lookup field on the Account object called Budget_Owner__c with the Contact ID of the Account Contact Role with the Role "Budget Owner".

 

I found this, but not sure how to change it from checking for Primary Contact to checking for Role = Budget Owner. Help!

 

trigger trgMnCopyPrimaryContact on Opportunity (before update) {

   for (Opportunity o : Trigger.new) {
   // o.NextStep = 'DEBUG: Running function...';

       OpportunityContactRole[] contactRoleArray =
       [select ContactID, isPrimary from OpportunityContactRole where OpportunityId = :o.id ORDER BY isPrimary DESC, createdDate];
       if (contactRoleArray.size() > 0) {

           o.Opportunity_contact__c = contactRoleArray[0].ContactID;
          
       }else{
      
           o.Opportunity_contact__c = null;
           // o.NextStep = 'DEBUG; CONTACT = null ';
       }
   }
 }