Rest API.
Auth. Provider In OrgA
Named Credential In OrgA
RestResource Class:
@RestResource(urlMapping='/Cases/*')
global with sharing class CaseManager {
@HttpGet
global static list<Case> getCases() {
list<Case> result = [SELECT CaseNumber,Subject,Status,Origin,Priority FROM Case limit 10];
return result;
}
@HttpPost
global static ID createCase(String subject, String status,
String origin, String priority) {
Case thisCase = new Case(
Subject=subject,
Status=status,
Origin=origin,
Priority=priority);
insert thisCase;
return thisCase.Id;
}
}
SalesforceConnect Class to Authenticate to OrgB and Get and create cases using named credential :
public class salesforceConnect {
public static void getCasesFromSalesforceOrgB() {
Http http = new Http();
HttpRequest request = new HttpRequest();
// calling Named Credential Like below ----- callout:SFDCB
//Calling rest resource class like services/apexrest/cases
request.setEndpoint('callout:SFDCB/services/apexrest/cases');
request.setMethod('GET');
HttpResponse response = http.send(request);
if(response.getStatusCode() == 200) {
list<Object> Results = (list<Object>) JSON.deserializeUntyped(response.getBody());
// Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
System.debug('Received the following cases:');
for(Object result: Results) {
Map<String, Object> caseProps = (Map<String, Object>) result;
System.debug('CaseNumber'+caseProps.get('CaseNumber')+'- SubJect'+caseProps.get('Subject'));
}
}
}
public static void createCaseInOrgB() {
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('callout:SFDCB/services/apexrest/cases');
request.setMethod('POST');
request.setBody('{"CaseNumber":"00001026","Subject":"Bigfoot Sighting!","Status":"New","Origin":"Phone","Priority":"Low"}');
HttpResponse response = http.send(request);
if(response.getStatusCode() == 200) {
list<Object> Results = (list<Object>) JSON.deserializeUntyped(response.getBody());
// Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
System.debug('Received the following cases:');
for(Object result: Results) {
Map<String, Object> caseProps = (Map<String, Object>) result;
System.debug('CaseNumber'+caseProps.get('CaseNumber')+'- SubJect'+caseProps.get('Subject'));
}
}
}
}
No comments:
Post a Comment