Activity Forums Salesforce® Discussions What is the difference between queueable methods and future methods in apex class in salesforce?

  • What is the difference between queueable methods and future methods in apex class in salesforce?

    Posted by Deepak on August 23, 2019 at 12:46 pm

    Is there any difference between queueable methods and future methods or both are same?

    Saddam replied 5 years, 2 months ago 3 Members · 2 Replies
  • 2 Replies
  • Piyush

    Member
    August 26, 2019 at 4:05 am

    Hi Deepak,

    Future Method :-

    1. For avoiding Mixed_DML_Operations exception, we will be going for future method where it will be running in asynchronous mode when the system resource becomes available.
    2. If you want to perform complex DML operations and want to make external webservice @future(callout=true) means you can go for future method.
    3. Future method should be static and return type is void, parameters must be primitive types, sObjects are not accepted as future method parameters.

    Queueable Apex :-

    1. It comes in handy , when you need to have both the operations of Batch and future method and it should implement Queueable Interface.
    2. If one job is dependent on other job means here we can chain the dependent job in execute method by system.enqueuejob(new secondJob());
    3. You can also able to chain upto 50 jobs and in developer edition you can able to chain upto 5 jobs only.
    4. It will accept non-primitive types like sObjects and it also runs in asynchronous mode.
    5. In this Queueable apex you can get Job Id to monitor the job progress.
  • Saddam

    Member
    August 26, 2019 at 7:56 am

    Hi Deepak,

    future method

    A future method runs in the background, asynchronously. You can call a future method for executing long-running operations, such as callouts to external Web services or any operation you'd like to run in its own thread, on its own time.

    Remember that any method using the future annotation requires special consideration because the method does not necessarily execute in the same order it is called.
    The system executes the asynchronous call when it has available resources – this means that you can’t assume anything about when the call will be executed.
    The asynchronous method executes in a different transaction context than the trigger. A failure of the callout will have no effect on the transaction controlling the trigger (i.e. changes will not be rolled back). You will have to build in suitable compensation logic yourself to account for any exceptions in the callout.
    Future methods won’t necessarily execute in the same order they are called. In addition, it’s possible that two future methods could run concurrently, which could result in record locking if the two methods were updating the same record.
    No more than 50 method calls per Apex invocation
    The maximum number of future method invocations per a 24-hour period is 250,000 or the number of user licenses in your organization multiplied by 200, whichever is greater
    Keep the governor limits in mind when making callouts. We are constrained by the interface provided by the remote web service. If the remote web service were to provide any kind of batch processing interface, choose that instead of making individual calls. It helps avoid hitting the governor limits and will be more performant as well since there are fewer round trips to make.
    Methods with the future annotation cannot be used in Visualforce controllers in either getMethodName or setMethodName methods, nor in the constructor.
    @future(callout = true) means that the method has ability to invoke external web services.
    Methods with the future annotation must be static methods
    can only return a void type
    The specified parameters must be primitive data types, arrays of primitive data types, or collections of primitive data types
    Methods with the future annotation cannot take sObjects or objects as arguments.
    You can invoke future methods the same way you invoke any other method. However, a future method can’t invoke another future method
    Methods declared as future aren't allowed in classes that implement theDatabase.Batchable interface.
    Methods declared as future can't be called from a batch Apex class.
    The getContent and getContentAsPDFPageReference methods cannot be used in methods with the future annotation
    Asynchronous calls, such as @future or executeBatch, called in a startTest, stopTest block, do not count against your limits for the number of queued jobs
    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.

    Queueable Apex

    Non-primitive types: Queueable class can contain member variables of non-primitive data types, such as sObjects or custom Apex types. Those objects can be accessed when the job executes.
    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.
    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.

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos