function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

Few apex questions Part - 1

1. Why @future methods has to be static ?
2. Difference between Database.query() and simply querying [query].
3. In batch apex if one record fails for some reason will the entire batch fail ?
4. How to fetch failed update records of a Batch Apex ? 
Best Answer chosen by Amrut
Heather ThompsonHeather Thompson
1. A non-static method means that you can keep context. Any variables you set get held in memory. If future apex were allowed to be non-static it would mean salesforce was holding those values in memory for potentially up to 24 hours. For this reason, any asynchronous apex will need to be static.

2. From this answer (
"Primarily I've used Database.query (Dynamic SOQL) when:
I'm creating a managed package and don't want to take an explicit dependency on a feature. E.g. List<sobject> revenueSchedules = Database.query('Select Id from OpportunityLineItemSchedule where OpportunityLineItemId in :oliIds'); If I did this in static SOQL all the installers of the managed package would need Product Schedules enabled.
I want to dynamically include an optional field or filter in the SOQL query. A custom setting may define the name of a field that should be used to populate something else. As it isn't know at "compile" time it needs to be dynamic.
Otherwise I'd generally use static SOQL to cut down on ceremony code.
As the other answers and comments point out, dynamic SOQL limits your binding options. You can do simple bindings, such as checking if an Id is in a Set. However, you can't do more complex bindings that traverse properties. Instead you will need to convert these to strings. See Salesforce Ideas: Dynamic SOQL binding expression support to match static SOQL
Note that you need to be more careful with dynamic SOQL that you don't open yourself up to SOQL Injection."
3. It depends. By default, when you make updates to reccords there is an all or nothing setting enabled which will cause them all to fail. You can control this by using
database.insert(yourRecordVariable, false);
 (where false is for the allOrNothing parameter)

4. Inside of your batch class there is a biolderplate method that looks something like this:
global void execute(Database.BatchableContext bc, List<P> records){
       // process each batch of records
The second variable (In this example "records") is your list of records.