Set File Password
Table of Contents
Overview
The setFilePassword task sets a password on a PDF or ZIP file object. When this task is used, the file content is encrypted and can be accessed only by entering the specified password. The encryption is applied directly to the file object stored in the variable.
Note:
- This task does not return any value. The specified password will be applied directly to the file object.
- For PDF files, the password can contain a maximum of 32 characters. If a longer password is provided, only the first 32 characters are used.
- This task cannot be used on files that are already password protected. Existing passwords cannot be modified, replaced, or removed using this task.
Syntax
<file>.setFilePassword(<password>);
where:
| Parameters | Data type | Description |
|---|---|---|
| <file> | FILE | Represents the file object to which the password will be applied. Supported file types: PDF and ZIP. |
| <password> | TEXT | Represents the password to be set on the file. |
Examples
Example 1: Set a password on a PDF file
The following script fetches a PDF file using the invokeURL task and applies a password to it.
// Fetch a PDF file using invokeURL
pdfFile=invokeurl
[
url:"https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf"
type:GET
];
// Set password on the PDF file
pdfFile.setFilePassword("mySecret123");
where:
pdfFile"mySecret123"Example 2: Set a password on a ZIP file
The following script creates a text file, compresses it into a ZIP file, and applies a dynamically generated password to the ZIP file.
// Create a text file and compress it into a ZIP file
fileVariable="Sample file content".toFile("sample.txt");
zipFile=fileVariable.compress("compressed_file");
// Generate password dynamically and set it on the ZIP file
password="Report"+zoho.currentdate.toString("ddMMyyyy");
// Set password on the ZIP file
zipFile.setFilePassword(password);
where:
zipFilepasswordUse Case Scenarios
Case 1: Emailing password-protected payslips on employee request in a self-service portal
Consider an organization that uses a Zoho Creator employee management application to maintain employee information and provide self-service capabilities. In this application, the HR team stores employees' monthly payslips as PDF files in a Payslips form, with each record mapped to the corresponding employee ID.
The application also includes an Employee Self Service Portal, where employees can view their details, raise requests, and perform other self-service activities. Employees may occasionally require a specific month's payslip for purposes such as tax filing, loan applications, or maintaining personal records. Sending these documents as regular email attachments can expose sensitive salary information during transmission.
To address this, a Payslip Request form can be created with fields such as Employee ID, Payslip Month, and Year. A form workflow can then be configured to execute on successful form submission.
When an employee submits a request, the workflow retrieves the corresponding payslip record by matching the employee ID with the requested month and year. Using the record ID of the retrieved record, the payslip PDF is downloaded through the invokeURL task by calling the Zoho Creator File Download API. The downloaded file object is then secured using the setFilePassword task. The password is generated by combining the employee's date of birth in DDMMYYYY format with the employee ID. Finally, the password-protected PDF is sent to the employee using the sendmail task, ensuring that sensitive payroll information is shared securely while allowing employees to conveniently access their payslips whenever required.
The following script demonstrates this workflow:
// Step 1: Get input values from the submitted Payslip Request form
employeeId=input.Employee_ID;
requestedYear=input.Year;
requestedMonth=input.Payslip_Month;
// Step 2: Fetch the employee's date of birth from Employee_Details form
employeeRecord=Employee_Details[Employee_ID==employeeId];
dob=employeeRecord.Date_of_Birth.toString("ddMMyyyy");
// Step 3: Fetch the matching payslip record for the employee, month, and year
payslipRecord=Payslip_Records[Employee_ID==employeeId&&Payslip_Month==requestedMonth&&Year==requestedYear];
if(payslipRecord.count()>0)
{
// Step 4: Fetch the payslip PDF using invokeurl via Zoho Creator File Download API payslipPDF=invokeurl [ url:"https://creator.zoho.com/api/v2/{owner}/hr-portal/report/Payslip_Records_Report/"+payslipRecord.ID+"/Payslip_PDF/download" type:GET connection:"creator_connection" ];
// Step 5: Set password using date of birth and employee ID
password=dob+employeeId; payslipPDF.setFilePassword(password);
// Step 6: Email the password-protected payslip to the employee
sendmail [ from:zoho.adminuserid to:zoho.loginuserid subject:"Your Payslip for "+requestedMonth+" "+requestedYear message:"Please find your requested payslip attached. Use your date of birth in DDMMYYYY format followed by your Employee ID to open the file." attachments:file:payslipPDF ];
}
Case 2: Sending password-protected region-wise lead reports to regional managers
Consider a sales organization that uses Zoho CRM to manage leads across multiple regions. Regional sales managers need a monthly summary of leads generated in their region to plan outreach and track pipeline health. Manually exporting and sharing this data is time-consuming, and sending raw lead data as a regular email attachment risks exposing sensitive prospect information during transmission.
To address this, a custom module called Region_Managers is created in Zoho CRM to maintain the mapping between each region and its corresponding regional manager's email address. A scheduled custom function is configured in Zoho CRM to run on the 1st of every month.
When the scheduled function runs, it fetches all region-manager mappings from the Region_Managers module. For each region, leads created during the previous month are retrieved using the invokeurl task by calling the Zoho CRM Search Records API. The retrieved lead data is compiled into a CSV file, compressed into a ZIP archive, and password-protected using the setFilePassword task. The password is constructed by combining the region code, month, and year. The secured ZIP is then emailed to the respective regional manager using the sendmail task, ensuring that lead reports are shared securely at the start of each month without manual effort.
The following script demonstrates this workflow:
// Step 1: Fetch all region-manager mappings from the Region_Managers custom module
query_data=Collection();
query_data.insert("fields":"Region,Manager_Email");
regionManagers=zoho.crm.v8.getRecords("Region_Managers",1,200,query_data,"crm_connection");
// Step 2: Calculate the start and end dates of the previous month
startDate=zoho.currentdate.subMonth(1).toString("yyyy-MM-dd");
endDate=zoho.currentdate.subDay(1).toString("yyyy-MM-dd");
// Step 3: Loop through each region
for eachregionRecordinregionManagers
{ region=regionRecord.get("Region"); managerEmail=regionRecord.get("Manager_Email"); // Step 4: Fetch leads created in the previous month for this region using invokeurl criteria="(Region:equals:"+region+")and(Created_Time:between:"+startDate+","+endDate+")";
leadResponse=invokeurl [ url:"https://www.zohoapis.com/crm/v8/Leads/search?criteria="+encodeUrl(criteria) type:GET connection:"crm_connection" ];
leads=leadResponse.get("data");
if(leads!=null) { // Step 5: Build CSV content from the fetched lead records fileContent="Lead Name,Email,Phone,Lead Source,Status\n";
for each lead in leads { fileContent=fileContent+lead.get("Full_Name")+","+lead.get("Email")+","+lead.get("Phone")+","+lead.get("Lead_Source")+","+lead.get("Lead_Status")+"\n";
}
// Step 6: Convert content to a file and compress it into a ZIP reportMonth=zoho.currentdate.subMonth(1).toString("MMM"); reportYear=zoho.currentdate.subMonth(1).toString("yyyy"); reportFile=fileContent.toFile("Lead_Report_"+region+"_"+reportMonth+"_"+reportYear+".csv"); zipFile=reportFile.compress("Lead_Report_"+region+"_"+reportMonth+"_"+reportYear);
// Step 7: Set password using region code, month, and year month=zoho.currentdate.subMonth(1).toString("MM"); year=zoho.currentdate.subMonth(1).toString("yyyy"); zipFile.setFilePassword(region+month+year);
// Step 8: Email the password-protected ZIP to the regional manager sendmail [ from:zoho.adminuserid to:managerEmail subject:"Monthly Lead Report - "+region+" "+reportMonth+" "+reportYear message:"Please find the lead report for your region for "+reportMonth+" "+reportYear+" attached. Use your region code followed by the month and year in MMYYYY format to open the file." attachments:file:zipFile ]; }}