Following are the most commonly used Salesforce api:
1)Rest api
2)Soap api
3)Bulk api
Let say I have two systems as SYSTEM A and another system as SYSTEM B.
What we will doing today is we will try to fetch account from SYSTEM B and show that using Visualforce page in SYSTEM A.
Let's start,
ACTIVITIES WE NEED TO DO IN SYSTEM B.
1)Create a connected app.
2)Rest api webservice to fetch data based on the requirement of SYSTEM A and send it back to SYSTEM A.
1) WHAT IS CONNECTED APP IN SALESFORCE?
For an external application that needs to authenticate with Salesforce we need to create a connected app so as to inform Salesforce about the new authentication entry point. Connected app uses standard OAuth 2.0 protocol to authenticate.
Note down the Consumer key, Consumer secret(We need to provide it SYSTEM A for authentication purpose along with username, password, security token of the user which SYSTEM A will be using for authentication).
2) REST API WEBSERVICE:
ACTIVITIES WE NEED TO DO IN SYSTEM A.
1)Create an apex controller.
2)Create a visualforce page.
3)Create a remote site setting for the URL of System B.
1) CREATE A APEX CONTROLLER:
2)CREATE A VISUALFORCE PAGE:
3)Remote site URL:
RESULT IMAGE after previewing Visualforce page:
In this way, we have completed Salesforce rest api by taking two Salesforce systems.
Please comment below if you have any doubts.
YOU CAN VISIT OTHER POST FROM OUR BLOG AS WELL,
Salesforce rest api integration to fetch list of records (Click here)
Salesforce rest api integration to create a record (Click here)
Salesforce rest api integration to update a record (Click here)
Salesforce rest api integration to delete a record (Click here)
Salesforce soap integration (Click here)
Salesforce rest api callouts (Click here)
TAGS: Salesforce rest api integration between Salesforce and Salesforce,Salesforce api,Salesforce rest api,Salesforce rest api integration to fetch a record.
1)Rest api
2)Soap api
3)Bulk api
4)Streaming api
Among the above Salesforce api, today we will trying to cover the topic Salesforce rest api integration by taking example of two Salesforce systems.
First let's see when to use rest api as we have lot of other Salesforce api.
Rest api is a simple and power webservice based on restful principles and it uses rest resource and HTTP methods to expose functionalities. It supports both XML and JSON . Rest api has lightweight requests and responsive framework and it is good for mobile and web apps.
Let say I have two systems as SYSTEM A and another system as SYSTEM B.
What we will doing today is we will try to fetch account from SYSTEM B and show that using Visualforce page in SYSTEM A.
Let's start,
ACTIVITIES WE NEED TO DO IN SYSTEM B.
1)Create a connected app.
2)Rest api webservice to fetch data based on the requirement of SYSTEM A and send it back to SYSTEM A.
1) WHAT IS CONNECTED APP IN SALESFORCE?
For an external application that needs to authenticate with Salesforce we need to create a connected app so as to inform Salesforce about the new authentication entry point. Connected app uses standard OAuth 2.0 protocol to authenticate.
Note down the Consumer key, Consumer secret(We need to provide it SYSTEM A for authentication purpose along with username, password, security token of the user which SYSTEM A will be using for authentication).
2) REST API WEBSERVICE:
@RestResource(urlMapping='/getAccountOnExternalIdtofetchsinglerecord/*')
global with sharing class getAccounttoSingleRecord {
@Httpget
global static Account fetchAccount(){
Account obj=new Account();
RestRequest req = RestContext.request;
RestResponse res = Restcontext.response;
string accId =
req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
obj=[Select id , name from Account where id=:accId];
return obj;
}
}
1)Create an apex controller.
2)Create a visualforce page.
3)Create a remote site setting for the URL of System B.
1) CREATE A APEX CONTROLLER:
public class
restApiTofetchSingleRecord {
private string cKey =
'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
private string cSecret =
'XXXXXXXXXXXXXXXXXXXXX';
private string uName =
'XXXXXXXXXXXXXXXXXXX';
private string passwd =
'Password+SecurityToken';
public static list < resultWrapper >
listWrap {
get;
set;
}
public restApiTofetchSingleRecord() {
listWrap = new list < resultWrapper
> ();
}
public class responseWrapper {
public string id;
public string access_token;
public string instance_url;
}
public string getRequestToken() {
string reqBody =
'grant_type=password&client_id=' + cKey + '&client_secret=' + cSecret +
'&username=' + uName + '&password=' + passwd;
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setBody(reqBody);
req.setMethod('POST');
req.setEndpoint('https://login.salesforce.com/services/oauth2/token');
HttpResponse hresp = h.send(req);
responseWrapper wResp =
(responseWrapper) JSON.deserialize(hresp.getBody(), responseWrapper.class);
system.debug('Instance url' +
wResp.instance_url);
system.debug('session id' + wResp.access_token);
return wResp.access_token;
}
public void getConList() {
list < account > accList1 = new
list < account > ();
String accToken;
string responseBody;
string externalAccId =
'0017F00001Xg9LH'; // Id of account from SYSTEM B
string endPoint =
'https://ap5.salesforce.com/services/apexrest/getAccountOnExternalIdtofetchsinglerecord/'
+ externalAccId;
restApiTofetchSingleRecord obj = new
restApiTofetchSingleRecord();
accToken = obj.getRequestToken();
system.debug('access token' +
accToken);
if (accToken != '') {
Http h1 = new Http();
HttpRequest req1 = new
HttpRequest();
req1.setHeader('Authorization',
'Bearer ' + accToken);
req1.setHeader('Content-Type',
'application/json');
req1.setMethod('GET');
req1.setEndpoint(endPoint);
HttpResponse hresp1 =
h1.send(req1);
resultWrapper wResp1 =
(resultWrapper) JSON.deserialize(hresp1.getBody(), resultWrapper.class);
listWrap.add(wResp1);
}
}
public class resultWrapper {
public string id {
get;
set;
}
public string name {
get;
set;
}
}
}
2)CREATE A VISUALFORCE PAGE:
<apex:page
controller="restApiTofetchSingleRecord">
<apex:form >
<apex:pageBlock >
<apex:pageblockButtons >
<apex:commandButton
value="TEST" action="{!getConList}"/>
</apex:pageblockButtons>
<apex:pageblocktable
value="{!listWrap}" var="a" >
<apex:column
value="{!a.name}"/>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
3)Remote site URL:
RESULT IMAGE after previewing Visualforce page:
In this way, we have completed Salesforce rest api by taking two Salesforce systems.
Please comment below if you have any doubts.
YOU CAN VISIT OTHER POST FROM OUR BLOG AS WELL,
Salesforce rest api integration to fetch list of records (Click here)
Salesforce rest api integration to create a record (Click here)
Salesforce rest api integration to update a record (Click here)
Salesforce rest api integration to delete a record (Click here)
Salesforce soap integration (Click here)
Salesforce rest api callouts (Click here)
TAGS: Salesforce rest api integration between Salesforce and Salesforce,Salesforce api,Salesforce rest api,Salesforce rest api integration to fetch a record.
How to we do the same if we need to display any Images?
ReplyDeleteSorry for the late reply. I have not checked yet, but I will update here if i try the same.
DeleteIf its using Named Credentials, what changes we need to make on Apex controller?
ReplyDeleteSorry but could you please explain in detail what exactly you want.
DeleteHi, after login is success how can we open SYSTEM B's home page? Thanks
ReplyDeleteHow can we hide username, password, client secret etc from the apex class?
ReplyDeleteYou can use custom labels or custom settings or custom metadata to store username, password, client secret etc and than refer them in apex class.
DeleteJust use Named Credentials
DeleteHello, the response of the oauth call is "access_token":"SESSION_ID_REMOVED"?
ReplyDeleteHow fix this?
In debug log you will not be able to see it. however in class you can access it.
DeleteHii Farukh i m getting the error like No content to map to Object due to end of input
ReplyDeleteError is in expression '{!getConList}' in component in page restapitofetchsinglerecordvfpage: Class.System.JSON.deserialize: line 15, column 1
Class.restApiTofetchSingleRecord.getConList: line 49, column 1
can u help me