How to add attachments alone to the contact from which the document was sent?

How to add attachments alone to the contact from which the document was sent?

Scenario: I send the documents to Zoho Sign using the 'Send with Zoho Sign' button on the Contacts view page. Once the document is sent for signing, the document details and the attachments are added to the 'Zoho Sign Documents' module. I want the attachments alone to be added to the contact from which the document was sent. Can I do this using a workflow in CRM?
This code will attach the signed document to the record from which it was triggered. For example if you have sent from record 'A' in module 'XYZ', then the signed document will be added to this record 'A'.
  1. Open ZohoCRM page, then navigate to Setup -> Customization -> Workflow rules. Create a new Workflow for the Zoho Sign Documents module.
  2. 'On a record action' -> 'Edit'
  3. Click the checkbox for Repeat this workflow whenever a Zoho Sign documents is edited.
  4. Select 'Specific field(s) gets modified'
  5. When 'Document Status' is modified to 'the value' as Signed.
  6. For the condition'Which Zoho Sign documents would you like to apply the rule to?', select the value as All Zoho Sign Documents.
                                -->Click Next.
                                -->In the list of action, select function
  7. Select the value Create your own function.
  8. Name the function and create it.
  9. Edit the arguments in the function.
  10. Add the key name as 'requestId', in the value type in # and select 'Zoho Sign Documents' module with fields as 'Zoho Sign Document Id'.
  11. Use the below code in the function and replace your Zoho Sign connection name.
  12. For Creating connection -> Go to Setup -> Developer Space -> Connections -> Create new Connection -> Select 'Zoho OAuth' -> give connection name here -> Use the same 'Connection Link Name' in the function.
  13. Select the following scopes --> Zoho Sign.account.ALL, Zoho Sign.documents.ALL, Zoho Sign.setup.READ, Zoho Sign.templates.ALL
  14. Click Create and Connect.
  15. Save the function and the workflow rules.
  1. response = invokeurl
  2. [
  3.   url :"https://sign.zoho.com/api/v1/requests/" + requestId
  4.  type :GET
  5.  connection:"Use Connection Name here"
  6. ];
  7. requestObj = response.get("requests");
  8. info requestObj;
  9. if(requestObj.containKey("request_status"))
  10. {
  11.  reqStatus = requestObj.get("request_status");
  12.  info reqStatus;
  13.  if(reqStatus.equals("completed") || reqStatus.equals("expired") || reqStatus.equals("declined") || reqStatus.equals("recalled"))
  14.  {
  15.   requestId = requestObj.get("request_id");
  16.   actions = requestObj.get("actions");
  17.   document_ids = requestObj.get("document_ids");
  18.   email = "";
  19.   order = 1000;
  20.   for each  action in actions
  21.   {
  22.    if(action.get("action_type").equals("SIGN") && action.get("signing_order") < order)
  23.    {
  24.     order = action.get("signing_order");
  25.     email = action.get("recipient_email");
  26.     records = zoho.crm.searchRecords("Custom_Module_Name","(Email:equals:" + email + ")");
  27.     if(records.size() > 0)
  28.     {
  29.      leadid = records.get(0).get("id");
  30.      for each  document in document_ids
  31.      {
  32.       document_id = document.get("document_id");
  33.       fileResponse = invokeurl
  34.       [
  35.        url :"https://sign.zoho.com/api/v1/requests/" + requestId + "/documents/" + document_id + "/pdf"
  36.        type :GET
  37.        connection:"Use Connection Name here"
  38.       ];
  39.       zoho.crm.attachFile("Custom_Module_Name",leadid,fileResponse);
  40.      }
  41.      fileResponse = invokeurl
  42.      [
  43.       url :"https://sign.zoho.com/api/v1/requests/" + requestId + "/completioncertificate"
  44.       type :GET
  45.       connection:"Use Connection Name here"
  46.      ];
  47.      zoho.crm.attachFile("Custom_Module_Name",leadid,fileResponse);
  48.     }
  49.    }
  50.    break;
  51.   }
  52.  }
  53. }