[SalesForce] DocuSign for Salesforce: How to attach completed Documents to parent object to the object that the Document was sent from

I have DocuSign set up to send from Quotes within Salesforce. However, once the document has been completed, I would like the Completed documents to be attached to the Opportunity record that is the parent of the Quote object.

I have tried configuring the Connect settings within DocuSign Admin but currently having no luck.

If I want to update the documents to the Opportunity, I don't have a field on the Opportunity that matches a reference on the Envelope as the Envelope just has the Quote details and there can be multiple Quotes for 1 Opportunity so I can't get this ID on the Opportunity level.

Has anyone had any luck with this previously?

Best Answer

DocuSign for Salesforce supports attachment of completed documents to the parent object:

To move the attachment from the parent object to that object's parent, you can create a trigger on the Attachment object:

trigger AttachmentTrigger on Attachment(after insert){

    Set<Id> parentIds = new Set<Id>();
    List<Attachment> quoteAttachments = new List<Attachment>();

    for(Attachment a : trigger.new) {
        // Pull attachments only related to Quotes
        if(a.ParentId.getSObjectType() == Schema.Quote.getSObjectType()){
            quoteAttachments.add(a);
            // Get the Quote Ids
            parentIds.add(a.ParentId);
        }
    }

    // Query for Quote Opportunity Ids
    Map<Id,Quote> quotes = new Map<Quote>();
    for(Quote q : [SELECT OpportunityId FROM Quote WHERE Id IN :parentIds ]){
        quotes.put(q.Id,q);
    }    

    // Change Parent Id on Attachments
    for(Attachment a : quoteAttachments) {
        a.ParentId = quotes.get(q.Id).OpportunityId;
    }

}

Attachments are tricky because they have so little metadata. If you need to identify a specific document and your document names are consistent, you could filter on attachment name.

Otherwise, if you need to ensure you're only working with attachments on the quote that came from DocuSign, you may want to build a trigger on the DocuSign envelope status, and work from there.

Related Topic