I am working on a setup of sending documents to be signed from Salesforce to Docusign, via their APEX toolkit. At this time I am doing almost a straight copy of the example code they have at the following url:


The code is failing in Section 2. Here is my small rework of the code they have presented. I have only changed how the document is added to the envelope, as it isn't stored linked to a contact, like their example.

public PageReference Send_Envelope(){
        //Step 2, Start of the document signing process now
        quoteId = ApexPages.currentPage().getParameters().get('id');
        // Getting contact and signer info
        List<Contact> contacts = [SELECT Id,Name,Phone,Email FROM Contact where Id IN (:quoteDetail[0].AccountContact__c,:quoteDetail[0].Co_Signer__c)];
        // Build a list of offer envelopes to send
        List<dfsle.Envelope> envelopes = new List<dfsle.Envelope>();
        for (Contact c : contacts) {
            system.debug('contacts Name = ' + c.Name);
            // Get an empty envelope with org defaults
            envelopes.add(dfsle.EnvelopeService.getEmptyEnvelope(new dfsle.Entity(c.Id))
                          // Customize the email subject and message
                          .withEmail('Quote for ' + c.Name, c.Name + ', please review and sign the enclosed Quote.')
                          // Add linked offer letter to envelope
                          .withDocuments(dfsle.DocumentService.getDocuments(Document.getSObjectType(), new Set<Id> { attachId }))
                          // Add the contact as a recipient
                              new Set<Id> { c.Id })));
        // Send the envelopes and determine which envelopes were sent successfully
        // Filter out unsuccessfully sent envelopes
        List<Contact> contactsToUpdate = new List<Contact>();
        List<dfsle.Envelope> sentEnvelopes = new List<dfsle.Envelope>();
        for (dfsle.Envelope envelope : dfsle.EnvelopeService.sendEnvelopes(envelopes)) {
            if (envelope.error == null) {
                // Envelope sent successfully
                contactsToUpdate.add(new Contact(
                    Id = envelope.source.id,
                    Department = 'Sent'));
            } else {
                // Handle send error
                System.debug('Envelope error: ' + envelope.error);
        return new PageReference('/'+quoteId);

When this code runs I get the following error message

Attempt to de-reference a null object Error is in expression '{!Send_Envelope}' in page docusignquote_202008_vp: >Class.Print_Docusign.Send_Envelope: line 82, column 1

An unexpected error has occurred. Your development organization has been notified..

Line 82 happens to be the following.


I don't see why this might be happening. While I believe this is a Docusign function, I assume there is something wrong with my code. I don't see it so far. I know that the 'contacts' array has entries in it. The same function seems to be working fine for the document process. Is there something I am missing in this?

  • Are you sure that the Docusign method accepts Schema.SObjectType? You'll need to check with Docusign support resources. Jan 19 at 0:37

So I have finally figured this out. Ton of reading in the apex guide and a bit of insight based on that. I believe that there was an error in the code that was published by the Docusign website. That line 82 was the following on the URL that I posted.


I got it work with this line of code instead


It needed a field to check to get the Object type of, i.e., in this case the C.Id field is part of a SOBject type for a contact record. 'Contact' wasn't valid for that code, C.Id is a valid field for that code. It both compiled clean and the code ran and did what was expected. woot woot

Not the answer you're looking for? Browse other questions tagged or ask your own question.