Skip to content

Commit 77fe275

Browse files
authored
Reduced SingleEmailMessage limit usage (#570)
* Fixed #369 by using instance method Messaging.SingleEmailMessage.setTargetObjectId() when sending failure emails to internal users * Updated build.yml to delete all profiles in the unsorted directory before generating package versions, because they sometimes upset the sf cli ᕕ( ᐛ )ᕗ
1 parent f2fe85c commit 77fe275

10 files changed

Lines changed: 78 additions & 113 deletions

File tree

.github/workflows/build.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,9 @@ jobs:
298298
# TODO remove this after the plugin @salesforce/packaging has been upgraded
299299
# to a new version that only looks at package directories
300300
- name: 'Temporarily delete problematic metadata'
301-
run: rm -rf ./config/experience-cloud
301+
run: |
302+
rm -rf ./config/experience-cloud/
303+
rm -rf ./nebula-logger/unsorted/main/default/profiles/
302304
303305
- name: 'Create Beta Managed Package Version'
304306
run: npm run package:version:create:managed
@@ -352,7 +354,9 @@ jobs:
352354
# TODO remove this after the plugin @salesforce/packaging has been upgraded
353355
# to a new version that only looks at package directories
354356
- name: 'Temporarily delete problematic metadata'
355-
run: rm -rf ./config/experience-cloud
357+
run: |
358+
rm -rf ./config/experience-cloud/
359+
rm -rf ./nebula-logger/unsorted/main/default/profiles/
356360
357361
- name: 'Create & Install Package Version'
358362
run: npx pwsh ./scripts/build/create-and-install-package-version.ps1 -targetpackagealias '"Nebula Logger - Core"' -targetreadme ./README.md -targetusername nebula-logger-package-demo

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55

66
The most robust logger for Salesforce. Works with Apex, Lightning Components, Flow, Process Builder & Integrations. Designed for Salesforce admins, developers & architects.
77

8-
## Unlocked Package - v4.11.10
8+
## Unlocked Package - v4.11.11
99

10-
[![Install Unlocked Package in a Sandbox](./images/btn-install-unlocked-package-sandbox.png)](https://test.salesforce.com/packaging/installPackage.apexp?p0=04t5Y000001OigxQAC)
11-
[![Install Unlocked Package in Production](./images/btn-install-unlocked-package-production.png)](https://login.salesforce.com/packaging/installPackage.apexp?p0=04t5Y000001OigxQAC)
10+
[![Install Unlocked Package in a Sandbox](./images/btn-install-unlocked-package-sandbox.png)](https://test.salesforce.com/packaging/installPackage.apexp?p0=04t5Y000001Oih7QAC)
11+
[![Install Unlocked Package in Production](./images/btn-install-unlocked-package-production.png)](https://login.salesforce.com/packaging/installPackage.apexp?p0=04t5Y000001Oih7QAC)
1212
[![View Documentation](./images/btn-view-documentation.png)](https://jongpie.github.io/NebulaLogger/)
1313

14-
`sf package install --wait 20 --security-type AdminsOnly --package 04t5Y000001OigxQAC`
14+
`sf package install --wait 20 --security-type AdminsOnly --package 04t5Y000001Oih7QAC`
1515

16-
`sfdx force:package:install --wait 20 --securitytype AdminsOnly --package 04t5Y000001OigxQAC`
16+
`sfdx force:package:install --wait 20 --securitytype AdminsOnly --package 04t5Y000001Oih7QAC`
1717

1818
---
1919

nebula-logger/core/main/log-management/classes/LoggerEmailSender.cls

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public without sharing class LoggerEmailSender {
1515
private static final List<Messaging.SingleEmailMessage> SENT_EMAILS = new List<Messaging.SingleEmailMessage>();
1616

1717
@TestVisible
18-
private static final List<String> CACHED_APEX_ERROR_RECIPIENTS {
18+
private static final List<ApexEmailNotification> CACHED_APEX_ERROR_RECIPIENTS {
1919
get {
2020
if (CACHED_APEX_ERROR_RECIPIENTS == null) {
2121
CACHED_APEX_ERROR_RECIPIENTS = queryApexErrrorRecipients();
@@ -82,18 +82,30 @@ public without sharing class LoggerEmailSender {
8282
return;
8383
}
8484

85-
if (CACHED_APEX_ERROR_RECIPIENTS.isEmpty()) {
86-
if (LoggerParameter.ENABLE_SYSTEM_MESSAGES) {
87-
Logger.info('Logger - no Apex email recipients configured, skipping sending email');
85+
if (CACHED_APEX_ERROR_RECIPIENTS.isEmpty() == true) {
86+
if (LoggerParameter.ENABLE_SYSTEM_MESSAGES == true) {
87+
// One of a few limited places in the codebase (except tests) that should use System.debug()
88+
// The rest of the codebase should use a method in Logger.cls
89+
System.debug(System.LoggingLevel.WARN, 'Nebula Logger - no Apex email recipients configured, skipping sending email'); // NOPMD
8890
}
8991
return;
9092
}
9193

92-
Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
93-
message.setToAddresses(CACHED_APEX_ERROR_RECIPIENTS);
94-
message.setSubject(buildSubject(errorMessages));
95-
message.setHtmlBody(buildHtmlBody(sobjectType, errorMessages));
96-
sendEmail(message);
94+
List<Messaging.SingleEmailMessage> messages = new List<Messaging.SingleEmailMessage>();
95+
for (Schema.ApexEmailNotification notification : CACHED_APEX_ERROR_RECIPIENTS) {
96+
Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
97+
message.setSubject(buildSubject(errorMessages));
98+
message.setHtmlBody(buildHtmlBody(sobjectType, errorMessages));
99+
100+
if (notification.UserId != null) {
101+
message.setTargetObjectId(notification.UserId);
102+
message.setSaveAsActivity(false);
103+
} else if (String.isNotBlank(notification.Email) == true) {
104+
message.setToAddresses(new List<String>{ notification.Email });
105+
}
106+
messages.add(message);
107+
}
108+
sendEmail(messages);
97109
}
98110

99111
private static List<String> getErrorMessages(List<Database.SaveResult> saveResults) {
@@ -120,24 +132,27 @@ public without sharing class LoggerEmailSender {
120132
return errorMessages;
121133
}
122134

123-
private static void sendEmail(Messaging.SingleEmailMessage message) {
124-
SENT_EMAILS.add(message);
135+
private static void sendEmail(List<Messaging.SingleEmailMessage> messages) {
136+
SENT_EMAILS.addAll(messages);
125137
if (IS_EMAIL_DELIVERABILITY_AVAILABLE) {
126-
List<Messaging.SingleEmailMessage> messages = new List<Messaging.SingleEmailMessage>{ message };
127-
List<Messaging.SendEmailResult> emailResults = Messaging.sendEmail(messages);
138+
List<Messaging.SendEmailResult> emailResults = System.Messaging.sendEmail(messages);
128139

129140
if (LoggerParameter.ENABLE_SYSTEM_MESSAGES == false) {
130141
return;
131-
} else if (emailResults.get(0).success) {
132-
Logger.info('Logger - The email was sent successfully');
142+
} else if (emailResults.get(0).success == true) {
143+
// One of a few limited places in the codebase (except tests) that should use System.debug()
144+
// The rest of the codebase should use a method in Logger.cls
145+
System.debug(System.LoggingLevel.INFO, 'Nebula Logger - The email was sent successfully'); // NOPMD
133146
} else {
134-
Logger.warn('Logger - The email failed to send: ' + emailResults.get(0).errors.get(0).message);
147+
// One of a few limited places in the codebase (except tests) that should use System.debug()
148+
// The rest of the codebase should use a method in Logger.cls
149+
System.debug(System.LoggingLevel.WARN, 'Nebula Logger - The email failed to send: ' + emailResults.get(0).errors.get(0).message); // NOPMD
135150
}
136151
}
137152
}
138153

139154
private static String buildSubject(List<String> errorMessages) {
140-
String emailSubjectTemplate = 'Logger - Error Notification - {0} ({1})';
155+
String emailSubjectTemplate = 'Nebula Logger - Error Notification - {0} ({1})';
141156
List<Object> emailSubjectInputs = new List<Object>{
142157
LoggerEngineDataSelector.getInstance().getCachedOrganization().Name,
143158
LoggerEngineDataSelector.getInstance().getCachedOrganization().Id
@@ -158,21 +173,13 @@ public without sharing class LoggerEmailSender {
158173
return String.format(emailBodyTemplate, emailBodyInputs);
159174
}
160175

161-
private static List<String> queryApexErrrorRecipients() {
162-
List<String> apexErrrorRecipients = new List<String>();
176+
private static List<ApexEmailNotification> queryApexErrrorRecipients() {
163177
List<ApexEmailNotification> notifications = LogManagementDataSelector.getInstance().getCachedApexEmailNotifications();
164178
if (System.Test.isRunningTest()) {
165179
notifications.clear();
166180
notifications.addAll(MOCK_NOTIFICATIONS);
167181
}
168182

169-
for (ApexEmailNotification notification : notifications) {
170-
if (notification.UserId != null) {
171-
apexErrrorRecipients.add(notification.UserId);
172-
} else if (String.isNotBlank(notification.Email)) {
173-
apexErrrorRecipients.addAll(notification.Email.split(';'));
174-
}
175-
}
176-
return apexErrrorRecipients;
183+
return notifications;
177184
}
178185
}

nebula-logger/core/main/logger-engine/classes/LogEntryEventBuilder.cls

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,7 @@ global with sharing class LogEntryEventBuilder {
774774
this.debugMessage = message;
775775
}
776776

777-
// One of few limited places in the codebase (except tests) that should use System.debug()
777+
// One of a few limited places in the codebase (except tests) that should use System.debug()
778778
// The rest of the codebase should use a method in Logger.cls
779779
System.debug(this.entryLoggingLevel, this.debugMessage);
780780
}

nebula-logger/core/main/logger-engine/classes/Logger.cls

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
global with sharing class Logger {
1616
// There's no reliable way to get the version number dynamically in Apex
1717
@TestVisible
18-
private static final String CURRENT_VERSION_NUMBER = 'v4.11.10';
18+
private static final String CURRENT_VERSION_NUMBER = 'v4.11.11';
1919
private static final System.LoggingLevel FALLBACK_LOGGING_LEVEL = System.LoggingLevel.DEBUG;
2020
private static final Set<String> IGNORED_APEX_CLASSES = initializeIgnoredApexClasses();
2121
private static final List<LogEntryEventBuilder> LOG_ENTRIES_BUFFER = new List<LogEntryEventBuilder>();
@@ -97,7 +97,7 @@ global with sharing class Logger {
9797
}
9898

9999
static {
100-
// One of few limited places in the codebase (except tests) that should use System.debug()
100+
// One of a few limited places in the codebase (except tests) that should use System.debug()
101101
// The rest of the codebase should use a method in Logger.cls
102102
System.debug(System.LoggingLevel.INFO, 'Nebula Logger - Version Number: ' + getVersionNumber());
103103
System.debug(System.LoggingLevel.INFO, 'Nebula Logger - Transaction ID: ' + getTransactionId());
@@ -3041,7 +3041,7 @@ global with sharing class Logger {
30413041
global static void setScenario(String scenario) {
30423042
if (LoggerParameter.USE_FIRST_SCENARIO_FOR_TRANSACTION == false || String.isBlank(transactionScenario)) {
30433043
transactionScenario = scenario;
3044-
// One of few limited places in the codebase (except tests) that should use System.debug()
3044+
// One of a few limited places in the codebase (except tests) that should use System.debug()
30453045
// The rest of the codebase should use a method in Logger.cls
30463046
if (String.isNotBlank(transactionScenario)) {
30473047
System.debug(System.LoggingLevel.INFO, 'Nebula Logger - Transaction Scenario: ' + transactionScenario);
@@ -3054,7 +3054,7 @@ global with sharing class Logger {
30543054

30553055
currentEntryScenario = scenario;
30563056
orderedScenarios.add(scenario);
3057-
// One of few limited places in the codebase (except tests) that should use System.debug()
3057+
// One of a few limited places in the codebase (except tests) that should use System.debug()
30583058
// The rest of the codebase should use a method in Logger.cls
30593059
if (String.isNotBlank(transactionScenario)) {
30603060
System.debug(System.LoggingLevel.INFO, 'Nebula Logger - Entry Scenario: ' + currentEntryScenario);

nebula-logger/core/main/logger-engine/lwc/logger/logger.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import { LightningElement, api } from 'lwc';
77
import { createLoggerService } from './loggerService';
88

9-
const CURRENT_VERSION_NUMBER = 'v4.11.10';
9+
const CURRENT_VERSION_NUMBER = 'v4.11.11';
1010

1111
export default class Logger extends LightningElement {
1212
#loggerService = createLoggerService();

0 commit comments

Comments
 (0)