-
Notifications
You must be signed in to change notification settings - Fork 522
CalloutRecipes
Demonstrates how to make an opinionated REST callout. This class utilizes the custom RestClient from the Shared Code group.
RestClient, CanTheUser
Constructor accepting a named credential.
| Param | Description |
|---|---|
namedCredential |
name of the Named Credetial to use |
Now that we have demonstrated how to callout to an endpoint, lets take a look at what else we can do with the response. When calling out to an external endpoint, the data may not always be in a format that can be directly deserialised into a Salesforce Object. If your callout returns untyped JSON, you can deserialize this into a Map<String, Object> by using a deserializeUntyped method to convert the string.
Type
Object>
Description
Map<String, Object>
System.debug(CalloutRecipes.httpCalloutWithUntypedResponse());Demonstrates a DELETE request to a second Salesforce org - A DELETE request is used to delete data from the target endpoint. In this example, we will be deleting a contact from another Salesforce org. We will store the parameters in the urlPath which can then be accessed through the .getParams() method in the org receiving the delete request.
| Param | Description |
|---|---|
contactId |
the Id of the contact that you would like to delete in |
Type
Integer
Description
String
Id contactId = [SELECT id FROM Contact LIMIT 1].id;
System.debug(CalloutRecipes.httpDeleteCalloutToSecondOrg(contactId));Demonstrates a GET request to a second Salesforce org. A Get request is used to retrieve data from a target endpoint, We will be using the performRestCallout method to make the callout. In this example, we will be requesting a list of Accounts from our second org. We will pass the endpoint our named credential, the url path to our IntegrationService custom REST endpoint, a null body and the GET method. We will then deserialize the JSON into a known object, in this case, a list of Accounts.
Type
List
Description
List
System.debug(CalloutRecipes.httpGetCalloutToSecondOrg());Demonstrates a PATCH request to a second Salesforce org a PATCH request is used to send data to a target endpoint and update already existing data. In this example, we will be sending a list of Account records to a second salesforce org for updating.
| Param | Description |
|---|---|
accountRecords |
a list of account records to be updated in the |
Type
Integer
Description
String The status of the callout
List<Contact> contacts = [SELECT id, firstName, lastName FROM Contact LIMIT 5];
System.debug(CalloutRecipes.httpPatchCalloutToSecondOrg(contacts));Demonstrates a POST request to a second Salesforce org a POST request is used to send data to a target endpoint and insert it. In this example, we will be sending a list of contact records to a second Salesforce org. We will serilaize the list and POST it in body of the callout.
| Param | Description |
|---|---|
contactRecords |
a list of contact records to be inserted in the |
Type
Integer
Description
String
List<Contact> contacts = [SELECT id, firstName, lastName FROM Contact LIMIT 5];
System.debug(CalloutRecipes.httpPostCalloutToSecondOrg(contacts));Demonstrates a PUT request to a second Salesforce org a PUT request is used to send data to a target endpoint and upsert it. In this example, we will be sending a list of contact records to a second org.
| Param | Description |
|---|---|
contactRecords |
a list of contact records to be upsert in the |
Type
Integer
Description
String
List<Contact> contacts = [SELECT id, firstName, lastName FROM Contact LIMIT 5];
System.debug(CalloutRecipes.httpPutCalloutToSecondOrg(contacts));As seen in the httpCalloutWithUntypedResponse method, we don't always get a perfect datastructure back from our callout. In this case, we have received and account and it's contacts that need to be inserted into Salesforce. Check out the Test class for an example of an untyped data structure.
| Param | Description |
|---|---|
untypedResponse |
the untyped JSON response that we received from |
CalloutRecipes.insertAccountAndContactsFromUntypedResponse(CalloutRecipes_Tests.goodUntypedJSON)Demonstrates how to make a raw HTTP request. This method demonstrates how to use the Http, HttpRequest and HttpResponse objects to construct a single get reuqest. The other methods in this class demonstrate the use of an intelligent abstraction layer - RestClient.cls - to make sending Http Requests easier, easier to test, and less error prone.
Type
String
Description
String
System.debug(CalloutRecipes.rawCallout());Internal custom exception class