
Create Linking Invoice in Zoho Books
- Peter
- Difficulty: Intermediate
- Estimated reading time: 5 mins
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:
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.
Need help with Zoho consulting or Zoho training and education? We’ve got all your Zoho needs covered!