Zoho Books批量操作自定义按钮报错及功能实现求助
问题排查与解决方案
报错原因
- 批量操作场景下,
invoices是包含多个发票对象的列表/数组,而非单个发票Map对象。原代码直接对invoices调用get()方法,完全不符合Zoho Books的API使用逻辑,这是导致参数类型不匹配报错的核心原因(第6行的invoices.get("total")及其他同类调用均为错误写法)。 - 原代码未实现批量处理逻辑,仅按单个发票的方式取值,无法满足批量发送多条选中发票数据的需求。
修复后的代码
// 获取组织ID(移除重复代码) organizationID = organization.get("organization_id"); // 初始化列表存储批量发票数据 invoiceList = List(); // 遍历所有选中的发票 for each invoice in invoices { // 从单个发票对象中提取字段 invoiceID = invoice.get("invoice_id"); customer_name = invoice.get("customer_name"); customer_id = invoice.get("customer_id"); invoiceTotal = invoice.get("total"); invoice_number = invoice.get("invoice_number"); // 构造单条发票数据Map invoiceData = Map(); invoiceData.put("invoice_id", invoiceID); invoiceData.put("invoice_total", invoiceTotal); invoiceData.put("customer_name", customer_name); invoiceData.put("invoice_number", invoice_number); invoiceData.put("customer_id", customer_id); invoiceData.put("organization_id", organizationID); // 添加到批量列表 invoiceList.add(invoiceData); } // 构造最终请求体 requestBody = Map(); requestBody.put("selected_invoices", invoiceList); // 发送POST请求到Webhook服务器 url = "https://example.com"; response = invokeurl [ url : url type : POST parameters : requestBody headers: {"Content-Type": "application/json"} ]; info response; // 返回结果Map resultMap = Map(); resultMap.put("message", response); resultMap.put("code", 0); return resultMap;
代码说明
- 新增循环遍历逻辑:通过
for each invoice in invoices遍历所有选中的发票,从单个发票对象中提取字段,解决了原代码直接操作数组导致的类型错误。 - 构建批量数据结构:将每条发票数据存入列表后封装到请求体,满足批量发送多条数据的需求。
- 优化代码结构:移除重复的组织ID获取代码,添加
Content-Type请求头确保Webhook服务器能正确解析JSON格式的请求体。
内容的提问来源于stack exchange,提问作者DR ABhay Daga
相关产品推荐
相关产品推荐

