Guest User

Untitled

a guest
Dec 18th, 2017
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.02 KB | None | 0 0
  1. global class AnnualContract
  2. {
  3. webService static string AM_SendToDocuSign(String id, string strObjType)
  4. {
  5. Docusign_API_Setting__c APISetting = Docusign_API_Setting__c.getInstance('API Settings');
  6. String envelopeId = '';
  7. string DealerName = '';
  8. string DealerId = '';
  9. String accountId = APISetting.AccountId__c;
  10. String userId = APISetting.UserId__c;
  11. String password = APISetting.Password__c;
  12. String integratorsKey = APISetting.IntegratorsKey__c;
  13. String webServiceUrl = APISetting.WebServiceUrl__c;
  14.  
  15. list<Lead> lstLead = new list<Lead>();
  16. list<Contact> lstContact = new list<Contact>();
  17.  
  18. if(strObjType == 'Lead')
  19. {
  20. lstLead = [SELECT Name,Status,Email,FirstName,LastName,Owner.Name,Title,FROM Lead where id = : Id limit 1];
  21. }
  22.  
  23. StaticResource objSR = [SELECT Id,name, SystemModStamp FROM StaticResource WHERE Name = 'AnnualContractPDF' LIMIT 1];
  24.  
  25. String url_file_ref = '/resource/' + String.valueOf(((DateTime)objSR.get('SystemModStamp')).getTime())+ '/' + objSR.get('Name');
  26. if(strObjType == 'Lead')
  27. {
  28. DealerName = lstLead[0].Name;
  29. }
  30.  
  31. DocuSignAPI.APIServiceSoap dsApiSend = new DocuSignAPI.APIServiceSoap();
  32. dsApiSend.endpoint_x = webServiceUrl;
  33.  
  34. //Set Authentication
  35. String auth = '<DocuSignCredentials><Username>'+ userId
  36. +'</Username><Password>' + password
  37. + '</Password><IntegratorKey>' + integratorsKey
  38. + '</IntegratorKey></DocuSignCredentials>';
  39. System.debug('Setting authentication to: ' + auth);
  40.  
  41. dsApiSend.inputHttpHeaders_x = new Map<String, String>();
  42. dsApiSend.inputHttpHeaders_x.put('X-DocuSign-Authentication',
  43. auth);
  44.  
  45. DocuSignAPI.Envelope envelope = new DocuSignAPI.Envelope();
  46. envelope.Subject = 'Please Sign this Contract' + lstLead[0].Name;
  47. 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';
  48. envelope.AccountId = accountId;
  49.  
  50. // Render the contract
  51. System.debug('Rendering the contract');
  52. PageReference pageRef = new PageReference(url_file_ref);
  53. Blob pdfBlob = pageRef.getContent();
  54.  
  55. DocuSignAPI.CustomField field = new DocuSignAPI.CustomField ();
  56. field.Name = '##SFLead';
  57. field.Value = lstLead[0].Id; //value of the external source Id
  58. field.Show = 'false';
  59. field.CustomFieldType = 'Text';
  60. envelope.CustomFields = new DocuSignAPI.ArrayOfCustomField();
  61. envelope.CustomFields.CustomField = new DocuSignAPI.CustomField[1];
  62. envelope.CustomFields.CustomField[0] = field;
  63.  
  64. // Document
  65. DocuSignAPI.Document document = new DocuSignAPI.Document();
  66. document.ID = 1;
  67. document.pdfBytes = EncodingUtil.base64Encode(pdfBlob);
  68. document.Name = 'Annual Contract';
  69. document.FileExtension = 'pdf';
  70. envelope.Documents = new DocuSignAPI.ArrayOfDocument();
  71. envelope.Documents.Document = new DocuSignAPI.Document[1];
  72. envelope.Documents.Document[0] = document;
  73.  
  74. // Recipient
  75. System.debug('Building up the recipient');
  76. DocuSignAPI.Recipient recipient = new DocuSignAPI.Recipient();
  77. recipient.ID = 1;
  78. recipient.Type_x = 'Signer';
  79. recipient.RoutingOrder = 1;
  80. recipient.Email = lstLead[0].Email;
  81. recipient.UserName = lstLead[0].FirstName + ' ' + lstLead[0].LastName;
  82.  
  83. recipient.RequireIDLookup = false;
  84.  
  85. envelope.Recipients = new DocuSignAPI.ArrayOfRecipient();
  86. envelope.Recipients.Recipient = new DocuSignAPI.Recipient[1];
  87. envelope.Recipients.Recipient[0] = recipient;
  88.  
  89. // Tab
  90. DocuSignAPI.Tab tab1 = new DocuSignAPI.Tab();
  91. tab1.Type_x = 'SignHere';
  92. tab1.RecipientID = 1;
  93. tab1.DocumentID = 1;
  94. tab1.AnchorTabItem = new DocuSignAPI.AnchorTab();
  95. tab1.AnchorTabItem.AnchorTabString = '/t1/';
  96. tab1.AnchorTabItem.XOffset = 100;
  97.  
  98.  
  99. DocuSignAPI.Tab tab2 = new DocuSignAPI.Tab();
  100. tab2.Type_x = 'DateSigned';
  101. tab2.RecipientID = 1;
  102. tab2.DocumentID = 1;
  103. tab2.AnchorTabItem = new DocuSignAPI.AnchorTab();
  104. tab2.AnchorTabItem.AnchorTabString = '/d1/';
  105.  
  106. DocuSignAPI.Tab tab3 = new DocuSignAPI.Tab();
  107. tab3.CustomTabType = 'Text';
  108. tab3.Name = 'Title';
  109. tab3.Type_x = 'Custom';
  110. tab3.RecipientID = 1;
  111. tab3.DocumentID = 1;
  112. tab3.TabLabel = 'Title';
  113. if(strObjType == 'Lead')
  114. {
  115. if(lstLead[0].Title != null)
  116. {
  117. tab3.Value = ''+lstLead[0].Title+'';
  118. }
  119. }
  120. else
  121. {
  122. if(lstContact[0].Title != null)
  123. {
  124. tab3.Value = ''+lstContact[0].Title+'';
  125. }
  126. }
  127. tab3.CustomTabWidth=100;
  128. tab3.CustomTabRequired=false;
  129. tab3.CustomTabLocked=false;
  130. tab3.CustomTabDisableAutoSize=false;
  131. tab3.TemplateLocked=false;
  132. tab3.TemplateRequired=false;
  133. tab3.ConditionalParentLabel='';
  134. tab3.ConditionalParentValue='';
  135. tab3.SharedTab=true;
  136. tab3.RequireInitialOnSharedTabChange=false;
  137. tab3.ConcealValueOnDocument=false;
  138. tab3.AnchorTabItem = new DocuSignAPI.AnchorTab();
  139. tab3.AnchorTabItem.AnchorTabString = '/t2/';
  140. tab3.AnchorTabItem.XOffset = 42;
  141. tab3.AnchorTabItem.YOffset = -5;
  142. tab3.MergeFieldXml = '<mergeField><allowSenderToEdit>true</allowSenderToEdit><configurationType>salesforce</configurationType><path>Lead.Title</path><row>1</row><writeBack>true</writeBack></mergeField>';
  143.  
  144. envelope.Tabs = new DocuSignAPI.ArrayOfTab();
  145. envelope.Tabs.Tab = new DocuSignAPI.Tab[3];
  146. envelope.Tabs.Tab[0] = tab1;
  147. envelope.Tabs.Tab[1] = tab2;
  148. envelope.Tabs.Tab[2] = tab3;
  149.  
  150. System.debug('Calling the API');
  151. try {
  152. DocuSignAPI.EnvelopeStatus es = dsApiSend.CreateAndSendEnvelope(envelope);
  153. envelopeId = es.EnvelopeID;
  154. System.debug('Returned successfully, envelope id = ' + envelopeId );
  155. return '';
  156. } catch ( CalloutException e) {
  157. System.debug('Exception - ' + e );
  158. envelopeId = 'Exception - ' + e;
  159. return '';
  160. }
  161.  
  162. return '';
  163. }
  164. }
Add Comment
Please, Sign In to add comment