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

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".

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.

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:
// 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

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:
// 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];
            v_LinkToDownload = v_CreatorBaseUrl + "/file" + zoho.appuri + "Download_File_Report/" + r_CreatorFile.ID + "/File_Download/download/"+v_PublishKey+"?filepath=/" + r_CreatorFile.File_Download;
        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
        // add record as zoho.loginuser so that only record owners can access this record in report or form
        r_HoldInCreator = insert into Download_File
        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.

  • 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
    	delete from Download_File[ID == input.ID];

Related Articles

Joes Revolver Map


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:

Donate to Joel Lipman via PayPal

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