51. Can we do callout from start method?
We can do from start, but it will hit the limit so best is to do from execute. The execute method processes records in batches, which means you can make callouts for each batch of records, allowing you to stay within Salesforce’s governor limits. Additionally, if a callout fails in the execute method, you can implement error handling and retry logic for that specific batch of records, which is more challenging to do in the start method.
52.Can we call queuable from batch?
We can call because it is a job and can be queued. It can be called from finish.
53.Can I call a batch from another batch?
We can call from finish.
54.Can we get records without querying a batch?
We will do using an iterator from Batch A we will call batch B
In Batch B constructor will pass success records
In this case we dont need to query but we will use an iterator
55. How to run Future Method?
If we want any method to run in future, put @future annotation. This way Salesforce understands that this method needs to run asynchronously.
@future public static void clothesInLaundry(){ } Void – As it runs in future, it does not return any value to you currently.
The specified parameters must be primitive data types, arrays of primitive data types, or collections of primitive data types; future methods can’t take objects as arguments.
56. What are Primitive Values and why to pass in future?
– Primitive data types, such as Integer, Boolean, or String, represent simple values with no internal complexity. – These values are typically immutable, meaning they can’t change after they are assigned. For example, once you set an Integer to 5, it will always be 5.
– Because of their immutability and simplicity, Salesforce can reliably pass primitive values to a future method without concerns about changes occurring between the time of invocation and execution.
Objects and Complexity:
– Objects in Salesforce can be complex structures with various fields and relationships.
– An object’s data can change at any time due to user interactions or other processes. For example, a record’s fields can be updated, related records can be created or deleted, and triggers can fire, all of which might modify the object’s state.
– When you pass an object to a future method, it’s possible that the object’s data may change before the future method runs. This can lead to unexpected behaviour or inconsistencies if the future method relies on specific data within the object.
In essence, allowing objects as arguments in future methods would introduce potential data integrity and consistency issues. By restricting future methods to primitive data types, Salesforce ensures that the data passed to future methods is stable and won’t change unexpectedly between the time of invocation and execution.
57. How to call future method?
ClassName.clothesInLaundry();
58. Scenarios when we use future method?
Scenario 1: Whenever we want our code to be run in the background.
Scenario 2: You must have faced DML exception. This exception occurs when we insert setup and non-setup objects in one transaction.
Setup: User, Group, Profile,Layout, Email Template
Non Setup: All other standard and custom objects (Account, Custom Objects)
In same context, we cannot insert User/Profile and Account in same trasanction. Future method helps in this situation.
Scenario 3: We cannot perform callout from trigger. Use future method to put callout in future method to make a callout.
59.What are Limitations of Future method?
1. You cannot call one future method from another method
2. Can have only primate as parameters
3. Order of Invocation is not respected
60.Can you write the syntax of a future method?
Methods with the future annotation must be static methods and can only return a void type.
global class FutureClass {@future public static void myFutureMethod(){// Perform some operations }}
61.What could be the workaround for sobject types?
To work with sObjects, pass the sObject ID instead (or collection of IDs) and use the ID to perform a query for the most up-to-date record.
global class FutureMethodRecordProcessing {@future public static void processRecords(List<ID> recordIds){// Get those records based on the IDs // Process records }}
62. How can I perform Callouts from Future methods?
We need to add a parameter callout=true in @future. global class FutureMethodExample {@future(callout=true) public static void doCallouts(String name) {// Perform a callout to an external service } }
63. Can I write a future call in Trigger?
Yes, you can.
64. How can avoid this Exception condition, without using try-catch?
We can update the trigger logic to leverage the System.isFuture() and System.isBatch() calls so that the future method invocation is not made if the current execution context is future or batch.
65.How Many Future methods can be defined in a Class?
Any number of. There are no restrictions as such.
66.If I want to call a future method from a future method, what could be the solution?
A workaround could be calling a web service that has a future invocation.
67. Once I call a future method, how can I trace its execution?
Salesforce uses a queue-based framework to handle asynchronous processes from such sources as future methods and batch Apex. So, we can check the apex jobs if it has run or not.
However, future methods don’t return an ID, so We can’t trace it directly. We can use another filter such as MethodName, or JobType, to find the required job.
68.How to test a future method?
To test methods defined with the future annotation, call the class containing the method in a startTest(), stopTest() code block. All asynchronous calls made after the startTest method are collected by the system. When stopTest is executed, all asynchronous processes are run synchronously.
69.Is it possible to call future method from apex scheduler or not?
Yes, it is possible to call future method from apex scheduler
70.Why future method is static and void?
Future methods will run in the future. You don’t want your synchronous code waiting an unknown period for an asynchronous bit of code to finish working. By only returning void, you can’t have code that waits for a result.
The future method is static so that variables with this method is associated to the class and not the instance and you can access them without instantiating the class.
71.From which places we can call future method?
• Trigger
• Apex Class
• Schedulable Class
72.Can we pass the wrapper to future method?
You can pass wrapper, but for that, you’ll need to serialize/deserialize that parameter. You can convert the Wrapper to a String which is a primitive.
Once converted into a String you can them pass that string as a parameter to the future method in consideration.
73. What is the advantage of Queueable Apex over future methods?
Queueable Apex allows you to submit jobs for asynchronous processing like future methods.
• Non-primitive types: Your Queueable class can contain member variables of non-primitive data types, such as sObjects or custom Apex types.
• Monitoring: When you submit your job by invoking the System.enqueueJob method, the method returns the ID of the AsyncApexJob record. You can use this ID to identify your job and monitor its progress, either through the Salesforce user interface in the Apex Jobs page, or programmatically by querying your record from AsyncApexJob.
// You can use jobId to monitor the progress of your job
AsyncApexJob jobInfo = [SELECT Id, Status, NumberOfErrors FROM AsyncApexJob
WHERE Id = :jobId];
• Chaining jobs: You can chain one job to another job by starting a second job from a running job. Chaining jobs is useful if you need to do some sequential processing.
74.What is the interface used for Queueable Apex?
Queueable interface
75. What are methods used in Queueable Apex Class?
Execute method.
76. How many jobs can you chain from executing a job?
You can add only one job from an executing job, which means that only one child’s job can exist for each parent job.
77.How can I use this Job Id to trace the Job?
Just perform a SOQL query on AsyncApexJob by filtering on the job ID.
AsyncApexJob jobInfo = [SELECT Status, NumberOfErrors FROM AsyncApexJob WHERE Id=:jobID];
78.Can I do callouts from a Queueable Job?
Yes, you have to implement the Database.AllowsCallouts interface to do callouts from Queueable Jobs.
79.What are the Limitations of Queueable Jobs?
50 jobs can be added to the queue with System.enqueueJob() method in a single transaction
Maximum depth of chain job is 5 i.e., 4 child jobs and initial parent jobs for Developer and Trail organizations but there is no limit in other editions
80. Can you write a blueprint of Queueable Job?
Create a class, implement the Queueable interface, and override the execute method. public class QueueableApexExample implements Queueable { public void execute(QueueableContext context) { //some process } }
81. What is QueueableContext?
It is an interface that is implemented internally by Apex, and contains the job ID. Once you queue for the Queueable Job, the Job Id will be returned to you, by apex through QueueableContext’s getJobId() method.
82.How can I queue Queueable Job?
Using System.enqueueJob Method. ID jobID = System.enqueueJob(new QueueableApexExample());
83.I have 200 records to be processed using Queueable Apex, How Can I divide the execution Context for every 100 records?
Similar to future jobs, queueable jobs don’t process batches, so you can’t divide the execution Context. It will process all 200 records, in a single execution Context.
84. Can I chain a job that has implemented allowsCallouts from a Job that doesn’t have?
Yes, callouts are also allowed in chained queueable jobs. 85.How to test Chaining?
You can’t chain queueable jobs in an Apex test. So you have to write separate test cases for each chained queueable job. Also, while chaining the jobs, add a check of Test.isRunningTest() before calling the enqueueJob.
86. What is an apex Scheduler?
The Apex Scheduler lets you delay execution so that you can run Apex classes at a specified time. This is ideal for daily or weekly maintenance tasks using Batch Apex.
87. What is the interface used for Schedulable Apex?
Schedulable interface
88.What are the methods used with a Schedulable interface?
The only method this interface contains is the execute method.
89.What is the parameter of the execute method ?
The parameter of this method is a SchedulableContext object.
90.What happens after the class is scheduled?
After a class has been scheduled, a CronTrigger object is created that represents the scheduled job. It provides a getTriggerId method that returns the ID of a CronTrigger API object.
91.What are the arguments of the System.Schedule method?
The System.Schedule method takes three arguments:
• Name for the job
• CRON expression used to represent the time and date the job is scheduled to run
• Name of the class.
92.How to Test Scheduled Apex?
To test Scheduled Apex you must ensure that the scheduled job is finished before testing against the results. To do this, use startTest and stopTest around the System. schedule method, to ensure processing finishes before continuing your test.
93. What is the governor limit of Scheduled Apex?
You can only have 100 scheduled Apex jobs at one time and there are a maximum number of scheduled Apex executions per a 24-hour period.
93.Can we make a callout from Scheduled Apex?
Synchronous Web service callouts are not supported from scheduled Apex.
94.How to monitor Scheduled Jobs?
After an Apex job has been scheduled, you can obtain more information about it by running a SOQL query on CronTrigger.
CronTrigger job = [SELECT Id, CronJobDetail.Id, CronJobDetail.Name, CronJobDetail.JobType FROM CronTrigger ORDER BY CreatedDate DESC LIMIT 1];
95. Explain code to schedule batch Apex to run at regular intervals? global class SampleBatchScheduler implements Schedulable {// Execute at regular intervals global void execute(SchedulableContext ctx){String soql = ‘SELECT Id, Name FROM Account’; SampleBatch batch = new SampleBatch(soql); Database.executebatch(batch, 200);} }
96. What is the return type of system.schedule?
System. schedule method returns the job ID in string format. String jobID = system.schedule(‘Merge Job’, sch, m);
97.How to get count of Apex Scheduled Job programmatically?
You can programmatically query the CronTrigger and CronJobDetail objects to get the count of Apex scheduled jobs.
98. If there are one or more active scheduled jobs for an Apex class, can you update the class or any classes referenced in Salesforce UI?
If there are one or more active scheduled jobs for an Apex class, you cannot update the class or any classes referenced by this class through the Salesforce user interface. However, you can enable deployments to update the class with active scheduled jobs by using the Metadata API
99. Does Apex Scheduler run in system mode?
The scheduler runs as system—all classes are executed, whether or not the user has permission to execute the class..
100. Callout is not supported in Scheduled Apex so what is the alternative?
Synchronous Web service callouts are not supported by scheduled Apex. To be able to make callouts, make an asynchronous callout by placing the callout in a method annotated with @future(callout=true) and call this method from the scheduled Apex. However, if your scheduled Apex executes a batch job, callouts are supported from the batch class.