Apex调用Messaging.renderStoredEmailTemplate发送DocuSign邮件正文为空
解决Apex调用DocuSign发送邮件正文空白问题
可能的原因及对应解决方案
1. 未同时提供纯文本与HTML正文
DocuSign的withEmail方法若只传入HTML正文,部分场景下会出现解析异常导致空白。Salesforce渲染后的邮件模板同时包含HTML和纯文本内容,需同时传入两种格式:
Messaging.SingleEmailMessage renderResult = Messaging.renderStoredEmailTemplate( opportunityEmailTemplateId, opportunityId_i, myContact.Id ); // 同时获取HTML和纯文本正文 String htmlBody = renderResult.getHtmlBody(); String plainTextBody = renderResult.getPlainTextBody(); // 使用重载方法传入双格式内容 myEnvelope = myEnvelope.withEmail( opportunityOfferEmailSubject, plainTextBody, // 纯文本正文 htmlBody // HTML正文 ); myEnvelope = dfsle.EnvelopeService.sendEnvelope( myEnvelope, true );
2. 清理HTML冗余外层标签
Salesforce渲染的邮件模板会包含<html>、<head>、DOCTYPE等外层结构,DocuSign仅需<body>内的核心内容,可提取后传入:
Messaging.SingleEmailMessage renderResult = Messaging.renderStoredEmailTemplate( opportunityEmailTemplateId, opportunityId_i, myContact.Id ); String fullHtml = renderResult.getHtmlBody(); // 提取<body>标签内的内容,兼容带属性的<body>标签 String cleanHtml = fullHtml != null ? fullHtml.substringAfter('<body').substringAfter('>').substringBefore('</body>') : ''; myEnvelope = myEnvelope.withEmail( opportunityOfferEmailSubject, renderResult.getPlainTextBody(), cleanHtml ); myEnvelope = dfsle.EnvelopeService.sendEnvelope( myEnvelope, true );
3. 处理HTML转义字符
渲染后的HTML可能存在特殊字符转义(如&替代&),可通过反转义确保内容正常解析:
String cleanHtml = ApexPages.unescapeHtml4(fullHtml.substringAfter('<body').substringAfter('>').substringBefore('</body>'));
4. 验证信封邮件配置
发送前打印信封的邮件参数,确认内容未被意外清空:
System.debug('Envelope Subject: ' + myEnvelope.emailSubject); System.debug('Envelope HTML Body: ' + myEnvelope.emailHtmlBody); System.debug('Envelope Plain Text Body: ' + myEnvelope.emailPlainTextBody);
内容的提问来源于stack exchange,提问作者user2018
相关产品推荐
相关产品推荐

