Overall Problem
I needed a solution to automatically calculate and send employee payslip information stored on Zoho CRM, to each of our employees at the end of each month based on their salary, allowances and sales commissions (sales commissions - which may change each pay period based on deals completed)
Solution
I create a module on Zoho CRM called "Payslip Records", which contains records for each employee with their personal information, total sales commissions, allowances, net salary etc.
I then created a payslip template in Zoho Writer, and used the merge fields from the CRM "Payslips Module" to populate the fields on the payslip in Zoho Writer. If I run this manually from Zoho CRM, it works - the fields are merged and I can send the payslip to the employees via email.
Automating Challenge - Merge and Send Function in Zoho CRM not Merging Fields
So that the process could be automated with a workflow, I created a function which seems to be working - teh console log returns successful. However, when the function runs and the email is sent off I receive blank payslip templates in my inbox. Here's a look at my code below, can anyone explain why this might be happening that's wrong?
- payslipRecords = zoho.crm.getRecordById("Payslip_Records",payslipId,"crmwriter");
- fields = zoho.writer.getMergeFields("cpqhxd8f21b4c97794e3089ed074e21b45a51","crmwriter");
- info "fields : " + fields;
- info "payslipRecords : " + payslipRecords;
- data = Map();
- //data.put("First_Name", payslipRecords.get("Account_Name").get("name"));
- //data.put("Email", payslipRecords.get("Account_Name").get("id"));
- payslips = list();
- payslipfield = Map();
- payslipfield.put("Payslip_Records. Employee Name",ifnull(payslipRecords.get("Name"),""));
- payslipfield.put("Payslip Records. Department",ifnull(payslipRecords.get("Department"),""));
- payslipfield.put("Payslip Records. Pay Period",ifnull(payslipRecords.get("Pay_Period"),""));
- payslipfield.put("Payslip Records. Date of Payment",ifnull(payslipRecords.get("Date_of_Payment"),""));
- payslipfield.put("Payslip Records. Salary",ifnull(payslipRecords.get("Salary"),""));
- payslipfield.put("Payslip Records. Upfront Commissions",ifnull(payslipRecords.get("Upfront_Commissions"),""));
- payslipfield.put("Payslip Records. Recurring Commissions",ifnull(payslipRecords.get("Recurring_Commissions"),""));
- payslipfield.put("Payslip Records. Internet",ifnull(payslipRecords.get("Internet"),""));
- payslipfield.put("Payslip Records. Net Pay",ifnull(payslipRecords.get("Net_Pay"),""));
- payslips.add(payslipfield);
- data.put("Payslip_Records",payslips);
- mergedata = Map();
- mergedata.put("merge_data",{"data":data});
- mergedata.put("subject","Payslip Due");
- //optionalDF
- mergedata.put("message","Please find your payslip attached.");
- info "mergedata : " + mergedata;
- update = zoho.writer.mergeAndSend("cpqhxd8f21b4c97794e3089ed074e21b45a51","pdf",payslipRecords.get("Email"),mergedata,"crmwriter");
- info "update : " + update;
Console Information
Info
- fields : {"rl_fields":[],"fields":["Employee_Name"]}
- payslipRecords : {"Salary":1000,"Owner":{"name":"John Doe","id":"4035844000008772172","email":"john@email.com"},"Email":"john@email.com","$currency_symbol":"Bds$","Net_Pay":1260.45,"$review_process":{"approve":false,"reject":false,"resubmit":false},"Name":"John Doe","Last_Activity_Time":null,"Record_Image":null,"Modified_By":{"name":"John Doe","id":"4035844000008772172","email":"john@email.com"},"Department":"Operations","$review":null,"$state":"save","Unsubscribed_Mode":null,"$process_flow":false,"Exchange_Rate":1,"Currency":"BBD","id":"4035844000024583003","$approved":true,"Recurring_Commissions":10.45,"$approval":{"delegate":false,"approve":false,"reject":false,"resubmit":false},"Modified_Time":"2021-02-23T10:20:21-04:00","Created_Time":"2021-02-23T10:20:21-04:00","Unsubscribed_Time":null,"$editable":true,"Date_of_Payment":"2021-02-28","$orchestration":false,"$in_merge":false,"Tag":[],"Internet":50,"Upfront_Commissions":200,"$approval_state":"approved","Pay_Period":"2021-02-15"}
- mergedata : {"merge_data":{"data":{"Payslip_Records":[{"Payslip_Records. Employee Name":"John Doe","Payslip Records. Department":"Operations","Payslip Records. Pay Period":"2021-02-15","Payslip Records. Date of Payment":"2021-02-28","Payslip Records. Salary":1000,"Payslip Records. Upfront Commissions":200,"Payslip Records. Recurring Commissions":10.45,"Payslip Records. Internet":50,"Payslip Records. Net Pay":1260.45}]}},"subject":"Payslip Due","message":"Please find your payslip attached."}
- update : {"merge_report_data_url":"https://docs.zoho.com/writer/api/v1/merge/job/aa5fea7ce716f6854f78ce450bf8333c67f0dd47c7f5a6b3f6fe5644fe18afcc/data","merge_report_url":"https://docs.zoho.com/writer/v1/mailmerge/job/aa5fea7ce716f6854f78ce450bf8333c67f0dd47c7f5a6b3f6fe5644fe18afcc","records":[{"download_link":"https://docs.zoho.com/writer/api/v1/merge/job/aa5fea7ce716f6854f78ce450bf8333c67f0dd47c7f5a6b3f6fe5644fe18afcc/record/aa5fea7ce716f6854f78ce450bf8333c9587b8912fcf060c77d27971ad2eecd9/attachment/1/download","SEQUENCE":1,"Payslip_Records":[{"Payslip Records. Net Pay":1260.45,"Payslip Records. Recurring Commissions":10.45,"Payslip Records. Department":"Operations","Payslip_Records. Employee Name":"John Doe","Payslip Records. Internet":50,"Payslip Records. Pay Period":"2021-02-15","Payslip Records. Date of Payment":"2021-02-28","Payslip Records. Upfront Commissions":200,"Payslip Records. Salary":1000}],"id":"758541b4-e20f-4275-a434-fe32553c3b65","status":"inprogress"}]}
Function executed successfully
Please help if you can, thanks! I've attached images of the merge template with the field ID's in Zoho Writer for reference.