100%

Create Linking Invoice in Zoho Books

Difficulty: Intermediate
Estimated reading time: 5 mins
Estimated implementation time: 1 hour

Overview

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:

  • The newly created invoice will be of the same amount as the primary invoice
  • The Due Date of the newly created invoice will be 14 days before the Event Date, unless the event date is less that 14 days out, in which case the due date will be set to the current date.
  • This will add a URL of the newly created Invoice on the primary Invoice, and the URL of the primary Invoice on the newly created Invoice.
  • This will not work if the items on the Invoice are serialized.

Setup

To make this custom function work, you will need to add two custom fields in Zoho Books:

  • Event Date [Date Type: Date]
  • Linking Invoice [Data Type: URL]
 				 					// 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.

Want to learn even more? Sign up for one of our Zoho courses!


More Free Resources

Deluge Tips – Null Check

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....

Read More »