Advertisement
Guest User

Untitled

a guest
Oct 12th, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.13 KB | None | 0 0
  1. public String envelopeId {get;set;}
  2. private String accountId = '7afed6db-0e61-4855-9573-4a75173ccd13';
  3. private String userId = 'c20a0a11-d623-4d1f-89dc-fc8e10fd5a28';
  4. private String password = 'madanamoni14';
  5. private String integratorsKey = '1630d186-efe9-497f-8ad2-dce2fb35b1bc';
  6. private String webServiceUrl = 'https://demo.docusign.net/api/3.0/dsapi.asmx';
  7.  
  8. public SendToDocuSignController (ApexPages.StandardController controller)
  9. {
  10. this.contract = [select Id,CustomerSignedId,AccountId,ContractNumber from Contract where id = :controller.getRecord().Id];
  11. envelopeId = 'Not sent yet';
  12.  
  13. SendNow();
  14. }
  15.  
  16. public void SendNow()
  17. {
  18. DocuSignAPI.APIServiceSoap dsApiSend = new DocuSignAPI.APIServiceSoap();
  19. dsApiSend.endpoint_x = webServiceUrl;
  20.  
  21. //Set Authentication
  22. String auth = '<DocuSignCredentials><Username>'+ userId
  23. +'</Username><Password>' + password
  24. + '</Password><IntegratorKey>' + integratorsKey
  25. + '</IntegratorKey></DocuSignCredentials>';
  26. System.debug('Setting authentication to: ' + auth);
  27.  
  28. dsApiSend.inputHttpHeaders_x = new Map<String, String>();
  29. dsApiSend.inputHttpHeaders_x.put('X-DocuSign-Authentication',
  30. auth);
  31.  
  32. DocuSignAPI.Envelope envelope = new DocuSignAPI.Envelope();
  33. envelope.Subject = 'Please Sign this Contract: ' + contract.ContractNumber;
  34. envelope.EmailBlurb = 'This is my new eSignature service, it allows me to get your signoff without having to fax, scan, retype, refile and wait forever';
  35. envelope.AccountId = accountId;
  36.  
  37.  
  38. // Render the contract
  39. System.debug('Rendering the contract');
  40. PageReference pageRef = new PageReference('/apex/RenderContract');
  41. pageRef.getParameters().put('id',contract.Id);
  42. Blob pdfBlob = pageRef.getContent();
  43.  
  44. // Document
  45. DocuSignAPI.Document document = new DocuSignAPI.Document();
  46. document.ID = 1;
  47. document.pdfBytes = EncodingUtil.base64Encode(pdfBlob);
  48. document.Name = 'Contract';
  49. document.FileExtension = 'pdf';
  50. envelope.Documents = new DocuSignAPI.ArrayOfDocument();
  51. envelope.Documents.Document = new DocuSignAPI.Document[1];
  52. envelope.Documents.Document[0] = document;
  53.  
  54. // Recipient
  55. System.debug('getting the contact');
  56. Contact contact = [SELECT email, FirstName, LastName from Contact where id = :contract.CustomerSignedId];
  57. System.debug('got ' + contact.email + ' ' + contact.FirstName + ' ' + contact.LastName);
  58.  
  59. System.debug('Building up the recipient');
  60. DocuSignAPI.Recipient recipient = new DocuSignAPI.Recipient();
  61. recipient.ID = 1;
  62. recipient.Type_x = 'Signer';
  63. recipient.RoutingOrder = 1;
  64. recipient.Email = contact.Email;
  65. recipient.UserName = contact.FirstName + ' ' + contact.LastName;
  66.  
  67. // This setting seems required or you see the error:
  68. // "The string '' is not a valid Boolean value. at System.Xml.XmlConvert.ToBoolean(String s)"
  69. recipient.RequireIDLookup = false;
  70.  
  71. envelope.Recipients = new DocuSignAPI.ArrayOfRecipient();
  72. envelope.Recipients.Recipient = new DocuSignAPI.Recipient[1];
  73. envelope.Recipients.Recipient[0] = recipient;
  74.  
  75. // Tab
  76. DocuSignAPI.Tab tab1 = new DocuSignAPI.Tab();
  77. tab1.Type_x = 'SignHere';
  78. tab1.RecipientID = 1;
  79. tab1.DocumentID = 1;
  80. tab1.AnchorTabItem = new DocuSignAPI.AnchorTab();
  81. tab1.AnchorTabItem.AnchorTabString = 'By:';
  82. tab1.AnchorTabItem.XOffset = 100;
  83.  
  84.  
  85. DocuSignAPI.Tab tab2 = new DocuSignAPI.Tab();
  86. tab2.Type_x = 'DateSigned';
  87. tab2.RecipientID = 1;
  88. tab2.DocumentID = 1;
  89. tab2.AnchorTabItem = new DocuSignAPI.AnchorTab();
  90. tab2.AnchorTabItem.AnchorTabString = 'Date Signed:';
  91.  
  92. envelope.Tabs = new DocuSignAPI.ArrayOfTab();
  93. envelope.Tabs.Tab = new DocuSignAPI.Tab[2];
  94. envelope.Tabs.Tab[0] = tab1;
  95. envelope.Tabs.Tab[1] = tab2;
  96.  
  97. System.debug('Calling the API');
  98. try {
  99. DocuSignAPI.EnvelopeStatus es = dsApiSend.CreateAndSendEnvelope(envelope);
  100. envelopeId = es.EnvelopeID;
  101. System.debug('Returned successfully, envelope id = ' + envelopeId );
  102. } catch ( CalloutException e) {
  103. System.debug('Exception - ' + e );
  104. envelopeId = 'Exception - ' + e;
  105. }
  106.  
  107. }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement