Automatically adding Bank Details in Invoices

Automatically adding Bank Details in Invoices

Hi all.

I am newbie.
I just trying to adding an automation to invoices creation. First I created 2 custom fields into invoices ("Banca" that is a list field and "IBAN" that is a text field). Second, I create a Custom Function as below and then a trigger into automations on changing the field "Banca" into a new or updated invoce.

This function should read the value of "Banca" field. If the value exists into the ibanList Json, the "iban" field is updated with the value of the JSON field "iban".

  1. ibanList = {"UBS EUR":{"iban":"IBAN EUR"},"UBS CH":{"iban" : "IBAN CH"}};

  2. invoiceID = invoice.get("invoice_id");
  3. organizationID = organization.get("organization_id");
  4. customFields = invoice.get("custom_fields");
  5. //info customFields;
  6. for each  customField in customFields
  7. {
  8. if(customField.get("placeholder").toString().equalsIgnoreCase("cf_banca"))
  9. {
  10. recordId = customField.get("value_formatted");
  11. info "'" + recordId + "'";
  12. record = ibanList.get(recordId);
  13. if(record == false) {
  14. info "IBAN Error: " + recordId;
  15. break;
  16. }
  17. iban = record.get("iban");
  18. CustomFields = List();
  19. CustomField = Map();
  20. CustomField.put("label","IBAN");
  21. CustomField.put("value",iban);
  22. CustomFields.add(CustomField);
  23. updatedInvoice = Map();
  24. updatedInvoice.put("custom_fields",CustomFields);
  25. updatedInvoice.put("notes","Dettagli IBAN: " + iban);
  26. info updatedInvoice;
  27. response = zoho.books.updateRecord("invoices",organizationID,invoiceID,updatedInvoice,"zoho_books");
  28. info response;
  29. }
  30. }
This works, but with a delay. So if I create a new invoice, select the field I need in the "Banca" field, and then Save and Send the invoice, i will receive the invoice PDF but without the "Iban" field with the correct value. What I notice is that the automation get a few seconds to be started and the the email is sent before the automation is done. Am I right ? If yes I think this could be an issue but I am hoping I doing the things I need in the wrong way.
Can you give me any suggestion ?

Thanks a lot