Fix email subject to use combined template Short Names

- Query Short_Name__c field from dfsle__EnvelopeConfiguration__c
- Use short names for email subject (fallback to full names if blank)
- Custom emailSubject still overrides if provided
- Always truncate subject to 100 characters max
- Fixes issue where selecting all templates created too-long subject
This commit is contained in:
Paul Huliganga 2026-03-03 18:51:42 -05:00
parent 90c65ea2c6
commit 32369c5939
2 changed files with 26 additions and 13 deletions

View File

@ -48,18 +48,23 @@ global with sharing class DocusignCompositeEnvelopeBuilder {
List<String> sortedTemplateIds = new List<String>(new Set<String>(req.templateIds));
sortedTemplateIds.sort();
// Query template names for document labels (shows in Docusign Status)
// Query template names and short names for document labels and email subject
Map<String, String> templateNames = new Map<String, String>();
Map<String, String> templateShortNames = new Map<String, String>();
for (dfsle__EnvelopeConfiguration__c config : [
SELECT dfsle__DocuSignId__c, Name
SELECT dfsle__DocuSignId__c, Name, Short_Name__c
FROM dfsle__EnvelopeConfiguration__c
WHERE dfsle__DocuSignId__c IN :sortedTemplateIds
]) {
templateNames.put(config.dfsle__DocuSignId__c, config.Name);
if (String.isNotBlank(config.Short_Name__c)) {
templateShortNames.put(config.dfsle__DocuSignId__c, config.Short_Name__c);
}
}
List<dfsle.Document> documents = new List<dfsle.Document>();
List<String> docNames = new List<String>();
List<String> subjectNames = new List<String>();
for (String templateId : sortedTemplateIds) {
String label = templateNames.containsKey(templateId)
? stripLanguageSuffix(templateNames.get(templateId))
@ -71,6 +76,12 @@ global with sharing class DocusignCompositeEnvelopeBuilder {
)
);
docNames.add(label);
// Use short name for email subject if available, otherwise use regular name
String subjectName = templateShortNames.containsKey(templateId)
? templateShortNames.get(templateId)
: label;
subjectNames.add(subjectName);
}
myEnvelope = myEnvelope.withDocuments(documents);
@ -93,13 +104,15 @@ global with sharing class DocusignCompositeEnvelopeBuilder {
List<dfsle.Recipient> recipients = resolveRecipients(req.recordId);
myEnvelope = myEnvelope.withRecipients(recipients);
// Set email subject if provided (truncated to 100 chars max)
if (String.isNotBlank(req.emailSubject)) {
String truncatedSubject = req.emailSubject.length() > 100
? req.emailSubject.left(97) + '...'
: req.emailSubject;
myEnvelope = myEnvelope.withEmail(truncatedSubject, '');
// Set email subject to combined template short names (or custom subject if provided)
String emailSubject = String.isNotBlank(req.emailSubject)
? req.emailSubject
: String.join(subjectNames, ', ');
// Truncate to 100 characters maximum as required by Docusign
if (emailSubject.length() > 100) {
emailSubject = emailSubject.left(97) + '...';
}
myEnvelope = myEnvelope.withEmail(emailSubject, '');
// Send the envelope
myEnvelope = dfsle.EnvelopeService.sendEnvelope(myEnvelope, true);

View File

@ -221,14 +221,14 @@ private class DocusignCompositeEnvelopeBuilderTest {
}
@isTest
static void testWithEmailSubject() {
static void testCustomEmailSubject() {
// Arrange
dfsle.TestUtils.setMock(new dfsle.ESignatureAPIMock());
DocusignEnvelopeRequest req = new DocusignEnvelopeRequest();
req.templateIds = new List<String>{'01234567-abcd-ef01-2345-6789abcdef01'};
req.recordId = '001000000ABC123';
req.emailSubject = 'Custom: Please review and sign';
req.emailSubject = 'Custom: Please review and sign'; // Custom subject overrides template names
// Act
Test.startTest();
@ -243,14 +243,14 @@ private class DocusignCompositeEnvelopeBuilderTest {
}
@isTest
static void testWithoutEmailSubject() {
static void testEmailSubjectFromTemplateNames() {
// Arrange
dfsle.TestUtils.setMock(new dfsle.ESignatureAPIMock());
DocusignEnvelopeRequest req = new DocusignEnvelopeRequest();
req.templateIds = new List<String>{'01234567-abcd-ef01-2345-6789abcdef01'};
req.recordId = '001000000ABC123';
req.emailSubject = null;
req.emailSubject = null; // No custom subject provided - should use combined template names
// Act
Test.startTest();
@ -261,7 +261,7 @@ private class DocusignCompositeEnvelopeBuilderTest {
Test.stopTest();
// Assert
System.assertEquals(true, results[0].success, 'Should succeed without subject');
System.assertEquals(true, results[0].success, 'Should succeed with auto-generated subject from template names');
}
@isTest