Open Khandagale-Saurabh opened 2 years ago
when we have conditon to perfom asynchrosl activity on very fridat, 2nd setarday , next week we gofor schedulable apex
any class implements schedualble show go for execute method
when we want to perform tasks separately at that time we use asynchronously . 3 future method log handle it separately
it not take sobjest as apartment because it reun seprately
whenever we are working on a large number of records there is a possibility of occurring error or government limit so we use batch apex
databse.querylocator it is use to fetch 50million record Start : use to fetch data
why you are using global: because you are using database.batchable interface which is global
https://xperiencepartage.wordpress.com/2018/12/11/what-is-batch-apex-or-batch-class/
Batch Apex Syntax Your class must implement the Database.Batchable interface and include the following three methods: Start,Execute and Finish
Start
This method collects the records or objects to pass the records to the execute method for processing. This method is called once at the beginning of a Batch Apex job This method uses either a Database.QueryLocator object or an Iterable that contains the records or objects passed to the job. With the QueryLocator object,total no. of records retrieved by SOQL query is upto 50 million records. With the Iterable,the normal limit applies means 50000 records you can fetch using Iterable. Execute
Performs the actual processing for each chunk or “batch” of data passed to the method. The default batch size is 200 records. Maximum batch size is 2000. Batches of records are not guaranteed to execute in the order they are received from the start method. It takes two arguments: A reference to the Database.BatchableContext object. A list of sObjects, such as List, or a list of parameterized types. Finish
Used to execute post-processing operations (for example, sending an email) and is called once after all batches are processed. We can’t create a batch class without finish method because we are using the Database.Batchable interface and all the methods are mandatory to use while using interface.This concept is same as we use interface in Java. Sample Batch Class:
Sample batch class.PNG
Calling Batch Class:
MyBatchClass myBatchObject = new MyBatchClass(); Id batchId = Database.executeBatch(myBatchObject);
Database.QueryLocator: 50 Million records can be returned.
Iterable:
Governor limits will be enforced. So, we can use up to 50,000 records only. Else, exception will be thrown. You can also use the Iterable to create your own custom process for iterating through the list. The biggest difference is probably that an Iterable can loop over aggregate queries. Best Practices and important points:
Only use Batch Apex if you have more than one batch of records. Batch Apex is typically stateless. If you specify Database.Stateful in the class definition, you can maintain state across all transactions. With the Apex flex queue, you can submit up to 100 batch jobs. Up to 5 batch jobs can be queued or active concurrently. Can’t use getContent/getContentAsPDF methods. We can call a batch class from trigger: trigger callbatchapex on Account (after insert) { List accList = new List(); for(account acc : trigger.new){ if(acc.annualrevenue < 20000) accList.add(acc); } if(accList.size() > 0) database.executebatch(new BatchApexDemo(),200); } Use extreme care if you are planning to invoke a batch job from a trigger. You must be able to guarantee that the trigger won’t add more batch jobs than the limit. A batch class can’t call a @future but can call a webservice so indirectly we can call future method using webservice. We can call a batch class inside another batch class.We need to call batch class in finish method of first batch class.If we need to call other batch class in execute method then we need to use Queueable class. Call the batch class inside Test.startTest() and Test.stopTest(); batch.PNG Make sure that the number of records inserted in test class is less than the batch size of 200 because test methods can execute only one batch total. Write more accurate SOQL query to gather the records. Minimize the number of asynchronous requests created to minimize the chance of delays.
Dekh Bhai: https://www.youtube.com/watch?v=d9n3bIQR6r8 when you want to make api call or want to perform a task where you want to use non setup [Profile,user] object then go for future. remeber you cannont call future to another fureutre method or batch method. If so use quable. return type always void. in paramertm of future method you cantake ono setup object only.
// This is valid for a future method
@future
public static void myFutureMethod(String param1, Integer param2) {
// Method logic here
}
// This is NOT valid for a future method because it accepts a non-primitive data type (CustomObject__c)
@future
public static void myFutureMethod(CustomObject__c obj) {
// Method logic here
}
Future method in salesforce is static because , it run on independent thread and does not require any instance of class to call it.
https://www.youtube.com/@sfdcstart/playlists
What is States of batch : holding,pending,
There are 2types of queue ; 1] Apex que where it store batch jobs upoto 5, and flex Q store upto 100, so it help to optimize,
Now in this case we have J7 which will get fail due to some reason then the it will go to flex queue, It will never go to apex queue as there will be all active jobs. Note we will never do directly from flex-> server it will go from flex-> apex-> server
Apex job is use to check asynchronous Asynchronous