Showing posts with label athenahealth. Show all posts
Showing posts with label athenahealth. Show all posts

Wednesday, 20 December 2017

REST Integration

REST : Representational State Transfer

REST is a simple way of sending and receiving data between client and server.
It supports transfer of data by JSON/XML/Plain text

Idempotent :

If you send retry a request multiple times, that should be equivalent to single request modification.
Ex: Whenever you are trying to POST a comment in site,By mistake double clicked the submit it creates two comments in the server.Since POST is not Idempotent it behaves that way.

Safe Methods : 

Do not modify the resource in the server.

Ex: GET

What are the HTTP Methods for RESTful Services ?


HTTP Method
Details
Idempotent
Safe Method
GET
GET request should be used only to fetch some information from the server.
Yes
Yes
POST
POST request can be used to create a new resource or to update an existing resource in the server.
No
No
PUT
PUT If the Request-URI refers to an already existing resource – an update operation will happen, otherwise create operation should happen.

Note: If and only if updating the full content of the specified resource. PUT should be used.

Yes
No
PATCH
PATCH for Partial Update of content
No
No
DELETE
DELETE method should be used to remove or delete something from the server
Yes
No


POST VS PUT

Ideally POST should use for creation of new resource & PUT for updating the existing resource.

Again it's design decision :
1.The end point must be idempotent
2. If request URI contains the existing resource update operation if not create the mentioned resource

If any one is not needed use POST else use PUT

REST Integration Requirement / Salesforce Coding Test Scenario :

Get the GeoCode [Lattitue,Longitude] & formatted_address from GoogleMap API

Implementation :

Apex Class
Description
BLG_RESTAPICall_Ex
GoogleMapAPI call & Parsing the response JSON
BLG_GoogleMapAPIResponse
GoogleMapAPI JSON Response class



Is there any limit on REST request payload ?

HttpPost doesn't have size limit, but usually limited by the server to request is sent to.






Wednesday, 6 December 2017

Salesforce Insert and Database.Insert

What is the difference between Insert and Database.Insert ?
Ans:
Actually both are same, DML statements.
Only difference we can see while processing the Bulk Data.
Ex: 5 records are processing for contact insert and LastName field is required for insertion,
3 have all required details and 2 record doesn't have.
Ex: All record added to list "lstContact"

Insert lstContact: 

It will not process any records,It rollbacks above 3 records also because of error thrown in 4th record

Database.Insert(lstContact,false) : 

It process all the 3 records and excludes 2 records.Partial DML operation is performed.
In Database we can see the 3 inserted records.

Database.Insert(lstContact,true) : 

It  is same as Insert,So it will not insert any record due to failure in other record.

opt_allOrNone :

Is the boolean variable used above.


Sample Execution Code :