public with sharing class dseController {
public dseController()
{
}
public dseController(ApexPages.StandardController controller)
{
}
@future (callout=true)
public static void updateEnvelope( string envelopeID, string recipientID )
{
RecipientResponse RecipientResponseDeserialized = new RecipientResponse();
RecipientTabResponse RecipientTabResponseDeserialized = new RecipientTabResponse();
string rResponse = '{}';
string recipientGuidwithContractTab;
string recipientGuidForBlockingRecipient;
string rTabResponse = '{}';
string rSetRecipientResponse = '{}';
string rSetTabForRecipientResponse = '{}';
string rTabRecipientDeleteResponse = '{}';
string rBlockingRecipientDeleteResponse = '{}';
try
{
//Call to get envelope recipients
rResponse = getEnvelopeRecipients(envelopeID);
system.debug(rResponse);
RecipientResponseDeserialized = parseRecipentResponse(rResponse);
system.debug(RecipientResponseDeserialized);
recipientGuidwithContractTab = getRecipientwithContractTab(RecipientResponseDeserialized);
system.debug(recipientGuidwithContractTab);
//Call to get recipient tab
rTabResponse = getRecipientTab(envelopeID, recipientGuidwithContractTab);
system.debug(rTabResponse);
RecipientTabResponseDeserialized = parseRecipientTabResponse(rTabResponse);
system.debug(RecipientTabResponseDeserialized);
//Call to add recipient with new id
rSetRecipientResponse = setRecipientForNewTab(envelopeID,RecipientResponseDeserialized);
system.debug(rSetRecipientResponse);
//Call to add tab to new recipient with new id
rSetTabForRecipientResponse = setNewTabforNewRecipient(envelopeID,RecipientTabResponseDeserialized);
system.debug(rSetTabForRecipientResponse);
//Call to delete old clone recipient
rTabRecipientDeleteResponse = deleteRecipientTab(envelopeID, recipientGuidwithContractTab);
system.debug(rTabRecipientDeleteResponse);
//Call to delete blocking user
recipientGuidForBlockingRecipient = getBlockingRecipient(RecipientResponseDeserialized);
rBlockingRecipientDeleteResponse = deleteBlockingRecipient(envelopeID, recipientGuidForBlockingRecipient);
system.debug(rBlockingRecipientDeleteResponse);
}
catch(Exception ex)
{
system.debug(ex);
}
finally
{
}
}
public static string getRecipientwithContractTab(RecipientResponse rResponse)
{
string rContractTabID = 'Not Found';
try{
List<Signer> mySigners = rResponse.signers;
for(Signer mySigner : mySigners)
{
if(mySigner.roleName == 'ContractIDApprover')
{
rContractTabID = mySigner.recipientIdGuid;
}
}
}
catch(Exception ex)
{
system.debug(ex);
}
finally
{
}
return rContractTabID;
}
public static string getBlockingRecipient(RecipientResponse rResponse)
{
string rContractTabID = 'Not Found';
try{
List<Signer> mySigners = rResponse.signers;
for(Signer mySigner : mySigners)
{
if(mySigner.roleName == 'BlockingUser')
{
rContractTabID = mySigner.recipientIdGuid;
}
}
}
catch(Exception ex)
{
system.debug(ex);
}
finally
{
}
return rContractTabID;
}
public static string getEnvelopeRecipients(string envelopeID)
{
string response = '{}';
string DSEndpoint = 'https://demo.docusign.net/restapi/v2/';
string DSUserId = 'yourdsUserid';
string DSPassword = 'yourdspassword';
string DSAccountID = 'yourdsaccountID';
string DSIntegratorKey = 'yourdsintegratorkey';
try
{
//FORCES that the DocuSign member has to be in the DocuSign account DSFS is configured for
List<dsfs__DocuSignAccountConfiguration__c> dsAccountConfig = [Select d.dsfs__UseSendOnBehalfOf__c, d.dsfs__DocuSignBaseURL__c, d.dsfs__DSProSFUsername__c, d.dsfs__DSProSFPassword__c, d.dsfs__AccountId__c From dsfs__DocuSignAccountConfiguration__c d limit 1];
for(dsfs__DocuSignAccountConfiguration__c myConfig : dsAccountConfig)
{
DSEndpoint = myConfig.dsfs__DocuSignBaseURL__c + 'restapi/v2/';
DSUserId = myConfig.dsfs__DSProSFUsername__c;
DSPassword = myConfig.dsfs__DSProSFPassword__c;
}
HttpRequest request = new HttpRequest();
request.setEndpoint(DSEndpoint + 'accounts/'+DSAccountID+'/envelopes/'+envelopeID+'/recipients');
request.setMethod('GET');
request.setHeader('Content-Type', 'application/json');
request.setHeader('X-DocuSign-Authentication', '<DocuSignCredentials><Username>'+DSUserId+'</Username><Password>'+DSPassword+'</Password><IntegratorKey>'+DSIntegratorKey+'</IntegratorKey></DocuSignCredentials>');
request.setHeader('Accept', 'application/json');
request.setTimeout(120000);
system.debug(request.getHeader('X-DocuSign-Authentication'));
HttpResponse myResponse = (new Http()).send(request);
system.debug(myResponse.getBody());
if(myResponse.getStatusCode().format()=='200')
{
response = myResponse.getBody();
system.debug(response);
}
}
catch(Exception ex)
{
system.debug(ex);
}
finally
{
}
return response;
}
public static string getRecipientTab(string envelopeID, string recipientGuid)
{
string response = '{}';
string DSEndpoint = 'https://demo.docusign.net/restapi/v2/';
string DSUserId = 'yourdsUserid';
string DSPassword = 'yourdspassword';
string DSAccountID = 'yourdsaccountID';
string DSIntegratorKey = 'yourdsintegratorkey';
try
{
//FORCES that the DocuSign member has to be in the DocuSign account DSFS is configured for
List<dsfs__DocuSignAccountConfiguration__c> dsAccountConfig = [Select d.dsfs__UseSendOnBehalfOf__c, d.dsfs__DocuSignBaseURL__c, d.dsfs__DSProSFUsername__c, d.dsfs__DSProSFPassword__c, d.dsfs__AccountId__c From dsfs__DocuSignAccountConfiguration__c d limit 1];
for(dsfs__DocuSignAccountConfiguration__c myConfig : dsAccountConfig)
{
DSEndpoint = myConfig.dsfs__DocuSignBaseURL__c + 'restapi/v2/';
DSUserId = myConfig.dsfs__DSProSFUsername__c;
DSPassword = myConfig.dsfs__DSProSFPassword__c;
}
HttpRequest request = new HttpRequest();
request.setEndpoint(DSEndpoint + 'accounts/'+DSAccountID+'/envelopes/'+envelopeID+'/recipients/'+recipientGuid+'/tabs/');
request.setMethod('GET');
request.setHeader('Content-Type', 'application/json');
request.setHeader('X-DocuSign-Authentication', '<DocuSignCredentials><Username>'+DSUserId+'</Username><Password>'+DSPassword+'</Password><SendOnBehalfOf>[email protected]</SendOnBehalfOf><IntegratorKey>'+DSIntegratorKey+'</IntegratorKey></DocuSignCredentials>');
request.setHeader('Accept', 'application/json');
request.setTimeout(120000);
system.debug(request.getHeader('X-DocuSign-Authentication'));
HttpResponse myResponse = (new Http()).send(request);
system.debug(myResponse.getBody());
if(myResponse.getStatusCode().format()=='200')
{
response = myResponse.getBody();
system.debug(response);
}
}
catch(Exception ex)
{
system.debug(ex);
}
finally
{
}
return response;
}
public static string setRecipientForNewTab(string envelopeID, RecipientResponse rResponse)
{
string response = '{}';
string DSEndpoint = 'https://demo.docusign.net/restapi/v2/';
string DSUserId = 'yourdsUserid';
string DSPassword = 'yourdspassword';
string DSAccountID = 'yourdsaccountID';
string DSIntegratorKey = 'yourdsintegratorkey';
try
{
//FORCES that the DocuSign member has to be in the DocuSign account DSFS is configured for
List<dsfs__DocuSignAccountConfiguration__c> dsAccountConfig = [Select d.dsfs__UseSendOnBehalfOf__c, d.dsfs__DocuSignBaseURL__c, d.dsfs__DSProSFUsername__c, d.dsfs__DSProSFPassword__c, d.dsfs__AccountId__c From dsfs__DocuSignAccountConfiguration__c d limit 1];
for(dsfs__DocuSignAccountConfiguration__c myConfig : dsAccountConfig)
{
DSEndpoint = myConfig.dsfs__DocuSignBaseURL__c + 'restapi/v2/';
DSUserId = myConfig.dsfs__DSProSFUsername__c;
DSPassword = myConfig.dsfs__DSProSFPassword__c;
}
Signer mySignerToAdd = new Signer();
List<Signer> mySigners = rResponse.signers;
for(Signer mySigner : mySigners)
{
if(mySigner.roleName == 'ContractIDApprover')
{
mySignerToAdd = mySigner;
}
}
String myBody;
myBody = '{"signers": [{"signInEachLocation": "false","name": "'+mySignerToAdd.name +'Added 1","email": "'+mySignerToAdd.email+'","recipientId": "7","requireIdLookup": "false","routingOrder": "19","roleName": "'+mySignerToAdd.roleName+'1"}]}';
HttpRequest request = new HttpRequest();
request.setEndpoint(DSEndpoint + 'accounts/'+DSAccountID+'/envelopes/'+envelopeID+'/recipients/');
request.setMethod('POST');
request.setHeader('Content-Type', 'application/json');
request.setHeader('X-DocuSign-Authentication', '<DocuSignCredentials><Username>'+DSUserId+'</Username><Password>'+DSPassword+'</Password><SendOnBehalfOf>[email protected]</SendOnBehalfOf><IntegratorKey>'+DSIntegratorKey+'</IntegratorKey></DocuSignCredentials>');
request.setHeader('Accept', 'application/json');
request.setTimeout(120000);
request.setBody(myBody);
system.debug(request.getHeader('X-DocuSign-Authentication'));
HttpResponse myResponse = (new Http()).send(request);
system.debug(myResponse.getBody());
if(myResponse.getStatusCode().format()=='201')
{
response = myResponse.getBody();
system.debug(response);
}
}
catch(Exception ex)
{
system.debug(ex);
}
finally
{
}
return response;
}
public static string setNewTabforNewRecipient(string envelopeID, RecipientTabResponse rTabResponse)
{
string response = '{}';
string DSEndpoint = 'https://demo.docusign.net/restapi/v2/';
string DSUserId = 'yourdsUserid';
string DSPassword = 'yourdspassword';
string DSAccountID = 'yourdsaccountID';
string DSIntegratorKey = 'yourdsintegratorkey';
try
{
//FORCES that the DocuSign member has to be in the DocuSign account DSFS is configured for
List<dsfs__DocuSignAccountConfiguration__c> dsAccountConfig = [Select d.dsfs__UseSendOnBehalfOf__c, d.dsfs__DocuSignBaseURL__c, d.dsfs__DSProSFUsername__c, d.dsfs__DSProSFPassword__c, d.dsfs__AccountId__c From dsfs__DocuSignAccountConfiguration__c d limit 1];
for(dsfs__DocuSignAccountConfiguration__c myConfig : dsAccountConfig)
{
DSEndpoint = myConfig.dsfs__DocuSignBaseURL__c + 'restapi/v2/';
DSUserId = myConfig.dsfs__DSProSFUsername__c;
DSPassword = myConfig.dsfs__DSProSFPassword__c;
}
TextTabs myTextTabToAdd = new TextTabs();
List<TextTabs> myTextTabs = rTabResponse.textTabs;
for(TextTabs myTextTab : myTextTabs)
{
if(myTextTab.tabLabel == 'ContractID')
{
myTextTabToAdd = myTextTab;
}
}
String myBody;
myBody = '{"textTabs": [{"height": '+myTextTabToAdd.height+',"shared": "'+myTextTabToAdd.shared+'","requireInitialOnSharedChange": "'+myTextTabToAdd.requireInitialOnSharedChange+'","name": "'+myTextTabToAdd.name+'1","value": "ContractID12345","width": '+myTextTabToAdd.width+',"required": "'+myTextTabToAdd.required+'","locked": "'+myTextTabToAdd.locked+'","concealValueOnDocument": "'+myTextTabToAdd.concealValueOnDocument+'","disableAutoSize": "'+myTextTabToAdd.disableAutoSize+'","tabLabel": "'+myTextTabToAdd.tabLabel+'","documentId": "'+myTextTabToAdd.documentId+'","recipientId": "7","pageNumber": "'+myTextTabToAdd.pageNumber+'","xPosition": "'+myTextTabToAdd.xPosition+'","yPosition": "'+myTextTabToAdd.yPosition+'"}]}';
HttpRequest request = new HttpRequest();
request.setEndpoint(DSEndpoint + 'accounts/'+DSAccountID+'/envelopes/'+envelopeID+'/recipients/7/tabs');
request.setMethod('POST');
request.setHeader('Content-Type', 'application/json');
request.setHeader('X-DocuSign-Authentication', '<DocuSignCredentials><Username>'+DSUserId+'</Username><Password>'+DSPassword+'</Password><SendOnBehalfOf>[email protected]</SendOnBehalfOf><IntegratorKey>'+DSIntegratorKey+'</IntegratorKey></DocuSignCredentials>');
request.setHeader('Accept', 'application/json');
request.setTimeout(120000);
request.setBody(myBody);
system.debug(request.getHeader('X-DocuSign-Authentication'));
HttpResponse myResponse = (new Http()).send(request);
system.debug(myResponse.getBody());
if(myResponse.getStatusCode().format()=='201')
{
response = myResponse.getBody();
system.debug(response);
}
}
catch(Exception ex)
{
system.debug(ex);
}
finally
{
}
return response;
}
public static string deleteRecipientTab(string envelopeID, string recipientGuid)
{
string response = '{}';
string DSEndpoint = 'https://demo.docusign.net/restapi/v2/';
string DSUserId = 'yourdsUserid';
string DSPassword = 'yourdspassword';
string DSAccountID = 'yourdsaccountID';
string DSIntegratorKey = 'yourdsintegratorkey';
try
{
//FORCES that the DocuSign member has to be in the DocuSign account DSFS is configured for
List<dsfs__DocuSignAccountConfiguration__c> dsAccountConfig = [Select d.dsfs__UseSendOnBehalfOf__c, d.dsfs__DocuSignBaseURL__c, d.dsfs__DSProSFUsername__c, d.dsfs__DSProSFPassword__c, d.dsfs__AccountId__c From dsfs__DocuSignAccountConfiguration__c d limit 1];
for(dsfs__DocuSignAccountConfiguration__c myConfig : dsAccountConfig)
{
DSEndpoint = myConfig.dsfs__DocuSignBaseURL__c + 'restapi/v2/';
DSUserId = myConfig.dsfs__DSProSFUsername__c;
DSPassword = myConfig.dsfs__DSProSFPassword__c;
}
HttpRequest request = new HttpRequest();
request.setEndpoint(DSEndpoint + 'accounts/'+DSAccountID+'/envelopes/'+envelopeID+'/recipients/'+recipientGuid);
request.setMethod('DELETE');
request.setHeader('Content-Type', 'application/json');
request.setHeader('X-DocuSign-Authentication', '<DocuSignCredentials><Username>'+DSUserId+'</Username><Password>'+DSPassword+'</Password><SendOnBehalfOf>[email protected]</SendOnBehalfOf><IntegratorKey>'+DSIntegratorKey+'</IntegratorKey></DocuSignCredentials>');
request.setHeader('Accept', 'application/json');
request.setTimeout(120000);
system.debug(request.getHeader('X-DocuSign-Authentication'));
HttpResponse myResponse = (new Http()).send(request);
system.debug(myResponse.getBody());
if(myResponse.getStatusCode().format()=='200')
{
response = myResponse.getBody();
system.debug(response);
}
}
catch(Exception ex)
{
system.debug(ex);
}
finally
{
}
return response;
}
public static string deleteBlockingRecipient(string envelopeID, string recipientGuid)
{
string response = '{}';
string DSEndpoint = 'https://demo.docusign.net/restapi/v2/';
string DSUserId = 'yourdsUserid';
string DSPassword = 'yourdspassword';
string DSAccountID = 'yourdsaccountID';
string DSIntegratorKey = 'yourdsintegratorkey';
try
{
//FORCES that the DocuSign member has to be in the DocuSign account DSFS is configured for
List<dsfs__DocuSignAccountConfiguration__c> dsAccountConfig = [Select d.dsfs__UseSendOnBehalfOf__c, d.dsfs__DocuSignBaseURL__c, d.dsfs__DSProSFUsername__c, d.dsfs__DSProSFPassword__c, d.dsfs__AccountId__c From dsfs__DocuSignAccountConfiguration__c d limit 1];
for(dsfs__DocuSignAccountConfiguration__c myConfig : dsAccountConfig)
{
DSEndpoint = myConfig.dsfs__DocuSignBaseURL__c + 'restapi/v2/';
DSUserId = myConfig.dsfs__DSProSFUsername__c;
DSPassword = myConfig.dsfs__DSProSFPassword__c;
}
HttpRequest request = new HttpRequest();
request.setEndpoint(DSEndpoint + 'accounts/'+DSAccountID+'/envelopes/'+envelopeID+'/recipients/'+recipientGuid);
request.setMethod('DELETE');
request.setHeader('Content-Type', 'application/json');
request.setHeader('X-DocuSign-Authentication', '<DocuSignCredentials><Username>'+DSUserId+'</Username><Password>'+DSPassword+'</Password><SendOnBehalfOf>[email protected]</SendOnBehalfOf><IntegratorKey>'+DSIntegratorKey+'</IntegratorKey></DocuSignCredentials>');
request.setHeader('Accept', 'application/json');
request.setTimeout(120000);
system.debug(request.getHeader('X-DocuSign-Authentication'));
HttpResponse myResponse = (new Http()).send(request);
system.debug(myResponse.getBody());
if(myResponse.getStatusCode().format()=='200')
{
response = myResponse.getBody();
system.debug(response);
}
}
catch(Exception ex)
{
system.debug(ex);
}
finally
{
}
return response;
}
public static RecipientResponse parseRecipentResponse(String json) {
return (RecipientResponse) System.JSON.deserialize(json, RecipientResponse.class);
}
public static RecipientTabResponse parseRecipientTabResponse(String json) {
return (RecipientTabResponse) System.JSON.deserialize(json, RecipientTabResponse.class);
}
public class Signer
{
public string name;
public string email;
public string recipientId;
public string recipientIdGuid;
public string requireIdLookup;
public string userId;
public string routingOrder;
public string roleName;
public string status;
public string signedDateTime;
public string deliveredDateTime;
public string templateLocked;
public string templateRequired;
}
public class RecipientResponse
{
public List<Signer> signers;
public List<Signer> agents;
public List<Signer> editors;
public List<Signer> intermediaries;
public List<Signer> carbonCopies;
public List<Signer> certifiedDeliveries;
public List<Signer> inPersonSigners;
public String recipientCount;
public String currentRoutingOrder;
}
public class TextTabs {
public Integer height;
public String validationPattern;
public String validationMessage;
public String shared;
public String requireInitialOnSharedChange;
public String name;
public String value;
public Integer width;
public String required;
public String locked;
public String concealValueOnDocument;
public String disableAutoSize;
public String tabLabel;
public String documentId;
public String recipientId;
public String pageNumber;
public String xPosition;
public String yPosition;
public String tabId;
}
public class RecipientTabResponse
{
public List<TextTabs> textTabs;
}
}