-->

How do I programatically void a DocuSign envelope

2019-03-02 19:22发布

问题:

I'm using the DocuSign For Salesforce app. As part of our workflow, we create envelopes when certain conditions are met, but I need to be able to void the envelope through an Apex trigger if the conditions are no longer met.

The documentation for the DocuSign app goes into detail on how to create envelopes, but doesn't mention voiding an envelope through Apex.

I know it's possible to use the API outside of Salesforce, but I'd like to keep the entire process in Salesforce if possible.

回答1:

I don't believe the DocuSign for Salesforce app has the ability to void envelopes, you can either void the envelope through the DocuSign Console or write the API call yourself to void.

If you can make an http request through Apex then you should be able to code this up easily. To void an envelope you make a PUT to the /accounts/{accountId}/envelopes/{envelopeId} URI. Here is what the call will look like:

PUT https://{server}/restapi/{apiVersion}/accounts/{accountId}/envelopes/{envelopeId}

X-DocuSign-Authentication: <DocuSignCredentials><Username>{name}</Username><Password>{password}</Password><IntegratorKey>{integrator_key}</IntegratorKey></DocuSignCredentials>
Accept: application/json
Content-Type: application/json

{
  "status":"voided",
  "voidedReason":"voided for incorrect recipient"
}


回答2:

The DocuSign for Salesforce managed package is capable of voiding envelopes. However, there are limitations on how you accomplish this.

There is a custom button created called Void Envelope when the managed package is installed. This is currently only available on the DocuSign Status records.

There is no option to programatically void an envelope within the DocuSign for Salesforce managed package.

I'm not an expert on Salesforce triggers or APEX classes, but you may be able setup a trigger or button for when your Credit Department marks a service as "no longer usable", it will redirect to the appropriate DocuSign Status record where the Void Envelope button resides.

Alternatively, you could build your own API workflow as @Ergin has suggested.



回答3:

Here's a simple client that you can use to void envelopes from apex.

public with sharing class DocusignClient {
    private static final String USERNAME = Label.docusign_username;
    private static final String PASSWORD = Label.docusign_password;    
    private static final String INTEGRATOR_KEY = label.docusign_integratorKey;
    private static final String ACCOUNT_ID = label.docusign_accountId;
    // for production use 'www.docusign.net/restapi/v2'
    // for sandbox use 'https://demo.docusign.net/restapi/v2'
    private static final String BASE_URL = label.docusign_baseurl;
    private static final String AUTH_TEMPLATE = '<DocuSignCredentials><Username>{0}</Username><Password>{1}</Password><IntegratorKey>{2}</IntegratorKey></DocuSignCredentials>';
    private static final String STATUS_VOID = 'voided';


    public HttpResponse voidEnvelope(String envelopeId, String voidReason){
        String endpoint = String.format('{0}/accounts/{1}/envelopes/{2}', new String[]{BASE_URL, ACCOUNT_ID, envelopeId});

        DocusignStatusRequest statusUpdate = new DocusignStatusRequest();
        statusUpdate.status = STATUS_VOID;
        statusUpdate.voidedReason = voidReason;

        // initialize the api client for the desired environment
        Http h = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint(endpoint);
        request.setMethod('PUT');
        request.setHeader('X-DocuSign-Authentication', String.format(AUTH_TEMPLATE, new String[]{USER_NAME,PASS_WORD,INTEGRATOR_KEY}));
        request.setBody(JSON.serialize(statusUpdate));

        return h.send(request);
    }

    public class DocusignStatusRequest{
        public String status {get; set;}
        public String voidedReason {get; set;}
    }
}