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
AAMAAM 

Trigger Sending Email Message Twice only in Production

 

I've recently added a couple of lines of code to a validation script I created in a trigger so that if a field is checked, after insert/update creates an email to be sent to me notifying me of the change. In the sandbox, the trigger works great and sends the email to me just once. However, in production the same trigger causes me to receive the email twice. Any thoughts?

 

Here's the email portion of the code:

 

String userName = UserInfo.getName();
String completeUser = UserInfo.getName() + ' (' + UserInfo.getUserName() + ')';
String triggerType;
				
if(trigger.isInsert) triggerType = 'adding';
if(trigger.isUpdate) triggerType = 'updating';
				        
String subject = userName + ' has overriden the Contact Validation script in Salesforce';
String plainTextHeader = completeUser + ' has overriden the Contact Validation script when ' + triggerType + ' ' + contactName + '\'s record in Salesforce. Here are matching records:\n';
String htmlHeader = '<h3><strong>' + completeUser + ' has overriden the Contact Validation script when ' + triggerType  + ' <a href=\'salesforceOrg' + contactId + '\'>' + contactName + '\'s</a> record in Salesforce. Here are matching records:</strong></h3><ul>';
String plainTextBody = '';
String htmlBody = '';
				
List<Account> a = [SELECT ParentId FROM Account WHERE Id =: branchId AND Home_Office__c != null];
firmId = a[0].ParentId;
				        
for (Contact c : [SELECT Id, Name, Account.Name FROM Contact WHERE Id !=: contactID AND Account.ParentId =: firmID AND Name =: contactName AND Active__c = True])
{
	Id matchId = c.Id;
	String matchName = c.Name;
	String matchCompany = c.Account.Name;
					          
	plainTextBody = plainTextBody + matchName + matchCompany + '\n';
	htmlBody = htmlBody + '<li><a href=\'salesforceOrg' + matchId + '\'>' + matchName + '</a> - ' + matchCompany + '</li>';
}
			        
String htmlFooter = '</ul>';
				
String plainTextMessage = plainTextHeader + plainTextBody;
String htmlMessage = htmlHeader + htmlBody + htmlFooter;
				        
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {'myemailaddress'};
mail.setToAddresses(toAddresses);
mail.setReplyTo('myemailaddress');
mail.setSenderDisplayName('Salesforce.com Contact Validation Override');
mail.setSubject(subject);
mail.setPlainTextBody(plainTextMessage);
mail.setHtmlBody(htmlMessage);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

 

Best Answer chosen by Admin (Salesforce Developers) 
forecast_is_cloudyforecast_is_cloudy

Your email code is probably not the culplit. One possible reason that you're getting 2 emails in Production is that you might have some workflow rules on the object in question that is causing your trigger to fire twice (each time sending out an email). I would recommend turning on the debug logs in Production and see the entire transaction end-to-end when someone updates your record. You should be able to figure out from the logs why your trigger is being invoked twice (if that is in fact the issue).

Best of luck. 

All Answers

forecast_is_cloudyforecast_is_cloudy

Your email code is probably not the culplit. One possible reason that you're getting 2 emails in Production is that you might have some workflow rules on the object in question that is causing your trigger to fire twice (each time sending out an email). I would recommend turning on the debug logs in Production and see the entire transaction end-to-end when someone updates your record. You should be able to figure out from the logs why your trigger is being invoked twice (if that is in fact the issue).

Best of luck. 

This was selected as the best answer
AAMAAM

Thanks for the input, it does appear their is a workflow on Contact that is updating a custom field on insert/update. Of course, now I have to find a way to escape this and make sure my trigger works as well as my test class.

 

BTW, do you know of a good resource for controlling recursive triggers + test class example? I found a good trigger example through the cookbook , but haven't had the same luck with the test class.

 

Thanks again,

Adriel

forecast_is_cloudyforecast_is_cloudy

Off the top of my head, I can't think of a resource for finding test class samples for recursive triggers. The cookbook recipe you referenced is the best source (but it doesn't have testing coverage as you pointed out).