For Zoho Services only:


I'm actually part of a bigger team at Ascent Business Solutions where we have support technicians and project consultants. Support is for smaller technical fixes but this can include developments, reports or integrations; depending on the size of the task. Projects are for more time-consuming developments such as revamps of the Zoho Suite of apps or on-site training. The advantage of a team is that if I am out-of-office for a day or so, there is always someone at Ascent Business Solutions who can deal with any queries/issues you may have.

Our support rates can be found and purchased at http://ascentbusiness.co.uk/zoho-support-2. A support bundle doesn't have an expiry date. So whether we can do what you want within the bundle and a year later need further support, if there are minutes left on the bundle then there is no additional charge.

Our project rates for bigger developments can be found at http://ascentbusiness.co.uk/crm-solutions/zoho-crm-packages-prices and will involve a dedicated project consultant along with developers who will hold your hand through the development process.

If you want help building a solution for one of the Zoho Apps in the Zoho Suite, contact us on 0121 293 8140 (UK) or by email at info@ascentbusiness.co.uk. You can also visit our website at http://ascentbusiness.co.uk.

I regularly build and specialize in 2-way API integrations for Xero, Shopify and eBay.

Zoho Creator: Download File from ZohoCRM field type "File Upload" (not attachments)

What?
This is an article to demo some Deluge code on how to download a file that was uploaded in CRM in a custom module in field that was of type "File Upload".

Why?
Just to remind me where I went wrong, I have a client with ZohoCRM who upload a PDF (can be any file but is usually a PDF) to a custom module in their CRM; I then need the customer from the customer portal in a ZohoCreator app to be able to download this file.

How?
The trick to doing this is that actually you have to treat it as an attachment. As you may already know, when you queried CRM for that field, you received a JSON response somewhat similar to the following:
copyraw
// this is sample JSON with replaced IDs

  "My_CRM_File": [
    {
      "extn": "pdf",
      "is_Preview_Available": true,
      "download_Url": "/crm/org12345678901/specific/ViewAttachment?fileId=aaaabbbbccccddddeeeeffff1111222233334&module=CustomModule1&parentId=123456789012345678&creatorId=987654321098765432&id=234567890123456789&name=dummy1.pdf&downLoadMode=default",
      "delete_Url": "/crm/org12345678901/deleteattachment.do?attachmentid=234567890123456789&module=CustomModule1&id=125878000002717078&creatorId=987654321098765432&fromFileUploadField=true&nextStep=relatedlist&isajax=true&fieldId=123456789012345678&fromPage=edit",
      "entity_Id": 345678901234567890,
      "mode": "pdfViewPlugin",
      "original_Size_Byte": "13264",
      "preview_Url": "/crm/org12345678901/specific/ViewAttachment?fileId=aaaabbbbccccddddeeeeffff1111222233334&module=CustomModule1&parentId=123456789012345678&creatorId=987654321098765432&id=234567890123456789&name=dummy1.pdf&downLoadMode=pdfViewPlugin",
      "file_Name": "dummy1.pdf",
      "file_Id": "aaaabbbbccccddddeeeeffff1111222233334",
      "attachment_Id": "234567890123456789",
      "file_Size": "13.26 KB",
      "creator_Id": 987654321098765432,
      "link_Docs": 0
    }
  ],
  1.  // this is sample JSON with replaced IDs 
  2.   
  3.    "My_CRM_File": [ 
  4.      { 
  5.        "extn": "pdf", 
  6.        "is_Preview_Available": true, 
  7.        "download_Url": "/crm/org12345678901/specific/ViewAttachment?fileId=aaaabbbbccccddddeeeeffff1111222233334&module=CustomModule1&parentId=123456789012345678&creatorId=987654321098765432&id=234567890123456789&name=dummy1.pdf&downLoadMode=default", 
  8.        "delete_Url": "/crm/org12345678901/deleteattachment.do?attachmentid=234567890123456789&module=CustomModule1&id=125878000002717078&creatorId=987654321098765432&fromFileUploadField=true&nextStep=relatedlist&isajax=true&fieldId=123456789012345678&fromPage=edit", 
  9.        "entity_Id": 345678901234567890, 
  10.        "mode": "pdfViewPlugin", 
  11.        "original_Size_Byte": "13264", 
  12.        "preview_Url": "/crm/org12345678901/specific/ViewAttachment?fileId=aaaabbbbccccddddeeeeffff1111222233334&module=CustomModule1&parentId=123456789012345678&creatorId=987654321098765432&id=234567890123456789&name=dummy1.pdf&downLoadMode=pdfViewPlugin", 
  13.        "file_Name": "dummy1.pdf", 
  14.        "file_Id": "aaaabbbbccccddddeeeeffff1111222233334", 
  15.        "attachment_Id": "234567890123456789", 
  16.        "file_Size": "13.26 KB", 
  17.        "creator_Id": 987654321098765432, 
  18.        "link_Docs": 0 
  19.      } 
  20.    ], 

The most logical thinkers would say that you simply need the download_Url value and prepend the CRM base URL such as https://crm.zoho.com + v_download_Url... But that is incorrect, apparently you have to treat the field of type "File Upload" as an attachment despite it not being an attachment (or at least not found in the "Attachments" section of the CRM module).

The Solution
So as a quick overview, this is what I did:
  1. Create a ZohoCreator form (I'm calling mine in this example: Download File to hold the file and some other fields
    1. Owner Email (type: Email)
    2. FIle Name (type: Single Line)
    3. File Ref (type: Single Line)
    4. Expiry Date Time (type: Date-Time)
    5. File Download (type: File upload)
  2. Create a connection in the Creator app with the scope: ZohoCRM.modules.ALL (potentially you could try just using ZohoCRM.modules.Attachments.READ but at this point I couldn't be asked).
  3. Then publish the report of this form (eg. "Download File Report") and get the long publish key it returns you. eg.
    https://creatorapp.zohopublic.eu + zoho.appuri + report-perma/Download_File_Report/abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKL
The Code to read, parse, create a holding record and generate a link for the customer portal user to click on:
copyraw
// accessing zoho.eu (not zoho.com) so adapt the 2 values
v_CreatorBaseUrl = "https://creator.zoho.eu";
v_ZohoApisDC = "https://www.zohoapis.eu";
//
// get field from CRM
l_ResultThisFile = ifnull(r_Row.get("My_CRM_File"),"-");
for each  m_ThisFile in l_ResultThisFile
{
    v_ThisFileCrmID = ifnull(m_ThisFile.get("file_Id"),"-");
    v_ThisFileName = ifnull(m_ThisFile.get("file_Name"),"-");
    v_ThisFileUrl = ifnull(m_ThisFile.get("download_Url"),"-");
    v_ThisFileAttachmentID = ifnull(m_ThisFile.get("attachment_Id"),"-");
    v_ThisFileSize = ifnull(m_ThisFile.get("file_Size"),"-");
    v_ExpiryTime = zoho.currenttime.addMinutes(10).toString("dd-MMM-yyyy HH:mm:ss");
    //
    // put your own Publish key here (publish the report holding the files to download and you will receive this key)
    v_PublishKey = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKL";
    //
    // check if already exists (double-security: Email and File Ref must match)
    r_CreatorFile = Download_File[Owner_Email == v_ResultEmail && File_Ref == v_ThisFileCrmID];
    if(r_CreatorFile.count()>0)
    {
        if(!isnull(r_CreatorFile.File_Download))
        {
            v_LinkToDownload = v_CreatorBaseUrl + "/file" + zoho.appuri + "Download_File_Report/" + r_CreatorFile.ID + "/File_Download/download/"+v_PublishKey+"?filepath=/" + r_CreatorFile.File_Download;
        }
    }
    else
    {
        v_CrmEndpointUrl = v_ZohoApisDC + "/crm/v2/Results/"+v_CrmResultID+"/Attachments/" + v_ThisFileAttachmentID;
        //
        // using a Zoho Oauth connection with scope (ZohoCRM.modules.ALL)
        r_DownloadFile = invokeurl
        [
            url :v_CrmEndpointUrl
            type :GET
            connection:"my_connector"
        ];
        // add record as zoho.loginuser so that only record owners can access this record in report or form
        r_HoldInCreator = insert into Download_File
        [
            Added_User=zoho.loginuser
            Owner_Email=v_ResultEmail
            File_Name=v_ThisFileName
            File_Ref=v_ThisFileCrmID
            Expiry_Date_Time=v_ExpiryTime
            File_Download=r_DownloadFile
        ];
        v_NewCreatorID = if(r_HoldInCreator.trim()!="", r_HoldInCreator.toLong(), 0);
        if(v_NewCreatorID != 0)
        {
            // re-get this record (to get the internal file name)
            r_NewCreatorFile = Download_File[Owner_Email == v_ResultEmail && File_Ref == v_ThisFileCrmID];
            v_LinkToDownload = v_CreatorBaseUrl + "/file" + zoho.appuri + "Download_File_Report/" + v_NewCreatorID + "/File_Download/download/"+v_PublishKey+"?filepath=/" + r_NewCreatorFile.File_Download;
        }
    }
    if(v_ThisFileUrl.trim() != "-")
    {
        v_ThisHtmlLink = "<a href='" + v_LinkToDownload + "'>" + v_ThisFileName + "</a>   <span>(" + v_ThisFileSize + ")</span>";
    }
}
// v_ThisHtmlLink can be displayed on a page as a link or you can put in a Note Field for the user to download just by clicking it.
  1.  // accessing zoho.eu (not zoho.com) so adapt the 2 values 
  2.  v_CreatorBaseUrl = "https://creator.zoho.eu"
  3.  v_ZohoApisDC = "https://www.zohoapis.eu"
  4.  // 
  5.  // get field from CRM 
  6.  l_ResultThisFile = ifnull(r_Row.get("My_CRM_File"),"-")
  7.  for each  m_ThisFile in l_ResultThisFile 
  8.  { 
  9.      v_ThisFileCrmID = ifnull(m_ThisFile.get("file_Id"),"-")
  10.      v_ThisFileName = ifnull(m_ThisFile.get("file_Name"),"-")
  11.      v_ThisFileUrl = ifnull(m_ThisFile.get("download_Url"),"-")
  12.      v_ThisFileAttachmentID = ifnull(m_ThisFile.get("attachment_Id"),"-")
  13.      v_ThisFileSize = ifnull(m_ThisFile.get("file_Size"),"-")
  14.      v_ExpiryTime = zoho.currenttime.addMinutes(10).toString("dd-MMM-yyyy HH:mm:ss")
  15.      // 
  16.      // put your own Publish key here (publish the report holding the files to download and you will receive this key) 
  17.      v_PublishKey = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKL"
  18.      // 
  19.      // check if already exists (double-security: Email and File Ref must match) 
  20.      r_CreatorFile = Download_File[Owner_Email == v_ResultEmail && File_Ref == v_ThisFileCrmID]
  21.      if(r_CreatorFile.count()>0) 
  22.      { 
  23.          if(!isnull(r_CreatorFile.File_Download)) 
  24.          { 
  25.              v_LinkToDownload = v_CreatorBaseUrl + "/file" + zoho.appuri + "Download_File_Report/" + r_CreatorFile.ID + "/File_Download/download/"+v_PublishKey+"?filepath=/" + r_CreatorFile.File_Download; 
  26.          } 
  27.      } 
  28.      else 
  29.      { 
  30.          v_CrmEndpointUrl = v_ZohoApisDC + "/crm/v2/Results/"+v_CrmResultID+"/Attachments/" + v_ThisFileAttachmentID; 
  31.          // 
  32.          // using a Zoho Oauth connection with scope (ZohoCRM.modules.ALL) 
  33.          r_DownloadFile = invokeUrl 
  34.          [ 
  35.              url :v_CrmEndpointUrl 
  36.              type :GET 
  37.              connection:"my_connector" 
  38.          ]
  39.          // add record as zoho.loginuser so that only record owners can access this record in report or form 
  40.          r_HoldInCreator = insert into Download_File 
  41.          [ 
  42.              Added_User=zoho.loginuser 
  43.              Owner_Email=v_ResultEmail 
  44.              File_Name=v_ThisFileName 
  45.              File_Ref=v_ThisFileCrmID 
  46.              Expiry_Date_Time=v_ExpiryTime 
  47.              File_Download=r_DownloadFile 
  48.          ]
  49.          v_NewCreatorID = if(r_HoldInCreator.trim()!="", r_HoldInCreator.toLong(), 0)
  50.          if(v_NewCreatorID != 0) 
  51.          { 
  52.              // re-get this record (to get the internal file name) 
  53.              r_NewCreatorFile = Download_File[Owner_Email == v_ResultEmail && File_Ref == v_ThisFileCrmID]
  54.              v_LinkToDownload = v_CreatorBaseUrl + "/file" + zoho.appuri + "Download_File_Report/" + v_NewCreatorID + "/File_Download/download/"+v_PublishKey+"?filepath=/" + r_NewCreatorFile.File_Download; 
  55.          } 
  56.      } 
  57.      if(v_ThisFileUrl.trim() != "-") 
  58.      { 
  59.          v_ThisHtmlLink = "<a href='" + v_LinkToDownload + "'>" + v_ThisFileName + "</a>   <span>(" + v_ThisFileSize + ")</span>"
  60.      } 
  61.  } 
  62.  // v_ThisHtmlLink can be displayed on a page as a link or you can put in a Note Field for the user to download just by clicking it. 

Additional:
  • Why not set a scheduled task on the expiry date/time to delete this entry for both house-keeping and security?
    Zoho Creator - Setup Scheduled Task - On Date Expirywith the following deluge
    copyraw
    if(!isnull(input.ID))
    {
    	delete from Download_File[ID == input.ID];
    }
    1.  if(!isnull(input.ID)) 
    2.  { 
    3.      delete from Download_File[ID == input.ID]
    4.  } 

Category: Zoho :: Article: 739

Credit where Credit is Due:


Feel free to copy, redistribute and share this information. All that we ask is that you attribute credit and possibly even a link back to this website as it really helps in our search engine rankings.

Disclaimer: The information on this website is provided without warranty and any content is merely the opinion of the author. Please try to test in development environments prior to adapting them to your production environments. The articles are written in good faith and, at the time of print, are working examples used in a commercial setting.

Thank you for visiting and, as always, we hope this website was of some use to you!

Kind Regards,

Joel Lipman
www.joellipman.com

Related Articles

Joes Revolver Map

Accreditation

Badge - Certified Zoho Creator Associate
Badge - Certified Zoho Creator Associate

Donate & Support

If you like my content, and would like to support this sharing site, feel free to donate using a method below:

Paypal:
Donate to Joel Lipman via PayPal

Bitcoin:
Donate to Joel Lipman with Bitcoin - Valid till 8 May 2022 3QnhmaBX7LQSRsC9hh6Je9rGQKEGNQNfPb
© 2021 Joel Lipman .com. All Rights Reserved.