-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmailTestControllerTest.apxc
More file actions
156 lines (127 loc) · 5.07 KB
/
EmailTestControllerTest.apxc
File metadata and controls
156 lines (127 loc) · 5.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
public with sharing class EmailTestController {
@AuraEnabled(cacheable=true)
public static List<OptionWrapper> getEmailTemplates() {
List<OptionWrapper> options = new List<OptionWrapper>();
for (EmailTemplate templateRecord : [
SELECT Id, Name, RelatedEntityType
FROM EmailTemplate
WHERE IsActive = TRUE
AND UiType = 'SFX'
ORDER BY Name
]) {
options.add(new OptionWrapper(
(String) templateRecord.Id,
templateRecord.Name
));
}
return options;
}
@AuraEnabled(cacheable=true)
public static String getTargetObjectType(Id templateId) {
if (templateId == null) {
throw new AuraHandledException('Template Id is required.');
}
EmailTemplate templateRecord = [
SELECT Id, RelatedEntityType
FROM EmailTemplate
WHERE Id = :templateId
LIMIT 1
];
return templateRecord.RelatedEntityType;
}
@AuraEnabled(cacheable=true)
public static List<OptionWrapper> getRecords(String objectType) {
if (String.isBlank(objectType)) {
throw new AuraHandledException('Object type is required.');
}
Map<String, Schema.SObjectType> globalDescribe = Schema.getGlobalDescribe();
if (!globalDescribe.containsKey(objectType)) {
throw new AuraHandledException('Unsupported object type: ' + objectType);
}
Schema.DescribeSObjectResult describeResult =
globalDescribe.get(objectType).getDescribe();
Map<String, Schema.SObjectField> fieldMap = describeResult.fields.getMap();
String labelField = getDisplayField(objectType, fieldMap);
String soql =
'SELECT Id, ' + labelField +
' FROM ' + objectType +
' ORDER BY CreatedDate DESC LIMIT 100';
List<SObject> records = Database.query(soql);
List<OptionWrapper> options = new List<OptionWrapper>();
for (SObject recordItem : records) {
String labelValue = getDisplayValue(recordItem, labelField, objectType);
options.add(new OptionWrapper(
(String) recordItem.get('Id'),
labelValue
));
}
return options;
}
@AuraEnabled
public static void sendTestEmail(Id templateId, Id recordId, String recipientEmail) {
if (templateId == null) {
throw new AuraHandledException('Template Id is required.');
}
if (recordId == null) {
throw new AuraHandledException('Record Id is required.');
}
if (String.isBlank(recipientEmail)) {
throw new AuraHandledException('Recipient email is required.');
}
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTemplateId(templateId);
mail.setToAddresses(new String[] { recipientEmail });
mail.setSaveAsActivity(false);
mail.setTreatTargetObjectAsRecipient(false);
String sObjectTypeName = recordId.getSObjectType().getDescribe().getName();
if (sObjectTypeName == 'Contact' || sObjectTypeName == 'Lead' || sObjectTypeName == 'User') {
mail.setTargetObjectId(recordId);
} else if (sObjectTypeName == 'Case') {
Case caseRecord = [
SELECT Id, ContactId
FROM Case
WHERE Id = :recordId
LIMIT 1
];
if (caseRecord.ContactId == null) {
throw new AuraHandledException(
'The selected Case does not have a Contact. A Contact is required to render this email template for testing.'
);
}
mail.setTargetObjectId(caseRecord.ContactId);
mail.setWhatId(caseRecord.Id);
} else {
throw new AuraHandledException(
'Testing is currently supported only for Contact, Lead, User, and Case-related templates.'
);
}
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
private static String getDisplayField(
String objectType,
Map<String, Schema.SObjectField> fieldMap
) {
if (fieldMap.containsKey('Name')) {
return 'Name';
}
if (objectType == 'Case' && fieldMap.containsKey('CaseNumber')) {
return 'CaseNumber';
}
if (fieldMap.containsKey('Subject')) {
return 'Subject';
}
if (fieldMap.containsKey('DeveloperName')) {
return 'DeveloperName';
}
throw new AuraHandledException(
'No supported display field was found for object type ' + objectType + '.'
);
}
private static String getDisplayValue(SObject recordItem, String labelField, String objectType) {
Object rawValue = recordItem.get(labelField);
if (rawValue != null) {
return String.valueOf(rawValue);
}
return objectType + ' - ' + String.valueOf(recordItem.get('Id'));
}
}