You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

DocuSign Gen Salesforce:使用Apex代码生成文档

DocuSign Gen Salesforce:使用Apex代码生成文档

嘿,很高兴能帮到你!你提到的需求完全可以实现——用Apex代码基于DocuSign模板为Salesforce账户生成文档,而不仅仅是创建信封。之前你看到的信封相关代码是DocuSign eSignature的功能,而我们需要用到DocuSign Gen(专门用于文档生成的工具)的Apex API,下面我给你梳理具体步骤和代码示例:

前提准备

  • 确保你的Salesforce org已经集成了DocuSign Gen,并且已经创建好关联账户字段的模板(模板里要设置好对应账户数据的占位符)。
  • 给运行代码的用户分配DocuSign Gen的权限集,确保Apex类有调用dsfs__DocuSignGenUtility的权限。

核心Apex代码示例

// 查询目标账户记录(替换成你的账户ID)
Account targetAccount = [SELECT Id, Name, BillingStreet, BillingCity, BillingState, BillingPostalCode 
                         FROM Account WHERE Id = '001XXXXXXXXXXXXXXX' LIMIT 1];

// 替换为你的DocuSign Gen模板ID
String templateId = 'a1b2c3d4-xxxx-xxxx-xxxx-xxxxxxxxx';

// 准备数据映射:键名要和模板中的占位符完全匹配
Map<String, Object> mergeData = new Map<String, Object>{
    'AccountName' => targetAccount.Name,
    'FullAccountAddress' => targetAccount.BillingStreet + ', ' + targetAccount.BillingCity + ', ' + targetAccount.BillingState + ' ' + targetAccount.BillingPostalCode
    // 可以添加更多字段,对应模板里的其他占位符
};

try {
    // 调用DocuSign Gen的生成文档API
    dsfs__DocuSignGenUtility.GenerateDocumentResult docResult = dsfs__DocuSignGenUtility.generateDocument(
        templateId,
        mergeData,
        targetAccount.Id, // 将生成的文档关联到该账户记录
        'Account Official Document - ' + targetAccount.Name, // 生成的文档名称
        true // 设置为true会自动把文档附加到账户记录的相关列表中
    );

    if(docResult.isSuccess()) {
        System.debug('文档生成成功!ContentDocument ID:' + docResult.getDocumentId());
        // 这里可以添加后续逻辑,比如给用户发通知、更新账户状态等
    } else {
        System.debug('文档生成失败,错误信息:' + docResult.getErrorMessage());
    }
} catch(Exception e) {
    System.debug('生成文档时出现异常:' + e.getMessage());
}

关键注意点

  • 模板中的占位符名称必须和mergeData里的键名完全一致,否则数据无法正确填充到文档中。
  • 如果需要将生成的文档直接用于签名流程,可以在文档生成成功后,调用DocuSign eSignature的API创建信封并附加该文档。
  • 通过docResult.getDocumentId()可以获取生成的ContentDocument的ID,后续可以用来实现下载、分享等操作。

如果还有模板配置、权限或者API细节方面的疑问,随时再问!

备注:内容来源于stack exchange,提问作者FlipperDev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.22 07:17:58