Advertisement
Guest User

Untitled

a guest
Sep 8th, 2017
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.53 KB | None | 0 0
  1. <apex:page renderAs="pdf" standardController="Contract">
  2. <apex:detail relatedList="true" title="true"/>
  3. <div style='clear:right;margin-top:50px'>
  4. <div style='float:right'>____________________________________</div>
  5. <div style='float:right'>By:</div>
  6. </div>
  7. <div style='clear:right;margin-top:50px'>
  8. <div style='float:right'>____________________________________</div>
  9. <div style='float:right'>Date Signed:</div>
  10. </div>
  11. </apex:page>
  12.  
  13. public with sharing class SendToDocuSignController {
  14. private final Contract contract;
  15.  
  16. public String envelopeId {get;set;}
  17. private String accountId = '';
  18. private String userId = '';
  19. private String password = '';
  20. private String integratorsKey = '';
  21. private String webServiceUrl
  22. = 'https://demo.docusign.net/api/3.0/dsapi.asmx';
  23.  
  24. public SendToDocuSignController(ApexPages.StandardController controller)
  25. {
  26. this.contract = [select Id, CustomerSignedId, AccountId, ContractNumber
  27. from Contract where id = :controller.getRecord().Id];
  28. envelopeId = 'Not sent yet';
  29.  
  30. SendNow();
  31. }
  32.  
  33. public void SendNow()
  34. {
  35. DocuSignAPI.APIServiceSoap dsApiSend
  36. = new DocuSignAPI.APIServiceSoap();
  37. dsApiSend.endpoint_x = webServiceUrl;
  38.  
  39. //Set Authentication
  40. String auth = '<DocuSignCredentials><Username>'+ userId
  41. +'</Username><Password>' + password
  42. + '</Password><IntegratorKey>' + integratorsKey
  43. + '</IntegratorKey></DocuSignCredentials>';
  44. System.debug('Setting authentication to: ' + auth);
  45.  
  46. dsApiSend.inputHttpHeaders_x = new Map<String, String>();
  47. dsApiSend.inputHttpHeaders_x.put('X-DocuSign-Authentication',
  48. auth);
  49.  
  50. DocuSignAPI.Envelope envelope = new DocuSignAPI.Envelope();
  51. envelope.Subject = 'Please Sign this Contract: '
  52. + contract.ContractNumber;
  53. envelope.EmailBlurb = 'This is my new eSignature service,'+
  54. ' it allows me to get your signoff without having to fax, ' +
  55. 'scan, retype, refile and wait forever';
  56. envelope.AccountId = accountId;
  57.  
  58.  
  59. // Render the contract
  60. System.debug('Rendering the contract');
  61. PageReference pageRef = new PageReference('/apex/RenderContract');
  62. pageRef.getParameters().put('id',contract.Id);
  63. Blob pdfBlob = pageRef.getContent();
  64.  
  65. // Document
  66. DocuSignAPI.Document document = new DocuSignAPI.Document();
  67. document.ID = 1;
  68. document.pdfBytes = EncodingUtil.base64Encode(pdfBlob);
  69. document.Name = 'Contract';
  70. document.FileExtension = 'pdf';
  71. envelope.Documents = new DocuSignAPI.ArrayOfDocument();
  72. envelope.Documents.Document = new DocuSignAPI.Document[1];
  73. envelope.Documents.Document[0] = document;
  74.  
  75. // Recipient
  76. System.debug('getting the contact');
  77. Contact contact = [SELECT email, FirstName, LastName
  78. from Contact where id = :contract.CustomerSignedId];
  79.  
  80. DocuSignAPI.Recipient recipient = new DocuSignAPI.Recipient();
  81. recipient.ID = 1;
  82. recipient.Type_x = 'Signer';
  83. recipient.RoutingOrder = 1;
  84. recipient.Email = contact.Email;
  85. recipient.UserName = contact.FirstName + ' ' + contact.LastName;
  86.  
  87. // This setting seems required or you see the error:
  88. // "The string '' is not a valid Boolean value.
  89. // at System.Xml.XmlConvert.ToBoolean(String s)"
  90. recipient.RequireIDLookup = false;
  91.  
  92. envelope.Recipients = new DocuSignAPI.ArrayOfRecipient();
  93. envelope.Recipients.Recipient = new DocuSignAPI.Recipient[1];
  94. envelope.Recipients.Recipient[0] = recipient;
  95.  
  96. // Tab
  97. DocuSignAPI.Tab tab1 = new DocuSignAPI.Tab();
  98. tab1.Type_x = 'SignHere';
  99. tab1.RecipientID = 1;
  100. tab1.DocumentID = 1;
  101. tab1.AnchorTabItem = new DocuSignAPI.AnchorTab();
  102. tab1.AnchorTabItem.AnchorTabString = 'By:';
  103.  
  104.  
  105. DocuSignAPI.Tab tab2 = new DocuSignAPI.Tab();
  106. tab2.Type_x = 'DateSigned';
  107. tab2.RecipientID = 1;
  108. tab2.DocumentID = 1;
  109. tab2.AnchorTabItem = new DocuSignAPI.AnchorTab();
  110. tab2.AnchorTabItem.AnchorTabString = 'Date Signed:';
  111.  
  112. envelope.Tabs = new DocuSignAPI.ArrayOfTab();
  113. envelope.Tabs.Tab = new DocuSignAPI.Tab[2];
  114. envelope.Tabs.Tab[0] = tab1;
  115. envelope.Tabs.Tab[1] = tab2;
  116.  
  117. System.debug('Calling the API');
  118. try {
  119. DocuSignAPI.EnvelopeStatus es
  120. = dsApiSend.CreateAndSendEnvelope(envelope);
  121. envelopeId = es.EnvelopeID;
  122. } catch ( CalloutException e) {
  123. System.debug('Exception - ' + e );
  124. envelopeId = 'Exception - ' + e;
  125. }
  126. }
  127. }
  128.  
  129. <apex:page standardcontroller="Contract" extensions="SendToDocuSignController">
  130. <h1>Your eSignature request is being sent to DocuSign API!</h1>
  131. <hr/>
  132. <apex:form >
  133. <apex:commandButton value="Send Again!" action="{!SendNow}"/>
  134. </apex:form>
  135. <hr/>
  136. <strong>The DocuSign EnvelopeId:</strong>{!envelopeId}<br/>
  137. </apex:page>
  138.  
  139. /apex/SendToDocuSign?id={!Contract.Id}
  140.  
  141. var SourceID = "{!opp.Id}";
  142. var LA = "0";
  143. var DST = "XXXXX"; //This is the template id that you will only get from docusign portal
  144. var OCO = "Send"; //this specify that the page is automatically send to avoid clicking send from that VF Page
  145. window.open("/apex/dsfs__DocuSign_CreateEnvelope?DSEID=0&SourceID={!opp.Id}&LA=0"+"&DST="+DST+"&OCO="+OCO, '_top');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement