Pushing Podio

Fun Experiments pushing Citrix Podio to the limit so you can get more done.
... with a little help from GlobiFlow, ProcFu, and other friends ...

Create an Invoice in Xero Accounting from Podio

- Posted in Uncategorized by

If you use Xero Accounting, I'm sure you've wanted to create invoices in Xero in an automated way. Using GlobiFlow and ProcFu, we can achieve this in Podio.

Connect Xero

The first step is to connect your Xero account to ProcFu.

  1. Log in to developer.xero.com and go to the My Apps page.
  2. Create a new App and make sure it's a Private App (or select an existing one which you have the key certificates for).
  3. In ProcFu, navigate to the Acct page and click on the Connect button next to Xero

  4. Copy the Consumer Key and Secret from your Xero App to ProcFu, and copy the Public Key Certificate from ProcFu to Xero (note that if you're using your own certificate, you need to paste your .cer and .pem certificate file contents into ProcFu).

Contacts

In order to create invoices in Xero, we need to have the ContactID of the contact. To simplify this process, we're going to sync all Xero contacts into a new Podio app. The app would need at minimum:

  • Name (single-line text field)
  • ContactID (single-line text field)

Then we'll use a GlobiFlow flow to bring the contacts over. You can do this as a by-date-or-day flow triggered once a day to keep your data fresh.

The first part of the flow will be to get the contacts from Xero, and then extract just the Contact array from the resulting JSON:

Just in case there are unescapable characters in the JSON from the first post, we're using ProcFu again to parse the JSON and get just the array.

Now that we have a JSON array, we can loop over it, and create the contacts in Podio. As we loop through them, we'll also check to make sure they don't already exist in order to prevent duplication:

Although this flow will run daily, you'll need to trigger it manually in the GlobiFlow UI if you don't want to wait for it to trigger on it's own.

Invoicing App

We'll also need an app in Podio to trigger the generation of invoices. For the sake of this example, we'll keep things simple and just generate single-line-item invoices. The app would need:

  • Contact (relationship field to our Xero Contacts app from above)
  • Due Date (date field)
  • Description (single-line text field)
  • Amount (number field)
  • Run (category field with option "Create Invoice" to initiate the flow that will generate the invoice in Xero)

Preparing Invoice Data

Looking at the Xero API documentation, it shows "Example of minimum elements required to add an approved sales (ACCREC) invoice to ABC Limited"

POST https://api.xero.com/api.xro/2.0/Invoices

{
  "Invoice": {
    "Type": "ACCREC",
    "Contact": { "ContactID": "eaa28f49-6028-4b6e-bb12-d8f6278073fc" },
    "DueDate": "\/Date(1518685950940+0000)\/",
    "LineItems": {
      "LineItem": {
        "Description": "Services as agreed",
        "Quantity": "4",
        "UnitAmount": "100.00",
        "AccountCode": "200"
      }
    },
    "Status": "AUTHORISED"
  }
}

This is WRONG

It seems that the api docs for Xero have some issues. Even pasting their example directly into their API previewer throws an error.

You'll need to play with your payload a little and also refer to the Xero blog for other inspiration. It took quite a few attempts to get the right format. The data you send will also be determined by your accounting settings.

It's a good idea to get some test data working through ProcFu directly first, and then reverse-engineer the payload that worked.

The format that ended up working for our specific setup was the following:

{
  "Invoices": [
    {
      "Type": "ACCREC",
      "Contact": {
        "ContactID": "eaa28f49-6028-4b6e-bb12-d8f6278073fc"
      },
      "DueDate": "2018-03-13",
      "LineItems": [
        {
          "Description": "Services Rendered",
          "Quantity": "1",
          "UnitAmount": 521,
          "TaxType": "NONE",
          "AccountCode": "400"
        }
      ],
      "Status": "AUTHORISED"
    }
  ]
}

Creating Invoices

Now that we know the format we need the data in, we can create a flow to do this.

Our flow will be on update when someone clicks on the Create Invoice category field:

The first thing the flow will need to do is the get the referenced contact record from the Xero contacts app from above so that we can get the contact's ID in Xero:

Next, we create our JSON payload that the Xero API expects in order to generate this invoice:

Now that we have the JSON payload, we can send it to Xero using ProcFu:

We're going to assume that it was successful and that the result includes the invoice, and we'll use ProcFu once more to extract the invoice object for us, and then try to extract the resulting invoice number and invoice ID from the invoice object:

If the invoice was created successfully, we'll now have the invoice number etc, and for now we'll just log it in a comment (you can do whatever you need in your own flow):

Alternatively, if invoice creation failed, we won't have an invoice number, and we'll just report whatever the API returned during the create invoice call:

Wrap-up

And that's it. You can now create invoices in Xero automatically using Podio data.

Using the same principles, you could also create contacts, PO's, etc. Anything that the Xero API allows you to do, you can now do from Podio.

Happy Accounting!

Comments