Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- global class AnnualContract
- {
- webService static string AM_SendToDocuSign(String id, string strObjType)
- {
- Docusign_API_Setting__c APISetting = Docusign_API_Setting__c.getInstance('API Settings');
- String envelopeId = '';
- string DealerName = '';
- string DealerId = '';
- String accountId = APISetting.AccountId__c;
- String userId = APISetting.UserId__c;
- String password = APISetting.Password__c;
- String integratorsKey = APISetting.IntegratorsKey__c;
- String webServiceUrl = APISetting.WebServiceUrl__c;
- list<Lead> lstLead = new list<Lead>();
- list<Contact> lstContact = new list<Contact>();
- if(strObjType == 'Lead')
- {
- lstLead = [SELECT Name,Status,Email,FirstName,LastName,Owner.Name,Title,FROM Lead where id = : Id limit 1];
- }
- StaticResource objSR = [SELECT Id,name, SystemModStamp FROM StaticResource WHERE Name = 'AnnualContractPDF' LIMIT 1];
- String url_file_ref = '/resource/' + String.valueOf(((DateTime)objSR.get('SystemModStamp')).getTime())+ '/' + objSR.get('Name');
- if(strObjType == 'Lead')
- {
- DealerName = lstLead[0].Name;
- }
- DocuSignAPI.APIServiceSoap dsApiSend = new DocuSignAPI.APIServiceSoap();
- dsApiSend.endpoint_x = webServiceUrl;
- //Set Authentication
- String auth = '<DocuSignCredentials><Username>'+ userId
- +'</Username><Password>' + password
- + '</Password><IntegratorKey>' + integratorsKey
- + '</IntegratorKey></DocuSignCredentials>';
- System.debug('Setting authentication to: ' + auth);
- dsApiSend.inputHttpHeaders_x = new Map<String, String>();
- dsApiSend.inputHttpHeaders_x.put('X-DocuSign-Authentication',
- auth);
- DocuSignAPI.Envelope envelope = new DocuSignAPI.Envelope();
- envelope.Subject = 'Please Sign this Contract' + lstLead[0].Name;
- 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';
- envelope.AccountId = accountId;
- // Render the contract
- System.debug('Rendering the contract');
- PageReference pageRef = new PageReference(url_file_ref);
- Blob pdfBlob = pageRef.getContent();
- DocuSignAPI.CustomField field = new DocuSignAPI.CustomField ();
- field.Name = '##SFLead';
- field.Value = lstLead[0].Id; //value of the external source Id
- field.Show = 'false';
- field.CustomFieldType = 'Text';
- envelope.CustomFields = new DocuSignAPI.ArrayOfCustomField();
- envelope.CustomFields.CustomField = new DocuSignAPI.CustomField[1];
- envelope.CustomFields.CustomField[0] = field;
- // Document
- DocuSignAPI.Document document = new DocuSignAPI.Document();
- document.ID = 1;
- document.pdfBytes = EncodingUtil.base64Encode(pdfBlob);
- document.Name = 'Annual Contract';
- document.FileExtension = 'pdf';
- envelope.Documents = new DocuSignAPI.ArrayOfDocument();
- envelope.Documents.Document = new DocuSignAPI.Document[1];
- envelope.Documents.Document[0] = document;
- // Recipient
- System.debug('Building up the recipient');
- DocuSignAPI.Recipient recipient = new DocuSignAPI.Recipient();
- recipient.ID = 1;
- recipient.Type_x = 'Signer';
- recipient.RoutingOrder = 1;
- recipient.Email = lstLead[0].Email;
- recipient.UserName = lstLead[0].FirstName + ' ' + lstLead[0].LastName;
- recipient.RequireIDLookup = false;
- envelope.Recipients = new DocuSignAPI.ArrayOfRecipient();
- envelope.Recipients.Recipient = new DocuSignAPI.Recipient[1];
- envelope.Recipients.Recipient[0] = recipient;
- // Tab
- DocuSignAPI.Tab tab1 = new DocuSignAPI.Tab();
- tab1.Type_x = 'SignHere';
- tab1.RecipientID = 1;
- tab1.DocumentID = 1;
- tab1.AnchorTabItem = new DocuSignAPI.AnchorTab();
- tab1.AnchorTabItem.AnchorTabString = '/t1/';
- tab1.AnchorTabItem.XOffset = 100;
- DocuSignAPI.Tab tab2 = new DocuSignAPI.Tab();
- tab2.Type_x = 'DateSigned';
- tab2.RecipientID = 1;
- tab2.DocumentID = 1;
- tab2.AnchorTabItem = new DocuSignAPI.AnchorTab();
- tab2.AnchorTabItem.AnchorTabString = '/d1/';
- DocuSignAPI.Tab tab3 = new DocuSignAPI.Tab();
- tab3.CustomTabType = 'Text';
- tab3.Name = 'Title';
- tab3.Type_x = 'Custom';
- tab3.RecipientID = 1;
- tab3.DocumentID = 1;
- tab3.TabLabel = 'Title';
- if(strObjType == 'Lead')
- {
- if(lstLead[0].Title != null)
- {
- tab3.Value = ''+lstLead[0].Title+'';
- }
- }
- else
- {
- if(lstContact[0].Title != null)
- {
- tab3.Value = ''+lstContact[0].Title+'';
- }
- }
- tab3.CustomTabWidth=100;
- tab3.CustomTabRequired=false;
- tab3.CustomTabLocked=false;
- tab3.CustomTabDisableAutoSize=false;
- tab3.TemplateLocked=false;
- tab3.TemplateRequired=false;
- tab3.ConditionalParentLabel='';
- tab3.ConditionalParentValue='';
- tab3.SharedTab=true;
- tab3.RequireInitialOnSharedTabChange=false;
- tab3.ConcealValueOnDocument=false;
- tab3.AnchorTabItem = new DocuSignAPI.AnchorTab();
- tab3.AnchorTabItem.AnchorTabString = '/t2/';
- tab3.AnchorTabItem.XOffset = 42;
- tab3.AnchorTabItem.YOffset = -5;
- tab3.MergeFieldXml = '<mergeField><allowSenderToEdit>true</allowSenderToEdit><configurationType>salesforce</configurationType><path>Lead.Title</path><row>1</row><writeBack>true</writeBack></mergeField>';
- envelope.Tabs = new DocuSignAPI.ArrayOfTab();
- envelope.Tabs.Tab = new DocuSignAPI.Tab[3];
- envelope.Tabs.Tab[0] = tab1;
- envelope.Tabs.Tab[1] = tab2;
- envelope.Tabs.Tab[2] = tab3;
- System.debug('Calling the API');
- try {
- DocuSignAPI.EnvelopeStatus es = dsApiSend.CreateAndSendEnvelope(envelope);
- envelopeId = es.EnvelopeID;
- System.debug('Returned successfully, envelope id = ' + envelopeId );
- return '';
- } catch ( CalloutException e) {
- System.debug('Exception - ' + e );
- envelopeId = 'Exception - ' + e;
- return '';
- }
- return '';
- }
- }
Add Comment
Please, Sign In to add comment