+ Start a Discussion
SFAdmin5SFAdmin5 

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:

 

If:

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”

Then:

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

SFFSFF

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?

SFAdmin5SFAdmin5

Thanks.

 

Basically what I have here is a simple trigger (trigger and test class below...it 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 : Trigger.new) { 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 = 'test@test.com'; contact.AccountId = account.Id; insert contact; Opportunity opportunity = new Opportunity(); opportunity.Name = 'Test'; opportunity.StageName = 'Prospecting'; opportunity.CloseDate = Date.today().addDays(1); 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 Trigger.new ?