Extract contents of a ZIP file and email the extracted files

Extract contents of a ZIP file and email the extracted files

Requirement  

Use Deluge to extract a ZIP file and email the extracted files with the click of a button.

Use Case  

An HR management application contains three forms: Add Employee , Employee Payslips , and Download My Payslips . The Add Employee form maintains the profiles of all employees. The Employee Payslips form is used to store payslips of all employees. At the end of each year, the HR (admin) uploads payslips of all employees as zip files into the Employee Payslips form. This form is accessible only by the admin. However, the employees can get their payslips using the Download My Payslips stateless form. When an employee requests for a payslip using the form, the selected month's payslip will be extracted from the zip file of the selected year (stored in Employee Payslips form) and sent as an email to the employee's email address.
 

Steps to follow  

1. Create two forms with the following details.
Form
Form Link Name
Field Name
Field Link Name
Field Type
Add Employee
Add_Employee
Official Email
Official_Email
Email
Employee ID
Employee_ID
Number
This use case requires only the above-mentioned fields. You can include as many fields as required.
Employee Payslip
Employee_Payslip
Employee Email
Employee_Email
Email
 
 
Payslips
  • Year
  • Payslip ZIP
Payslips
  • Year_field
  • Payslip_ZIP
Subform
  • Number
  • File Upload
Download My Payslip ( Stateless form )
Download_My_Payslip
Employee ID
Employee_ID
Lookup (Add_Employee)
Enter Year
Enter_Year
Number
Select Month
Select_Month
Dropdown
( import predefined choices for Months of the year )
 
We have created the Download My Payslip form as stateless because we do not require the data provided to the form be stored in the application.
In order to create a stateless form , you will need to create a regular form  with required fields and duplicate the form with the "Data will be stored in Zoho Creator" checkbox unchecked.
 
2. Remove the Submit and Reset default buttons and add a button named Get Payslip in the Download  My  Payslip stateless form. You can also choose to retain the Reset button , if required.
 
3.  Create workflow  with the following details.

4. Save the following script in the Deluge Editor:
  1. // Disable field
  2. disable Employee_ID;

  3. // Assign the record ID of the logged in employee's record in Add_Employee form to the Employee_ID lookup field
  4. input.Employee_ID = Add_Employee[Official_Email == zoho.loginuserid].ID;

5. Create a connection to Zoho Creator service named "creator_oauth_connection". This connection will be used in the Deluge script in step 7  to download file.
 
6.  Create another workflow with the following details.
 
7.  Click Add New Action and select Deluge script. Save the following Deluge script in the Deluge editor:
  1. // Fetch employee record of the logged in employee from Employee_Payslip Form
  2. empRecord = Employee_Payslip[Employee_Email == zoho.loginuserid];

  3. //Iterate through the subform that holds employee's payslips as zip files 
  4. for each subformRow in empRecord.Payslips
  5. {

  6.  //Find the subform row that holds the payslip zip file of the requested year
  7.  if(subformRow.Year_field == Enter_Year)
  8.  {

  9. //Download the requested year's payslip zip file using Zoho Creator V2 API
  10. //Replace <app_admin_name> and <application_link_name> with appropriate values
  11. zip_file = invokeurl
  12. [
  13. url :" https://creator.zoho.com/api/v2/<app_admin_name>/<application_link_name>/report/All_Employee_Payslips/" + empRecord.ID + "/Payslips.Payslip_ZIP/" + subformRow.ID + "/download"
  14. type :GET
  15. connection:"creator_oauth_connection"
  16. ];

  17. // Extract the zip file to get pdf files containing payslip of individual months
  18. extractedFiles = zip_file.extract();

  19. //The extracted files are returned as Map in which the keys are file name and values are their corresponding pdf file
  20. //Assuming the file names are stored in the format EmployeeID-Month-Year.pdf, construct file name of the requested payslip
  21. fileName = input.Employee_ID.Employee_ID + "-" + input.Select_Month + "-" + input.Enter_Year + ".pdf";

  22. /Get the required payslip from the extracted file
  23. requestedFile = extractedFiles.get(fileName);

  24. //Send the requested payslip as mail
  25. sendmail
  26. [
  27.  from :zoho.adminuserid
  28.  to :zoho.loginuserid
  29.  subject :"Payslip" + "-" + input.Select_Month + " " + input.Enter_Year
  30.  message :"Please find your requested payslip in the attachments"
  31.  Attachments :file:requestedFile
  32. ]

  33. //Display an acknowledgement message
  34. alert "The requested payslip has been sent as email";
  35.  }

See how it works

Points to note  

  • The Deluge script provided in this tutorial is specific to the ZIP files that contai s 12 PDF files each corresponding to months of a year. For the script to work as expected, the filenames of the PDF files must be in the format: <employeeID>-<month>-<year>.pdf .

    Example: 2406-January-2020

    Please check the attachments for the sample file on which the provided Deluge script works.