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:
// 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 } ],
- // 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:
- Create a ZohoCreator form (I'm calling mine in this example: Download File to hold the file and some other fields
- Owner Email (type: Email)
- FIle Name (type: Single Line)
- File Ref (type: Single Line)
- Expiry Date Time (type: Date-Time)
- File Download (type: File upload)
https://creatorapp.zohopublic.eu + zoho.appuri + report-perma/Download_File_Report/abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKL
// 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.
- // 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.
Additional:
- Why not set a scheduled task on the expiry date/time to delete this entry for both house-keeping and security?
with the following deluge