Create Linking Invoice in Zoho Books

Invoice to Serialized Equipment

This custom function will create an additional invoice based on the Event Date of a previous Invoice in Zoho Books. 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.

Need help with Zoho consulting or Zoho training and education? We’ve got all your Zoho needs covered!

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

Want to learn how to create Invoice and collect payment from Zoho Creator?

Or learn how complex assignment rules config Via Zoho CRM modules works?

Want this functionality, but don't want to do it yourself?

Click here to speak to us!

Need help with your Zoho system?

Hire a full time Zoho System Admin from our poolof highly qualified program graduates!

Related Resources