Notes
We need to Salesforce Orgs
The integration will be done from Org A to Org B
The following setup will be done in Org B
- Connected App
- Endpoint (Apex Rest Resources)
Step 1 | Org B: Create Connected App
Step 2 | Org B: Create Apex Class for Endpoint Web Resources
@RestResource(urlMapping='/Account/*')
global with sharing class EndPointAPI {
@HttpDelete
global static void DeleteAccount() {
RestRequest req = RestContext.request;
String acId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
Account account = [SELECT Id FROM Account WHERE Id = :acId];
delete account;
}
@HttpGet
global static Account GetAccount() {
RestRequest req = RestContext.request;
String acId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
Account result = [SELECT Id, Name, Phone, Website FROM Account WHERE Id = :acId];
return result;
}
@HttpPost
global static String PostAccount(String Name) {
Account account = new Account();
account.Name = Name;
insert account;
return account.Id;
}
}
Step 3 | Org A: Create Auth. Provide
Provider Type: Salesforce
Customer Key: Customer Key Value from your Connected App
Customer Secret: Customer Secret Value from your Connected App
Authorize Endpoint URL: https://login.salesforce.com/services/oauth2/authorize
Token Endpoint URL: https://login.salesforce.com/services/oauth2/token
Default Scope: api or full
Step 4 | Org B: Add the Callback URL to the Connected App
After creating Auth provider, you will be provided 4 URLs. One of them is the Callback URL. Copy and insert it in the connected app you created earlier.
FROM (ORG A):
TO (ORG B):
Step 5 | Org A: Name Credentials
When you save name credential it will ask you to login to ORG B to get access.
Step 6 | Org A: Name Credentials
public with sharing class AccountWebService {
public static Http http = new Http();
public static HTTPResponse response;
public static HttpRequest request;
public class NewAccountRequestWrapper {
public String name {get; set;}
}
public static void getAccount(Id accId) {
request = new HttpRequest();
request.setMethod('GET');
request.setEndpoint('callout:SalesforceAccount/services/apexrest/Account/' + accId);
response = http.send(request);
System.debug(response.getBody());
}
public static void addAccount(NewAccountRequestWrapper newAccount) {
request = new HttpRequest();
request.setMethod('POST');
request.setEndpoint('callout:SalesforceAccount/services/apexrest/Account');
request.setHeader('Content-Type', 'application/json;charset=UTF-8');
request.setBody(JSON.serialize(newAccount));
response = http.send(request);
System.debug(response.getBody());
}
public static void deleteAccount(Id accId) {
request = new HttpRequest();
request.setMethod('DELETE');
request.setEndpoint('callout:SalesforceAccount/services/apexrest/Account/' + accId);
response = http.send(request);
System.debug(response.getBody());
}
}
//Add a new Account
AccountWebService.NewAccountRequestWrapper newAccount = new AccountWebService.NewAccountRequestWrapper();
newAccount.name = 'TestAccount';
newAccount.phone = '123456789';
AccountWebService.addAccount(newAccount);
//get Account details based on Id
AccountWebService.getAccount('123123123');
//delete Account based on Id
AccountWebService.deleteAccount('12121212');