Thursday 4 January 2018

JavaScript Remoting

What is Javascript Remoting ?

This is a tool that developers can use to make an AJAX request from Visualforce page directly to an Apex Controller.

Key Features of Javascript Remoting?

Asynchronous 
Load only the initial page & Lazily load additional data.
Speed 
This is the fastest way of calling controller code.
No Forms
It will not post the form, So not keeping view state which improves the performance, On button click it will not load the whole page / no partial rendering. Only perform the action.
No SOQL Governor Limit
Javascript remoting calls don't count against SOQL Governor limit.
Responsive
It provides more responsive experience in both Mobile pages and web pages. 


Note : Salesforce has confirmed that the calls of the Remoting JavaScript methods do not count towards the limit of API calls


When to use Javascript Remoting?

Javascript Remoting can provide an efficient,responsive,and optimized user experience.

This is useful more in Mobile Web Development.


Limitations of Javascript Remoting ?

1. Since there is no forms and no view state maintained need to manage the state of page by self on the client side. Which may takes more development efforts.
2. Parameters need's to pass to the controller, It will take more maintenance in case of additional fields to the form.
3. All the validations need to be written.
4. Error handling must be incorporated into both page and the controller.
5. The response of the server is limited to a maximum of 15 Mb.
6. The response of the remote call must return maximum in 120 seconds.
7. As the response of the asynchronous server, conflicts or unexpected behaviors can occur, although very rarely.

Javascript Remoting VS ActionFunction ?


These are the 2 options to call the Apex from Javascript inside a Visualforce page.
Both will be used in different scenario's.

Javascript Remoting
Action Function
Performance much faster. No posting forms on request hence no view state.
Form will be post data on every request and View state will be transferred to the server.
There is no whole page/partial section re-rendering.
This can re-render the page or a specific section of the page to update it with new values from the controller instance.
Call back is available
No call back
Implementation is trickier.
Implementation is straight forward.

Note :
Any scenario where you don’t want to refresh the page/no partial page rendering and just want to perform an action.Then go for Javascript Remoting else Action Function can be used.


Visualforce Remote Objects VS  Javascript Remoting ?


Visualforce Remote Object
Javascript Remoting
The benefit of Remote Objects is that you can access server data without using API calls or writing Apex Code.
JavaScript Remoting needs Apex Code with the method @RemoteAction annotation
It's slower than the AJAX toolkit and the RemoteAction annotation.
This is the fastest way of calling controller code.
Currently creation of multiple records is not possible.
Creation of multiple records is possible.
The CRUD/FLS is automatically taken care in case of Remote Objects.
In apex you will need to write extra code to respect CRUD/FLS
Makes basic “CRUD” object access easy
It requires Javascript and Apex code
Supports minimal server-side application logic

Supports complex server-side application logic
Doesn’t provide automatic relationship traversals; you must look up related objects yourself

Handles complex object relationships better

  Test Class is not required, as no Controller is involved.

       Test Class is required, as Controller is involved.



Drawback of Visualforce Remote Object ?

The name space is hard coded in the mark-up. 


Javascript Remoting VS Lightning Component Framework?

Javascript Remoting Implementation ?

Action Function Implementation ?

Visualforce Remote Object Implementation ?

Tuesday 2 January 2018

transient in Salesforce

What is transient?

transient is a keyword in Salesforce.

What is the use of transient ?

Use the transient keyword to declare instance variables that can't be saved, and shouldn't be transmitted as part of the view state for a Visualforce page.

What is View State ?

http is stateless protocol,To maintain the state between post backs View State is used.


What is the size of View State ?

Since it transfers over http every request ,The size should be controlled.
View State can be 135 KB.
Only one View state will be created per page.

transient variables can be serialized?

No

transient vs static ?

Both transient & static variables are not Serializable ,From this confusion starts.
Ideally both are 2 different concepts.

Static variables are not bound to any particular object,Its class level.Serialization based on object hence static variable are not serializable.

Transient variables are used when any instance variable need to be excluded from Serialization.