Welcome to my blog. In this blog post, we will learn how to integrate Salesforce with Xero.
Whenever we talk about integration, the very important thing is how securely connect to the third party system and then make the communication secure.
You can use below link to know all about Xero Authentication. Note: – Xero Uses OAuth 2.0 and OAuth 1.0 for authentication and we will use OAuth 2.0
Step1 – Create Connected Application in Xero
Navigate to This Link and then click on New App
Go to the Xero developer portal and create an OAuth2 app.
If you haven’t already signed up for a xero account you can do so here.
Use the following values:
- App Name – your choice, but can’t contain the word ‘Xero’
- Company or application URL – this needs to be an https address, but isn’t used
- OAuth 2.0 redirect URI – also needs to be https but won’t be used in salesforce. We will change this URL in later steps
- Click Create App
- Click Generate a secret
- Keep the page open
Step2 – Create Auth Provider in Salesforce
Once you have created the Connected Application in Xero, now the time is to Create the Auth. Provider in Salesforce
- Login to Salesforce Org
- Navigate to Setup -> Identity -> Auth. Providers -> New
- For Provider Type Select Open ID Connect
- For Consumer Key, provide the Client Id you have noted down
- For Consumer Secret, provide the Client Secret you have noted down
- For Authorize Endpoint URL provide use “https://login.xero.com/identity/connect/authorize“
- For Token Endpoint URL use “https://identity.xero.com/connect/token“
- For User Info Endpoint URL use “https://identity.xero.com/connect/userinfo“
- For Token Issuer use “https://identity.xero.com“
- For Default Scopes use “offline_access openid profile email accounting.transactions accounting.transactions.read accounting.contacts”
- Go ahead and Click on Save
- Now from the Detail Page Copy “Callback URL”
Step3 – Change the Callback URL in Xero Application
- Get back to the Xero Connected Application Detail Page
- Click on Configuration Tab from Left
- Click on “Add Another URI Button”
- Paste the URL that you have copied and then save it
Step4 – Create Named Credentials in Salesforce
Once you have created the Auth. Provider and Updated the callback URL in Xero Connected Application. Now, let’s created the Named Credentials as this will be used to Authenticate Xero with Salesforce
Make sure your named credentials looks like below image.
For Scope use below values
|offline_access accounting.settings openid profile email accounting.transactions accounting.transactions.read accounting.contacts|
Step5 – Test the Integration
To test the integration and make sure that this is working fine. Run the below code from the developer console
HttpRequest request = new HttpRequest(); request.setMethod('GET'); request.setEndpoint( 'callout:Xero/'+'connections' ); request.setHeader('Accept', 'application/json'); request.setHeader('xero-tenant-id', ''); HttpResponse response = new HttpResponse(); response = new Http().send(request); System.debug(' \n '+ response.getStatusCode() ); System.debug(' \n '+ response.getBody() );
After executing the code you must see a 200 as status code. See the image below
Below is the complete code for your reference
Note: – XeroUtilApi class is the main Class which is using XeroAPICalloutService class to make the callouts to Xero Org.
Here are some examples that are already implemented
- XeroUtilApi.getXeroInvoces(); – To fetch All the Invoices
- XeroUtilApi.getXeroAccounts(); – To fetch All the Accounts
- XeroUtilApi.getXeroContacts(); – To fetch All the Contacts
Thanks for reading 🙂 Happy Learning