Following are the most commonly used salesforce api:
@RestResource(urlMapping='/getContactFromEmailAndPhone/*')
global with sharing class getContactRecord {
@Httpget
global static Contact fetchContact(){
Contact obj=new Contact();
RestRequest req = RestContext.request;
RestResponse res = Restcontext.response;
Map<String, String> requestParam = req.params;
String conEmail=requestParam.get('email');
String conPhone=requestParam.get('phone');
obj=[Select id,lastname,email,phone from contact where email=:conEmail and phone=:conPhone];
/***Modify the HTTP status code that will be returned to external system***/
res.statuscode=201;
/***Modify the HTTP status code that will be returned to external system***/
return obj;
}
}
1) Create an apex controller.
2) Create a visualforce page.
3) Create a remote site setting for the URL of System B.
public class restApiTofetchSingleRecord {
private string cKey;
private string cSecret;
private string uName;
private string passwd;
public string instanceURL;
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 responseWrapper getRequestToken() {
List < Store_Cred__mdt > connectionParam = [SELECT Id, MasterLabel, client_id__c, client_secret__c, username__c, password__c from Store_Cred__mdt];
if(connectionParam.size() >0){
cKey=connectionParam[0].client_id__c;
cSecret=connectionParam[0].client_secret__c;
uName=connectionParam[0].username__c;
passwd=connectionParam[0].password__c ;
}
System.debug('Store_Cred__mdt' + connectionParam);
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('reqBody '+reqBody );
system.debug('wResp'+wResp );
system.debug('Instance url' + wResp.instance_url);
system.debug('session id' + wResp.access_token);
return wResp;
}
public void getConList() {
string integration1 = 'Testing integration';
list < account > accList1 = new list < account > ();
String accToken;
String instanceUrl;
string responseBody;
string email = 'testemail123@gmail.com';
string phone = '123';
restApiTofetchSingleRecord obj = new restApiTofetchSingleRecord();
responseWrapper obj1= obj.getRequestToken();
accToken = obj1.access_token;
instanceUrl = obj1.instance_url;
string endPoint = instanceURL+'/services/apexrest/getContactFromEmailAndPhone/?' + 'email=' + email + '&phone=' + phone;
system.debug('endPoint'+endPoint );
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.setHeader('Accept','application/json');
req1.setMethod('GET');
req1.setEndpoint(endPoint);
HttpResponse hresp1 = h1.send(req1);
system.debug('hresp1'+hresp1 );
system.debug('hresp1.getStatusCode()'+hresp1.getStatusCode());
system.debug('hresp1.getBody()'+hresp1.getBody());
if (hresp1.getStatusCode() == 201) {
resultWrapper wResp1 = (resultWrapper) JSON.deserialize(hresp1.getBody(), resultWrapper.class);
listWrap.add(wResp1);
}
}
}
public class resultWrapper {
public string id {
get;
set;
}
public string LastName{
get;
set;
}
public string Email{
get;
set;
}
public string Phone{
get;
set;
}
}
}
<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.LastName}"/>
<apex:column value="{!a.Email}"/>
<apex:column value="{!a.Phone}"/>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
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.
DeleteHi, How to I include composite data in request body?
ReplyDeleteHello, Iam not able to see connected app under create apps? can anyone help me please
ReplyDeleteCleared 90% of my doubts... Thanks a lot for the resource
ReplyDelete