Pushing Podio
Search
💳

Create an Invoice in Xero Accounting from Podio

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.
  1. Create a new App and use https://procfu.com/authenticate.php as the redirect URL
  1. In ProcFu, navigate to the Configuration page and click on the Add API button in the API Services tile
  1. Fill in the required details
    • Client ID: from your App in Xero
    • Client Secret: from your App in Xero
    • Scopes: openid profile email offline_access accounting.transactions accounting.contacts
  1. Get a User Login Link (username can be anything - but remember it for future)
  1. Go to the URL generated in step 5 and authorize your user
  1. Do a raw request to https://api.xero.com/connections
  1. Record the tenantId from the step above - you'll need this going forward

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:

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.

Note that we're using the tenantId re retrieved initiall.

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:

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 (again using the tenantId we retrieved initially)

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!

(c) 2020 Globi Web Solutions | Join the Discussion in our Podio Workspace.