Question

Apex: Field Update not happening based on envelope status in salesforce

  • 29 April 2024
  • 1 reply
  • 79 views

Badge

I am sending document using apex. Sending and signing is happening properly also, document is attaching back to salesforce record once signing is completed by all parties.

I also want to update field in salesforce record based on envelope status. Like this:

Envelope status ='Sent' then Document__c.Envelope_Status__c='Sent'

Envelope status ='Completed' then Document__c.Envelope_Status__c='Completed'.

I am giving these conditions in code like below:

 

 myEnvelope = myEnvelope.withOptions(
                new dfsle.Envelope.Options(
                    null, 
                    new dfsle.Document.WriteBack(
                        recordId, 
                        dfsle.Document.WRITE_BACK_NAME_ENVELOPE_STATUS_PDF, 
                        true, 
                        false  
                    ), 
                    
                    new Map<String, Map<String, Object>>{
                        dfsle.Envelope.STATUS_SENT => new Map<String, Object>{
                            'Document__c.DSEnvelope_Status__c'=>'Sent'
                                },
                                    dfsle.Envelope.STATUS_COMPLETED => new Map<String, Object>{
                                        'Document__c.DSEnvelope_Status__c'=>'Completed'
                                            }
                    },
                    
                    new Map<String,Map<String, Object>>{
                        dfsle.Recipient.STATUS_DECLINED => new map<String,Object>{
                            'Document__c.DSEnvelope_Status__c' =>'Declined'
                                }
                    }
                )
            );
            
            The problem is I am always getting blank value in DSEnvelope_Status__c field in custom object Document__c record(Source record). I am testing with system admin profile.


1 reply

Userlevel 1
Badge +4

Hello Team

 

Could you please share with the complete code to test it out? I have tried the other way & it works as expected.

Please find the below:

 

public class ApexWritebackDate {
    public static void sendEnvelopeMethod(){
        Id myopportunityId = ''; // The ID of the initiating Salesforce object.
         //Id myFileId = [SELECT Id from ContentVersion where ContentDocumentId = :myDocumentId LIMIT 1].Id; // Content version ID of document to send
      //Find an Opportunity
       Opportunity myOpportunity = [SELECT Id, Amount FROM Opportunity WHERE Name = 'WRITE_BACK_ENVELOPE_STATUS_PDF' LIMIT 1];
       //Find your contact to add
       //Contact myContact = [SELECT Id, Name, Email FROM Contact WHERE Name = 'Green Beard' LIMIT 1];      
        // Create an empty envelope.
        dfsle.Envelope myEnvelope = dfsle.EnvelopeService.getEmptyEnvelope(
            new dfsle.Entity(myOpportunity.Id));
        //use the Recipient.fromSource method to create the Recipient
        dfsle.Recipient myRecipient = dfsle.Recipient.fromSource(
            '', // Recipient name
            '@gmail.com', // Recipient email
            null, //Optional phone number
            'Created By', //Role Name. Specify the exact role name from template
            null); //source object for the Recipient
        //define a merge filed
          dfsle.Tab.MergeField myMergeField = new dfsle.Tab.MergeField('opportunity.description',null,null,true,false);
        
       //Add a  text tab        
        dfsle.Tab t = new dfsle.TextTab()
            .withMergeField(myMergeField) //map tab to the mergeField
            //.withValue('Happiness') //For MergeFields values are pulled from Salesforce
            .withReadOnly(true) //true = read only or locked
            //set the tab on the anchor string Cool, 40 pixels right and -5 pixels up.
            //.withAnchor(new dfsle.Tab.Anchor( 'Cool', true, true, null, true, true, 'pixels', 80, -5))        
            .withDataLabel('Description');
        myRecipient = myRecipient.withTabs(new List<dfsle.Tab> {t});
        //add Recipient to the Envelope
        myEnvelope = myEnvelope.withRecipients(new List<dfsle.Recipient> { myRecipient });
      //myTemplateId contains the DocuSign Id of the DocuSign Template
        dfsle.UUID myTemplateId = dfsle.UUID.parse('7b43080a'); //OpptyMergeField
        //create a new document for the Envelope
        dfsle.Document myDocument = dfsle.Document.fromTemplate(
            myTemplateId, // templateId in dfsle.UUID format
            'myTemplate'); // name of the template
      
       //Add the document to the envelope
        //myEnvelope = myEnvelope.withDocuments(dfsle.DocumentService.getDocuments(ContentVersion.getSObjectType(), new Set<Id> { myFileId }));
          myEnvelope = myEnvelope.withDocuments(new List<dfsle.Document> {myDocument});
            myEnvelope.withOptions(new dfsle.Envelope.Options(
  // Whether to populate auto-place tags for default roles
  false,
  new dfsle.Document.WriteBack(
    // Where to link the completed document(s)
   myopportunityId ,
    // The completed document(s) file name format
    dfsle.Document.WRITE_BACK_ENVELOPE_STATUS_PDF,
    // Whether to combine all the documents into a single PDF
    true,
    // Whether to include the Certificate of Completion (CoC)
    true),
  // Envelope status event updates
  new Map<String, Map<String, Object>> {
    // Set Opportunity.StageName to "Prospecting" upon completion
    dfsle.Envelope.STATUS_COMPLETED => new Map<String, Object> {
      'Opportunity.StageName' => 'Perception Analysis',
                'Opportunity.CloseDate' => Date.newInstance(2023, 11, 17)
    }
  },
  // Recipient status event updates
  new Map<String, Map<String, Object>>()));
        myEnvelope = dfsle.EnvelopeService.sendEnvelope(
           myEnvelope, // The envelope to send
           true);
    }
}
 

 

Thanks

Sai

Reply