Salesforce & Xero Integration in 2024 | All You Need to Know
This article is prepared by our Salesforce Developer Lisa Vasilieva.
Xero is a cloud-based accounting software for small and medium-sized businesses. This system gives the ability to work with bank feeds, invoicing, accounts payable, expense claims, fixed asset depreciation, purchase orders, bank reconciliations, and standard business and management reporting.
There are two options on how you can try out Xero:
1. Trial organization, where everything can be set up from the scratch.
2. Demo Company with predefined data with almost all Xero features except for automatic bank feeds and the ability to invite other users.
We can establish a connection between Salesforce and Xero to migrate records of different objects, email invoices to the client, etc.
For establishing connections we need to create a connected app. There are 3 types of integrations provided while creation:
- Web app with (standard auth code).
- Mobile or desktop app (auth code with PKCE).
- Connected app (premium one-to-one integration that utilises the client credentials grant type and available only to Xero organizations in UK, Australia and New Zealand).
One of the things that I’ve found is that for one tenant you can have only 2 active web apps. In the next steps you will see how we can connect Xero and Salesforce using a web app.
Xero Side (App Creation)
- Open the following link: https://developer.xero.com/app/manage.
- Click the New App button.
- Specify App Name, Company or Application URL, Redirect URI. In the first step Redirect URI can be any.
- After Auth Provider creation in Salesforce this value will be replaced with a callback URL.
- After the app is created, open Configuration on the left panel and click Generate Secret.
Don't forget to check out: Why Zero Code App is The Best Option For WhatsApp Integration With Salesforce?
- Copy Client Id and Client Secret, save them somewhere, because you will need them in the next step.
Salesforce Side
Auth Provider Creation
In Setup find Auth Providers and create a new one with the type Open ID Connect. Specify necessary information and save.
- Authorize Endpoint URL: https://login.xero.com/identity/connect/authorize.
- Token Endpoint URL: https://identity.xero.com/connect/token.
- Copy Callback URL from created auth provider and replace Redirect URIs in Xero app with it.
Named Credentials Creation
In Setup find Named Credentials. Use the drop-down menu next to the New button, choose New Legacy. Specify all parameters like on the screen below:
- Scope: offline_access accounting.settings openid profile email accounting.transactions accounting.transactions.read accounting.contacts.
- URL: https://api.xero.com.
- Before saving the named credential be sure that you logged in to the Xero.
- Click Save. After that you will be redirected to a new page where you need to choose the Xero Organization to which your application will be connected. After choosing organization click Allow Access.
After establishing a connection we need to get a tenant-id, which is used for making callouts. To do this, open anonymous apex and run the following code:
HTTPRequest request = new HTTPRequest(); request.setMethod( 'GET' ); request.setEndpoint( 'callout:Xero_Named_Credential/'+'connections' ); request.setHeader( 'Accept', 'application/json' ); request.setHeader( 'xero-tenant-id', '' ); HTTPResponse response = new HTTPResponse(); HTTP objHTTP = new HTTP(); response = objHTTP.send( request );
In the response body you’ll find a tenant-id, which can be stored in a custom setting. This is how tenant-id is used during request to Xero:
request.setHeader('xero-tenant-id', tenantIdValueFromCustomSetting);
In the next part I will tell you about interesting facts and issues which you might face during Salesforce/Xero integration.
One of the things that needs to be remembered is that Xero API like Salesforce has limits:
- Concurrent Limit: 5 calls in progress at one time
- Minute Limit: 60 calls per minute
- Daily Limit: 5000 calls per day
There is also a limit to the number of API calls your app can make per minute across all tenants.
- App Minute Limit: 10,000 calls per minute
Check out another amazing blog here by Vimera: How to Integrate Data from Third-party Systems with your Salesforce Org?
If a limit is reached, you can use the Retry-After header which will show how long to wait before making another call.
response.getHeader('X-Rate-Limit-Problem')); response.getHeader('Retry-After'));
During integration we can make GET, POST, PUT, DELETE requests, but not each object supports all of them. For example Invoice object doesn’t include DELETE request, instead to remove record we need to use POST. Depending on the status there will be 2 ways, how it can be implemented:
- If invoice status is DRAFT or SUBMITTED we set it to DELETED in the request body.
{ "InvoiceNumber": "INV-000", "Status": "DELETED" }
- If invoice status is AUTHORISED we set it to VOIDED.
{ "InvoiceNumber": "INV-000", "Status": "Voided" }
To read about interesting lifehacks that were found during integration with Xero, please visit our website.
Responses