Skip to main content

Salesforce to Salesforce Integration using OAuth 2.0 and Named Credentials


Hello Everyone,

After investing lot of time, I was able to Integrate with my own sandbox( you can say one Salesforce Instance to Other Salesforce instance)

I had a scenario where i had to call a rest method created and Exposed in my own sandbox. So I started digging on this as you guys are now.

In order to perform OAuth with salesforce you need to perform 3 important steps.
1.       Create Connected App
2.       Create Auth. Provider
3.       Create Named Credentials
Once you are authorized, you just need to write 5- 6 lines of code to invoke your REST method.
So let’s gets started.
As you know Salesforce uses OAuth 2.0 now. So our job is to set up OAuth.
Importantly, OAuth requires 4 important details and they are,
Client ID, Secrete Key, User Name and Password.
So how do we get them?
Client ID and Secrete Key (AKA Client Secrete) can be obtained from Creating a Connected app. But what about User Name and Password??
User Name and Password can be obtained using Named credentials. Let's see how.

Creating Connected App:
Setup èApps èScroll Down to Connected App section and Click New.
Fill the details. Refer Screenshots.
Pic 1


Pic 2

Run As User: Chose your Org’s system Admin (preferably) / the person whom you want to run the Exposed REST method.When you try to invoke REST method salesforce will run the class with this user.
Scopes: Full Access (full)
Any field you don’t see in the screen shot, you can leave them blank.
We will come back to CallBack URL in a while. Just update Dummy value as “https://login.salesforce.com " for now and Click Save.
You will now see some page like this.
Pic 3


Now you got your Client Key and Secret. Well done. Let’s hit the road and find a way to use Username and Password.
As I said Named Credentials are the way to do it.
In order to create Named Credentials, we need something called Authentication Provider.
This Auth Provider will act as junction between Named Credentials and Connected App.
Creating the Auth Provider:
Setup è Auth.Providers è Click New
Chose Provider Type as Salesforce (Since we are integrating with Salesforce to Salesforce).
Name and URL Suffix: Give any suitable name as per your convenience.
Consumer Key: Created in Previous step (Creating a Connect App) Refer the screenshot.
Consumer Secret: Same as above.
Execute Registration As: Give the Same user which you gave in above step (Run As User in Connected App Section)

Once you click save you will see something like this.
Pic 4

Now you have got Call Back URL. (Do you remember we gave dummy value somewhere before this step?) Now it’s time to copy the call back URL and go back to connected app and edit and Update the Call Back URL .
Pic 5


As I mentioned Auth provider acts as junction between named Credentials and Connected App. Lets see how.
In any REST web call we need to pass User Name, Password, and Client ID and secrete in the request header for authentication.
Client Secret and Key are generated in Connected App and You Updated the Connected App with Call Back URL generated in Auth Provider. So now you see how Connected App and Auth Provider are linked. 
In Short Call Back URL generated in Auth provider is used in Connected App.
Now time to see how Auth Provider is linked with Named Credentials.

Creating Named Credentials: 
Setup è Named Credentials è Click New
Setup as below.
Pic 6

Look Closely. You need to choose the Auth Provider which you created in previous step .With this Named Credentials and Auth provider are tied together . This is how Auth provider acts as Junction between Named Cred and Connected App.
Note: 
Start Authentication Flow on Save should be checked and put the scope as full.
If you see the pic, the Authentication Status is “Pending” Now
URL : This is the URL of your Salesforce Instance. ( It may be na2 or na** based upon what is assigned to your org. In my case I had created my own domain so looks little different in the pic.)

When you click save it will ask you for the username and password of your salesforce org. once you provide the Valid Username and Password, somehow salesforce will save this in the backend.
After you provide the valid username and Password you will get something like this.

Pic 7


Allow It and You are Done. Authentication Status will be changed to Authenticated as “ Authenticated as yourUserName” .
Pic 8


Now you just need to refer in you controller.
HttpRequest req = new HttpRequest();
            req.setEndpoint('callout:eBloger_NamedCred/services/apexrest/YourMethodNameWhichIsExposed?param1='+userToBeUpdated.ID+'&param2='+accountIDtobeUpdated);
            req.setMethod('GET');
            Http http = new Http();
            HTTPResponse res = http.send(req);
            System.debug(res.getBody());


NOTE: Parma1 and Param2 and something my httpGet method is expecting as a parameter. Otherwise it can be vomited or modified according to your scenario.

Now it is time for me and you to go explore what Refresh Token full means and things which you are not clear from this article.

Even I am new-Bee to this integration stuff and trying to help who is in my position J

Comments

  1. I am always searching online for articles that can help me. There is obviously a lot to know about this. I think you made some good points in Features also. Keep working, great job!
    Salesforce.com training in chennai
    Salesforce crm Training in Chennai

    ReplyDelete
  2. Cool! Do you know how to get Authentication Status from the APEX ?

    ReplyDelete
  3. In this example, do you connect salesforce with the same enviroment, but... how I can connect Salesforce to Salesforce using this protocol?

    ReplyDelete

Post a Comment

Popular posts from this blog

Salesforce - Invoke Apex from Visual Flows

This post will brief you about how one can invoke an Apex class from Visual Flows. Important Note: The method which you would like to invoke should be marked as  @InvocableMethod  and the variables which you would like use in and out of Flow, we need to mark them as  @InvocableVariable Best Practices : Make 2 wrapper classes. One for getting the values from Flow to Apex and other wrapper class is to get the values from Apex to Flow. The below sample code explains how you can get a detail in and out of the flow. Use Case : We ask user to enter the Account ID which they want to update and New Name which they want to update on the entered Account ID. Note: Since this is for the demo purpose, we are asking user to enter the Account ID (SFDC 15/18 digit ID ). In Ideal scenario we can invoke the Flow from custom button/links. (  Refer This ) Here we created a Small Visual Flow which has 2 screens ( 1. For asking details from user and other for Showing the success/fa

Lightning - Passing value from Parent Component to Child Component

Hey Guys, This post I will try to cover some of the basics of Lightning Event. So if you have reached this post, I am assuming you have got some basic knowledge of Lightning framework. So, In brief Lightning Framework (LF ) works based on component and event driven modal. So when I say component  based, you might have seen that most of the codes are broken in to component in lightning development. You will understand more when you start creating one. There are few scenarios I would like to explain. Scene 1: Parent Component has one child and i want to pass some value from Parent to it's Child. Scene 2:Vice Versa Now .  I want to pass some value from Child Comp to it's parent components. How will you do ? There are 2 ways For Scene 1 and they are Way 1. Passing the value as Attribute Way 2. Sending the values using Lightning Methods. Way 1 is pretty simple. You will create a Attribute inside component 2(Child Comp) and will pass the required va