+ Start a Discussion
balaji Jayararmanbalaji Jayararman 

Contact and Lead count against singleEmail limitation. but not for user?

Hi all,

I have apex method that will send outbound email. The issue is "it Count against singleEmail limit for contact and lead, but not for user"?
List<TSB__Survey_Recipients__c> lst_SurveyRecipients = [SELECT Id, TSB__ObjectId__c, TSB__Email_Address__c, TSB__Survey_Master__c FROM TSB__Survey_Recipients__c WHERE Id IN :lst_SurveyRecipientsToProcess];
        Set<String> st_StandardObjIdPrefixes = new Set<String> {'003', '005', '00Q'};
		if (!lst_SurveyRecipients.isEmpty()) {
            TSB__Survey_Master__c surMaster = [SELECT Id, TSB__Email_Template__c, TSB__From_Name__c, TSB__From_Address__c FROM TSB__Survey_Master__c WHERE Id = :lst_SurveyRecipients[0].TSB__Survey_Master__c];
            OrgWideEmailAddress[] owdEmailAddr = [select Id from OrgWideEmailAddress where Address = :surMaster.TSB__From_Address__c];
            String domainUrl = getSiteUrlString();
			Messaging.SingleEmailMessage[] lst_EmailMsg = new List<Messaging.SingleEmailMessage>();
            for (TSB__Survey_Recipients__c surveyRecipient : lst_SurveyRecipients) {
            	Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
				if (String.isNotBlank(surveyRecipient.TSB__ObjectId__c) && (surveyRecipient.TSB__ObjectId__c.length() == 18) && st_StandardObjIdPrefixes.contains(surveyRecipient.TSB__ObjectId__c.subString(0, 3))) {
					System.debug('passing Id = '+surveyRecipient.TSB__ObjectId__c);
					//mail.setToAddresses(new String[]{surveyRecipient.TSB__ObjectId__c});
					mail.setTargetObjectId(surveyRecipient.TSB__ObjectId__c);
					mail.setTreatTargetObjectAsRecipient(true);
					mail.setSaveAsActivity(false);
					if (surveyRecipient.TSB__ObjectId__c.startsWithIgnoreCase('005')) {
						mail.setSaveAsActivity(false);
					}
				} else {
					System.debug('passing email Id = '+surveyRecipient.TSB__Email_Address__c);
					mail.setToAddresses(new String[]{surveyRecipient.TSB__Email_Address__c});
				}
				if (String.isNotBlank(emailSubject)) {
					mail.setSubject(emailSubject);
				}
                if (!owdEmailAddr.isEmpty()) {
                    mail.setOrgWideEmailAddressId(owdEmailAddr[0].Id);
                } else if (String.isNotBlank(surMaster.TSB__From_Name__c)) {
                	mail.setSenderDisplayName(surMaster.TSB__From_Name__c);  
                }
				if (String.isNotBlank(emailBody)) {
					emailBody = emailBody.unEscapeHtml4();
					String url='<a href=\''+domainUrl+surveyRecipient.Id+'\'>click here</a>';					
					emailBody = emailBody.replaceAll('<URL>', url);
					mail.setPlainTextBody(emailBody);
                	mail.setHtmlBody(emailBody);
				}
				System.debug('get Target Object ID = '+mail.getTargetObjectId());
				System.debug('get To Address = '+mail.getToAddresses());
                lst_EmailMsg.add(mail);
            }
            Messaging.SendEmailResult[] lst_EmailResults = Messaging.sendEmail(lst_EmailMsg, false);
			for (Integer i = 0; i < lst_EmailResults.size(); i++) {
				Messaging.SendEmailResult emailResult = lst_EmailResults[i];
				if (emailResult.isSuccess()) {
					lst_SurveyRecipients[i].TSB__Survey_Status__c = 'Email Sent';
				} else {
					lst_SurveyRecipients[i].TSB__Survey_Status__c = 'Delivery Failed';
					lst_SurveyRecipients[i].TSB__Delivery_Failed_Reason__c = '';
					for (Messaging.SendEmailError emailError : emailResult.getErrors()) {
						lst_SurveyRecipients[i].TSB__Delivery_Failed_Reason__c += emailError.getMessage();
					}
				}
			}
			Database.update(lst_SurveyRecipients, false);
		}

 
Deepak DineshDeepak Dinesh

Hi Balaji,

I guess, setTargetObjectId() set against user alone will not count towards email limits.

Please see the salesforce Explanation on the same: Emails sent using 'setTargetObjectId()' set against the User object do not count against the 'SingleEmailMessage' limit.

Please see the link for the same : https://help.salesforce.com/articleView?id=000323568&language=en_US&type=1&mode=1

Please let me know if this solves your query.