Apex Integration Flashcards
What is API?
- Application Programming Interface
- Gets a request from the client, parses the request and tells the rest of the system what kind of operations to perform based on the result.
- (it is the messenger that takes request and tells the system what you want to do and returns the response back to you)
- They store resources, or individual record, using URI’s (Uniform Resource Identifiers)
What is Salesforce Connect?
allows us to establish a connection between our Salesforce org and an external database
Helpful because if we have a large amount of data in an outside system that we plan to continue using and don’t want to upload it to your org due to storage reasons or just for ease of use
How do we access URI’s?
We can access URIs at their corresponding Uniform Resource Locators (URLs), which are a part of the URI
What are the different HTTP Methods? Which are idempotent?
- GET
- PUT
- POST
- PATCH
- DELETE
Three of them are idempotent.
What is meant by idempotent? Which methods are idempotent?
- Being idempotent means that the end result of a set of duplicate requests with the same method to the same URL is independent of the number of duplicate requests
- We CAN get different status codes in responses from idempotent methods
Tldr: you can request a method X amount of times and it’s not going to make a difference to what the end result is going to be
GET PUT DELETE
What is GET?
- idempotent method
- Retrieves resource stored with the target URI
- Because we’re only reading data, not changing it, it makes no difference if we make a GET request once or 100 times
What is PUT?
- An idempotent method
- Either creates a resource at the target URL, or updates (read:overwrites) the entirety of the resource at said URL if it already exists (usually update)
- If you invoke a PUT API N times, the very first request will update the resource; the other N-1 requests will just overwrite the same resource state again and again – effectively not changing anything.
What is POST?
- NOT an idempotent method
- Lets us create a resource at the target URI or update an existing resource
- So POST is not idempotent because duplicate requests to the same URL will have compounding results
- The first POST operation will create one resource, the second will create a second resource, etc.
What is the difference between POST and PUT?
POST is used to “create new” resource.
PUT is used to “insert, replace if it already exists”. 5”.
What is PATCH?
- NOT an idempotent method
- Allows us to update parts of existing resource, but doesn’t overwrite the entire resource
- When making PATCH request, we only provide values for the fields that we want to change on the existing resource
What is DELETE?
- An idempotent method
- The first DELETE call will delete the resource with the specified URI
- Because the resource no longer exists after the first DELETE call, we can make as many duplicate DELETE requests as we want
- However, subsequent DELETE calls will give different status codes in their responses
What is HTTP Status Codes?
When we make a request using an HTTP method, the server will return a status code along with its response.
Status code represents the outcome of the operation.
Number Range: 100 - 599
Tell me about the 1xx range status codes
- Informational response:
- Indicates request was received, continue process
- 100: Continue
- No errors processing the request so far
Tell me about the 2xx range status codes
- Sucess: request was successfully received, understood, and acceped
- Common:
- 200 - Code Message: Ok
- Request was successful
-
201- Created
- resource was created
-
204 - No Content
- Request was successful but the body of response is empty (common response from DELETE call).
Tell me about the 3xx range status codes
- Redirects - further action required
-
300: Multiple Choice
- Multiple responses need to choose one
-
301: Moved Permanently
- Resource URL has changed and new URL resource is included in the response body
-
307: Temporary Redirect
- Resource URL has changed temporarily, make request to the URI included in body
Tell me about the 4xx range status codes
- Client Side Errors - request contains bad syntax or canot be fulfilled
-
400: Bad Request
- Request had syntax error
-
401: Unauthorized
- Need to provide credentials
-
403: Forbidden
- Provided credentials but not authorized
-
404: Page Not Found
- Request sent to non-existent URL
-
400: Bad Request
Tell me about the 5xx range status codes
- Server Side Errors - failed to fulfil valid request
-
500: Internal Server Error
- Server didn’t know how to handle when processing our request
-
502: Bad Gateway
- Server made call to another service to get the information we requested, but that second service gave invalid response
-
500: Internal Server Error
What is SOAP?
Simple Object Access Protocol, it is an XML-based communication architecture that allows different platforms and languages to communicate through HTTP requests.
SOAP services explain the ways to interact with them via WSDL - Web Service Description Language, a language based on XML
What is Allowlisting?
Security feature that only allows trusted files, application, processes to run. Before we can make any request to an external site (doesn’t matter the kind of service), we need to allowlist it in our org.
- Setup > Security > Remote SIte Settings and then click on New Remote Site
- Give the URL of the site, give it an alias, and click Save
- Once this is completed, we’re able to make an external callout to the service
Mock Classes
Mock Classes act as fake objects that stand in for real instances of objects.
- Apex test classes don’t let us make callouts to external services.
- We need to create a response that mocks the one we would get as a result of the actual request
- For SOAP callouts specifically, we achieve this by making a second test class that implements the WebServiceMock interface
- must be global or public and define the doInvoke() method
Test.setMock() method
- Lets the system know which mock we’re
using - Takes 2 parameters
- First is the type of interface our mock
implements(WebServiceMock.class when
we’re testing SOAP callouts). - Second is the instance of class that
implements this interface
Salesforce SOAP API
- Standard Out-Of-the-Box (OOB) SOAP API that we can use
- Allows us to find duplicate records, execute queries, get information about objects and fields, perform DML operations, and more
RESTful Services
- Stands for Representational State Transfer
- Set of guidelines for API architecture that has requirements like implementing a uniform interface across the service and the server being stateless
- Using REST services generally tends to be easier and than using SOAP webservices
Salesforce REST API
- Standard REST API that they provide
- Can use it to execute queries, get information about objects and fields, perform DML operations, and more
OOB Salesforce APIs
- Bulk API
- Chatter REST API
- User Interface API
- Analytics REST API
- Tooling API
- Metadata API
- Streaming API
OOB = out of box
SOAP vs REST API
SOAP - protocol, uses service interface
REST - architectural style, uses URI