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
Itay MaozItay Maoz 

Workflow for Opportunity Contact Role

Is there a way to generate an email alert to a Contact listed as primary contact in the opportunity contact role list? If not, is there a way to populate that contact in the Opportunity automatically? 
v varaprasadv varaprasad

Hi Itay,


Please check once below code it may useful for you..



list<OpportunityContactRole> optcont=[SELECT ContactId,contact.email,IsPrimary,LastModifiedDate,OpportunityId,Role FROM OpportunityContactRole WHERE IsPrimary = true];
      system.debug('optcont  >>>'+optcont);

List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();

for(OpportunityContactRole op : optcont){
    if(op.contact.email != null){
        
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    
      // Step 2: Set list of people who should get the email
      List<String> sendTo = new List<String>();
      sendTo.add(op.contact.email);
      mail.setToAddresses(sendTo);
    
      // Step 3: Set who the email is sent from
      mail.setReplyTo('varam@gmail.com');
      mail.setSenderDisplayName('Official Bank of Nigeria');
    
      // (Optional) Set list of people who should be CC'ed
      List<String> ccTo = new List<String>();
      ccTo.add('business@bankofnigeria.com');
      mail.setCcAddresses(ccTo);

      // Step 4. Set email contents - you can use variables!
      mail.setSubject('URGENT BUSINESS PROPOSAL');
      String body = 'Dear ';
      body += 'I confess this will come as a surprise to you.';
      body += 'I am John Alliston CEO of the Bank of Nigeria.';
      body += 'I write to request your cooperation in this ';
      body += 'urgent matter as I need a foreign partner ';
      body += 'in the assistance of transferring $47,110,000 ';
      body += 'to a US bank account. Please respond with ';
      body += 'your bank account # so I may deposit these funds.';
      mail.setHtmlBody(body);
    
      // Step 5. Add your email to the master list
      mails.add(mail);
  
  // Step 6: Send all emails in the master list
  Messaging.sendEmail(mails);
    }
    
}
let me know this helps or not...


Thanks
V varaprasad

 
v varaprasadv varaprasad
put above code in trigger like below


Trigger sendmail OpportunityContactRole(after insert){


}

Thanks
V varaprasad
Itay MaozItay Maoz
Thank you, testing this out! WIll let you know shortly how it went.