Showing posts with label SFDC. Show all posts
Showing posts with label SFDC. Show all posts

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, 12 December 2017

Batch Apex - Iterable

Syntax : global (Database.QueryLocator | Iterable) start(Database.BatchableContext bc) {}
When do we use Iterable ?
When some thing is not achieved by direct SOQL .
1. Aggregate queries in Batch Apex
2. Querying on multiple object
3. To process CSV / XML files
Refer SF Knowledge Article:
Sample Code :


Batch Apex - Call Out

What is the Call Out limit from Batch Apex ?

As per latest the upper limit is 100

Any workaround to process more than 100 call outs ?

Database.executeBatch(new BatchClass(),100) 
Pass the scope parameter as 100,Which can split the batch execution into multiple batches

How to define the scope w.r.t the callouts?
In execute method you have 2 call outs ,Your batch size should be 100/2 = 50
execute method has N callouts your batch size should be 100/N

Which interface need to use to implement call out from Batch Apex?

Database.AllowCallouts

What happens if we don't implement this interface and try to do call out ?
First error: Too many callouts: 1



Which type of call outs are allowed ?

HTTP request as well as methods defined with  web service key word .

Call Outs are allowed from Start /Finish methods?
Yes,Its possible 

REST API Call from Batch Apex :



Modify the standard object field label

Please find the below steps to modify the standard object field label.
Ex: Modify Contact Phone to Landline
Click on the Next button to rename the field label
Rename the field label as per requirement



Friday, 8 December 2017

Batch Apex to Future Methods

Batch Apex:

Batch Apex used to process large volume (50 Million) of data asynchronously.
The maximum number of batch apex method executions per a 24 hour period is 2,50,000
Call outs upper limit is 100

Batch Apex Constructor VS Start Method ?
Batch class must implement Database.Batchable interface,Must have implementation for the start,execute,finish methods.
If requirement is to pass the parameters to Start method that can be done via Constructor.
Always constructor executes 1st.

Ex: Process only records created by specific user,Then that user id can be passed to Constructor from there will be assigned to local variable and that can be used in Start method.

Future Method :

Future methods also executes asynchronously,If any long running operation needs to be performed and don't want to wait for it to get complete then that logic can be written in future methods.

Is it possible to call future method from Batch Apex?

No,Both are asynchronous calls so its not allowed to call from Batch Apex.

Any other alternate way to implement this?

Yes,Only from finish method can be called.

If we try to call from execute method ,We will not get any compilation error but in run time we will get below error
"First error: Future method cannot be called from a future or batch method: DT_Utility.sendEmail()"

Ex :

Create a future method to send email notification :




Try calling from Batch Apex Execute method :



Run the batch job :


Database.executeBatch(new BatchApexUnderstanding());


Apex Job Status :






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 :