A quick article on how to get around this error.
Why?
The use-case here is that I am using the send invoice API and I kept getting this error despite including the customer_id and there was a primary contact on the customer record.
This is from within ZohoCreator, requesting for an invoice in ZohoBooks to be sent.
How?
So the key here is that there were no contact persons on the customer record. Instead, when building the invoice, this had to be added (don't ever remember having to do this) as contact_ids (array/list). Instead the below script will show you how to use this API having a few email addresses as the recipients:
A sample code snippet:
For the below, I have a connection to ZohoBooks from ZohoCreator called "zbooks". It has fullaccess as a scope but I'm sure you can lock it down to just the scopes you need.
void ZohoBooks.fn_SendZohoBooksInvoice(int p_OrderID) { v_OrderID = ifnull(input.p_OrderID,0).toLong(); /* ******************************************************************************* Function: void ZohoBooks.fn_SendZohoBooksInvoice(int p_OrderID) Trigger: On Demand when button in report is clicked Purpose: This will send a ZohoBooks invoice directly to its recipient Inputs: Zoho Creator record ID of the order Outputs: - Date Created: 2023-03-06 (Joel Lipman) - Initial release Date Modified: ??? - ??? More Info: Error 7008: There are no contact persons associated with this Invoice. - Encountered when trying to use the send an invoice API - Need the generated invoice to pre-select a contact person to email - Superceded by including "send=true" in the invoice creation request More Info: Error 1038: JSON is not well formed - Encountered when trying to use the send an invoice API - Was sending to_mail_ids in the URL instead of the body parameters ******************************************************************************* */ v_BooksOrgID = "12345678901"; v_BooksInvoiceID = 0; v_ZB_CustomerID = 0; // // get this order details from ZohoCreator c_ThisOrder = Order[ID == v_OrderID]; // // get this customer record from ZohoCreator c_ThisCustomer = Customer[ID == c_ThisOrder.Customer]; if(c_ThisCustomer.count() > 0) { v_CustomerName = c_ThisCustomer.Customer; if(!c_ThisCustomer.ZohoBooks_Customer_ID.isBlank()) { v_ZB_CustomerID = c_ThisCustomer.ZohoBooks_Customer_ID; } } if(c_ThisOrder.count() > 0) { // // get invoice details if(!c_ThisOrder.ZohoBooks_Invoice_ID.isBlank()) { v_BooksInvoiceID = c_ThisOrder.ZohoBooks_Invoice_ID; r_ZB_InvoiceDetails = zoho.books.getRecordsByID("invoices",v_BooksOrgID,v_BooksInvoiceID.toString(),"zbooks"); if(r_ZB_InvoiceDetails.get("invoice") != null) { m_ZB_InvoiceDetails = r_ZB_InvoiceDetails.get("invoice"); // // determine recipients l_ZB_InvoiceRecipients = List(); r_ZohoBooks_CustomerDetails = zoho.books.getRecordsByID("contacts",v_BooksOrgID,m_ZB_InvoiceDetails.get("customer_id"),"zbooks"); if(r_ZohoBooks_CustomerDetails.get("contact") != null) { if(!r_ZohoBooks_CustomerDetails.get("contact").get("email").isBlank()) { l_ZB_InvoiceRecipients.add(r_ZohoBooks_CustomerDetails.get("contact").get("email")); } if(r_ZohoBooks_CustomerDetails.get("contact").get("contact_persons") != null && l_ZB_InvoiceRecipients.isEmpty()) { l_ZB_ContactPersons = r_ZohoBooks_CustomerDetails.get("contact").get("contact_persons"); for each m_ZB_Person in l_ZB_ContactPersons { if(!m_ZB_Person.get("email").isBlank() && l_ZB_InvoiceRecipients.isEmpty()) { l_ZB_InvoiceRecipients.add(m_ZB_Person.get("email")); } } } } // // email the invoice (using template and updating comments/history) - also marks as sent m_Params = Map(); // recipients is a list of emails unlike contact persons in invoice request m_Params.put("to_mail_ids",l_ZB_InvoiceRecipients); v_Endpoint = "https://www.zohoapis.com/books/v3/invoices/" + v_BooksInvoiceID + "/email?organization_id=" + v_BooksOrgID; r_SendInvoice = invokeurl [ url :v_Endpoint type :POST parameters:m_Params.toString() connection:"zbooks" ]; info "------------"; // info "Send Invoice Request:"; // info m_Params; // info v_Endpoint; // info "Send Invoice Response:"; info r_SendInvoice; } // // no need to mark as sent as sending via the above will automatically do that in ZohoBooks. } } }
- void ZohoBooks.fn_SendZohoBooksInvoice(int p_OrderID)
- {
- v_OrderID = ifnull(input.p_OrderID,0).toLong();
- /* *******************************************************************************
- Function: void ZohoBooks.fn_SendZohoBooksInvoice(int p_OrderID)
- Trigger: On Demand when button in report is clicked
- Purpose: This will send a ZohoBooks invoice directly to its recipient
- Inputs: Zoho Creator record ID of the order
- Outputs: -
- Date Created: 2023-03-06 (Joel Lipman)
- - Initial release
- Date Modified: ???
- - ???
- More Info: Error 7008: There are no contact persons associated with this Invoice.
- - Encountered when trying to use the send an invoice API
- - Need the generated invoice to pre-select a contact person to email
- - Superceded by including "send=true" in the invoice creation request
- More Info: Error 1038: JSON is not well formed
- - Encountered when trying to use the send an invoice API
- - Was sending to_mail_ids in the URL instead of the body parameters
- ******************************************************************************* */
- v_BooksOrgID = "12345678901";
- v_BooksInvoiceID = 0;
- v_ZB_CustomerID = 0;
- //
- // get this order details from ZohoCreator
- c_ThisOrder = Order[ID == v_OrderID];
- //
- // get this customer record from ZohoCreator
- c_ThisCustomer = Customer[ID == c_ThisOrder.Customer];
- if(c_ThisCustomer.count() > 0)
- {
- v_CustomerName = c_ThisCustomer.Customer;
- if(!c_ThisCustomer.ZohoBooks_Customer_ID.isBlank())
- {
- v_ZB_CustomerID = c_ThisCustomer.ZohoBooks_Customer_ID;
- }
- }
- if(c_ThisOrder.count() > 0)
- {
- //
- // get invoice details
- if(!c_ThisOrder.ZohoBooks_Invoice_ID.isBlank())
- {
- v_BooksInvoiceID = c_ThisOrder.ZohoBooks_Invoice_ID;
- r_ZB_InvoiceDetails = zoho.books.getRecordsByID("invoices",v_BooksOrgID,v_BooksInvoiceID.toString(),"zbooks");
- if(r_ZB_InvoiceDetails.get("invoice") != null)
- {
- m_ZB_InvoiceDetails = r_ZB_InvoiceDetails.get("invoice");
- //
- // determine recipients
- l_ZB_InvoiceRecipients = List();
- r_ZohoBooks_CustomerDetails = zoho.books.getRecordsByID("contacts",v_BooksOrgID,m_ZB_InvoiceDetails.get("customer_id"),"zbooks");
- if(r_ZohoBooks_CustomerDetails.get("contact") != null)
- {
- if(!r_ZohoBooks_CustomerDetails.get("contact").get("email").isBlank())
- {
- l_ZB_InvoiceRecipients.add(r_ZohoBooks_CustomerDetails.get("contact").get("email"));
- }
- if(r_ZohoBooks_CustomerDetails.get("contact").get("contact_persons") != null && l_ZB_InvoiceRecipients.isEmpty())
- {
- l_ZB_ContactPersons = r_ZohoBooks_CustomerDetails.get("contact").get("contact_persons");
- for each m_ZB_Person in l_ZB_ContactPersons
- {
- if(!m_ZB_Person.get("email").isBlank() && l_ZB_InvoiceRecipients.isEmpty())
- {
- l_ZB_InvoiceRecipients.add(m_ZB_Person.get("email"));
- }
- }
- }
- }
- //
- // email the invoice (using template and updating comments/history) - also marks as sent
- m_Params = Map();
- // recipients is a list of emails unlike contact persons in invoice request
- m_Params.put("to_mail_ids",l_ZB_InvoiceRecipients);
- v_Endpoint = "https://www.zohoapis.com/books/v3/invoices/" + v_BooksInvoiceID + "/email?organization_id=" + v_BooksOrgID;
- r_SendInvoice = invokeUrl
- [
- url :v_Endpoint
- type :POST
- parameters:m_Params.toString()
- connection:"zbooks"
- ];
- info "------------";
- // info "Send Invoice Request:";
- // info m_Params;
- // info v_Endpoint;
- // info "Send Invoice Response:";
- info r_SendInvoice;
- }
- //
- // no need to mark as sent as sending via the above will automatically do that in ZohoBooks.
- }
- }
- }
Additional
You can include a parameter in the request when creating an invoice which is sent = true which will automatically send it and mark it as sent. As the function I use before this to generate the invoice, also updates an invoice, I wanted to keep the action of sending separate so that staff can control that without the worry of updating an invoice and re-sending it.