A peek into Deluge's file functions
Hello everyone,
Recognizing the importance of files in Zoho products, Deluge implements FILE as a unique type of data. It also comes with a set of built-in functions that operate on FILE data type to identify the properties of files, zip or unzip files, and more. In this post, we will cover Deluge file functions that let you work with your digital documents using Deluge.
Files in Deluge
Files in Deluge must be fetched from the web or cloud service. It doesn't operate on offline files. The following are the ways you can bring files into Deluge scripting:
Irrespective of the format, all the files fetched using any of the above-mentioned methods, except the "fetch records" task and "input" keyword, are treated as file objects (or FILE data type). For example, be it a .pdf format file or a .jpeg format file, both are categorized under FILE data type. The files fetched using the "fetch records" task and "input" keyword are not considered as FILE data type, and hence file functions cannot be applied on them. However, such files can be used in integration tasks (for example in attach file crm task), or in the send mail task to send as a file attachment, or to assign the file as a value to another Zoho Creator file field.
File functions
File functions are built-in functions that operate on file objects (or FILE datatype). These functions cannot be applied on files fetched using the Zoho Creator "fetch records" task or "input" keyword.
The following table lists the file functions available in Deluge:
Function
| Description
|
| Checks if a variable is of FILE data type.
|
| Converts a TEXT or COLLECTION value into file object. This function is supported in all Zoho services except Creator.
|
| Returns the name of the input file.
|
| Returns the size of the input file.
|
| Returns the content of the input file.
|
| Returns the type of the input file. This function is supported in all Zoho services except Creator.
|
| Renames the input file with the specified name.
|
| Compresses one or more file into a single zip file.
|
| Extracts the content of the input zip file.
|
Example 1: Uses setFileName and compress functions
Imagine you want to rename a few documents, convert them into a single zip file, and send the compressed zip file as an email attachment. The following example performs these actions in the below order:
- Fetches an invoice from Zoho Books.
- Generates file name based on the customer name and current date & time, and renames the fetched invoice using the setFileName function.
- Fetches the customer's contract from Zoho Workdrive.
- Generates file name based on the customer name and current date & time, and renames the fetched contract using the setFileName function.
- Converts the fetched invoice and contract documents into a zip file using compress function.
- And lastly, sends the renamed file as attachment of sendMail task.
Step 1:
Step 2:
- // Generate the file name with customer name and current time. Here, customer_name is the input parameter of the custom function
- generated_pdf_name = customer_name + "_invoice_" + zoho.currentTime.toString("dd-MMM-yyyy_hh:mm");
- // Rename the file with the generated file name
- invoice_pdf.setFileName(generated_pdf_name);
Step 3:
Step 4:
- // Generate the file name with customer name and current time. Here, customer_name is the input parameter of the custom function
- generated_doc_name = customer_name + "_contract_" + zoho.currentTime.toString("dd-MMM-yyyy_hh:mm");
- // Rename the file with the generated file name
- contract.setFileName(generated_doc_name);
Step 5:
- // Create a list to add files that need to be compressed
- filesList = List();
- filesList.add(invoice_pdf);
- filesList.add(contract);
- // Perform compress function
- zip_file = filesList.compress("invoice");
Step 6:
- // Send email with the compressed file as attachment
- sendmail
- [
- from: zoho.loginuserid
- to: customer_email
- subject: "Invoice"
- message: "Please check attachment to find your invoice "
- attachments: file: zip_file
- ]
Example 2: Uses extract and getFileContent functions
The following example extracts .csv files from a .zip file and uploads the content of the extracted files into a Zoho Sheet worksheet:
Step 1:
- // Extract .csv files from .zip file. file_collection holds the extracted files as KEY-VALUE collection
- file_collection = compressed_file.extract();
Step 2:
- // Iterate through the collection to access individual .csv files
- for each file in file_collection
- {
- // Fetch the content of the extracted .csv file
- content = file.getFileContent();
- // Use Sheet integration task to insert the fetched CSV into the specified Zoho Sheet
- response = zoho.sheet.insertCSV("6fbyeXXXXXXXXXXXXXXXXXXXXXXXXXXX44b56", "Sheet1", content);
- }
Learn more about file functions from our help documents and do let us know your questions and suggestions in the comments.
Sticky Posts
Function #41: Sync Associated Subforms!
Welcome back everyone! The last custom function showed how to update a Contact with Product details from it's Related list in Deals. This week, let's look at a function that lets you update subform records in two modules simultaneously when one of them is updated. Business scenario Let's look at how subform helps in an education institution that has deployed Zoho CRM. National Public School, Austin (made up, of course!) has set up Zoho CRM and it follows the same relationship pattern of "Students",