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

Trigger to send email to Opportunity Primary Contact Role on Opportunity Insert Only

I need to have Salesforce automatically send an email to the Primary Contact Role on an opportunity, when the opportunity is created only (not updated), when the created opportunity meets very specific criteria in 4 of its fields.


I can get this to work for one-off opps created in the UI with a combination of a trigger and a workflow rule with email alert, BUT, this is not a viable solution, because I'm hitting governor limits when opps are created in bulk.


Any code help that a dev can provide would be much appreciated.


Here are the requirements for this to work:



An opportunity is created


And the following criteria are TRUE regarding that opportunity:

  • Opportunity Creator = “Salesforce Leads RT”
  • Opportunity Record Type = “Custom Services”
  • Opportunity field called Service_Type__c  = “Quick Start”
  • Opportunity field called Referring_Group__c = “In-Product Sale”


Send an email from Salesforce to the Contact Role on that opportunity listed as the Primary Contact on that opportunity


Note: the email should be sent using a custom template that is already created and saved in Salesforce with name “Test”.  If this is not possible and there is some other way to send the email subject/body, that is fine, as long as I can modify the subject/body content


What are the governor limit errors you are having? I've implemented something similar before and didn't have any issues. Can you post your code?




Basically what I have here is a simple trigger (trigger and test class passes 100% with this test), that updates a custom lookup field on opp object called Primary_Contact__c (a lookup field to contact object).


The trigger just populates an opp' Primary Contact with the first contact created for the account the opp belongs to.  This is good enough.


Everything works, except when I got into my sandbox and do a test insert of 1000 new opps, I get an error that says there are "too many soql queries".


  ///Set PrimaryOppContact

///trigger on opp object   

if (Trigger.isInsert && Trigger.isBefore) { for (Opportunity o : { List<Contact> contactList = [select Id from Contact where AccountId = :o.AccountId]; o.Primary_Contact__c = contactList[0].Id; } }

///test method static testMethod void testPrimaryOppContact() { Account account = new Account(); account.Name = 'Test'; insert account; Contact contact = new Contact(); contact.FirstName = 'TestFirst'; contact.LastName = 'TestLast'; contact.Email = ''; contact.AccountId = account.Id; insert contact; Opportunity opportunity = new Opportunity(); opportunity.Name = 'Test'; opportunity.StageName = 'Prospecting'; opportunity.CloseDate =; opportunity.AccountId = account.Id; insert opportunity; Opportunity resultOppty = [Select Id, Primary_Contact__c from Opportunity where Id = :opportunity.Id ]; System.assertEquals(contact.Id, resultOppty.Primary_Contact__c); }


Veeru AVeeru A
Don't you need to bulkify this trigger by doing a for with ?