How to Integrate Salesforce with Xero

on

|

views

and

comments

Hi #Trailblazers,

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

https://developer.xero.com/documentation/sdks-and-tools/tools/postman/#steps-to-get-up-and-running

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

Then:

  1. Click Create App
  2. Click Generate a secret
  3. Keep the page open
Image with a page of inputs where you enter your app details

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

  1. Login to Salesforce Org
  2. Navigate to Setup -> Identity -> Auth. Providers -> New
  3. For Provider Type Select Open ID Connect
  4. For Consumer Key, provide the Client Id you have noted down
  5. For Consumer Secret, provide the Client Secret you have noted down
  6. For Authorize Endpoint URL provide use “https://login.xero.com/identity/connect/authorize
  7. For Token Endpoint URL use “https://identity.xero.com/connect/token
  8. For User Info Endpoint URL use “https://identity.xero.com/connect/userinfo
  9. For Token Issuer use “https://identity.xero.com
  10. For Default Scopes use “offline_access openid profile email accounting.transactions accounting.transactions.read accounting.contacts”
  11. Go ahead and Click on Save
  12. Now from the Detail Page Copy “Callback URL”

Step3 – Change the Callback URL in Xero Application

  1. Get back to the Xero Connected Application Detail Page
  2. Click on Configuration Tab from Left
  3. Click on “Add Another URI Button”
  4. 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

  1. XeroUtilApi.getXeroInvoces(); – To fetch All the Invoices
  2. XeroUtilApi.getXeroAccounts(); – To fetch All the Accounts
  3. XeroUtilApi.getXeroContacts(); – To fetch All the Contacts

Thanks for reading 🙂 Happy Learning

Amit Singh
Amit Singhhttps://www.pantherschools.com/
Amit Singh aka @sfdcpanther/pantherschools, a Salesforce Technical Architect, Consultant with over 8+ years of experience in Salesforce technology. 21x Certified. Blogger, Speaker, and Instructor. DevSecOps Champion
Share this

Leave a review

Excellent

SUBSCRIBE-US

Book a 1:1 Call

Must-read

How to Utilize Salesforce CLI sf (v2)

The Salesforce CLI is not just a tool; it’s the cornerstone of development on the Salesforce Platform. It’s your go-to for building, testing, deploying, and more. As one of the most important development tools in our ecosystem

Save the day of a Developer with Apex Log Analyzer

Table of Contents What is Apex Log Analyzer? Apex Log Analyzer, a tool designed with Salesforce developers in mind, is here to simplify and accelerate your...

Salesforce PodCast

Introduction Hey Everyone, Welcome to my podcast, the first-ever podcast in India for Salesforce professionals. Achievement We are happy to announce that we have been selected as Top...

Recent articles

More like this

LEAVE A REPLY

Please enter your comment!
Please enter your name here

5/5

Stuck in coding limbo?

Our courses unlock your tech potential