This custom function will create an additonal invoice based on the Event Date of a previous Invoice. Here are some details of how it works:
To make this custom function work, you will need to add two custom fields in Zoho Books:
// Org and Invoice Info organizationId = organization.get("organization_id"); invoiceId = invoice.get("invoice_id"); // Primary Invoice Info primaryInvoiceSearch = zoho.books.getRecordsByID("Invoices",organizationId,invoiceId); primaryInvoice = primaryInvoiceSearch.get("invoice"); info "Primary Invoice:"; info primaryInvoice; info "|++++++++++++++++++++++++++++++++++++++++|"; // Process Custom Fields primaryInvoiceCustomFields = primaryInvoice.get("custom_fields"); primaryInvoiceUrl = "https://books.zoho.com/app#/invoices/" + invoiceId; eventDate = null; linkingInvoice = null; for each customField in primaryInvoiceCustomFields { if(customField.get("placeholder") == "cf_event_date") { eventDate = customField.get("value"); } else if(customField.get("placeholder") == "cf_linking_invoice") { linkingInvoice = customField.get("value"); } } // Create Secondary Invoice secondaryInvoiceMap = Map(); secondaryInvoiceMap.put("customer_id",primaryInvoice.get("customer_id")); // Set Due Date to Two Weeks Before Event Date secondaryDueDate = null; if(primaryInvoice.get("due_date").daysBetween(eventDate) >= 14) { secondaryDueDate = eventDate.subDay(14); } else { secondaryDueDate = zoho.currentdate; } secondaryInvoiceMap.put("due_date",secondaryDueDate); secondaryInvoiceMap.put("salesperson_id",primaryInvoice.get("salesperson_id")); secondaryInvoiceMap.put("custom_fields",{{"label":"Linking Invoice","value":primaryInvoiceUrl},{"Label":"Event Date","value":eventDate}}); // Set Line Items lineItemsList = List(); for each lineItem in primaryInvoice.get("line_items") { lineItem.deleteKey("line_item_id"); lineItemsList.add(lineItem); } secondaryInvoiceMap.put("line_items",lineItemsList); // Create Secondary Invoice createSecondaryInvoice = zoho.books.createRecord("Invoices",organizationId,secondaryInvoiceMap); info "Create Secondary Invoice:"; info createSecondaryInvoice; info "|++++++++++++++++++++++++++++++++++++++++|"; secondaryInvoice = createSecondaryInvoice.get("invoice"); secondaryInvoiceId = secondaryInvoice.get("id"); // Update Primary Invoice with Linking Invoice URL secondaryInvoiceUrl = "https://books.zoho.com/app#/invoices/" + secondaryInvoiceId; updatePrimaryInvoiceMap = Map(); updatePrimaryInvoiceMap.put("custom_fields", {{"label":"Linking Invoice","value":secondaryInvoiceUrl}}); updatePrimaryInvoice = zoho.books.updateRecord("Invoices", organizationId, invoiceId, updatePrimaryInvoiceMap); info "Update Primary Invoice:"; info updatePrimaryInvoice; info "|++++++++++++++++++++++++++++++++++++++++|";
Click here to copy these scripts. For more Zoho-wizardry, check out our GitHub page.
When scripting, ask yourself this question – will this value ever be null? If it’s a yes, that’s a place for a null check! Here are 3 tips and best practices to help you kickstart the habit....
Learn to create a customized inventory report with Zoho Analytics. This tutorial contains a link to our GitHub page for SQL code that will help with your table creations....
How nice would it be if you could, at the press of a button, send clients an email w/a Zoho Books invoice? Replete with “Pay Now” buttons that link to PayPal/Stripe/other payment gateways?...
Whether you work primarily out of CRM or the Zoho Finance Suite, you can use Analytics to build commissions dashboards. This involves some fairly simple SQL code....
If you are an inventory manager, this blog post could change your life. If you are not, it will at least teach you how to build some wicked inventory tracking for your business....
Convert fields, related activities, attachments, notes and more from one record to another across modules via custom function....