Skip to main content

Apex Future methods



In the last three posts, we examined calling a webservice from a visualforce page's controller. We explored calling them as synchronous apex callouts and asynchronous apex callout (Continuation pattern) -

 Now lets explore one more way to call Apex asynchronously. Future methods are used in Apex to execute their logic in a separate thread than the main thread. This is very useful in moving a long running Apex callout into a asynchronous mode execution.

So in our example, if we want to take a value from the user and convert it to Celcius by calling the webservice but dont need the user to wait for the result, we can use a future apex method. The user will be free to proceed with other work as the callout happens in a different thread completely. That thread will receive the results of the callout and process it.

Note that it has no way to communicate back to the main thread. So your user never sees the results unless you use some other means of doing this. For ex: - the result could be written to a Object field value in your future method. Next time the user visits the page layout of that object, he will the field as populated with the result. This makes it different from the continuation pattern which involves a visualforce page to show the results.


Future methods can be used effectively to avoid governor limits related to concurrency from long running callouts. However they have limits too. Following post calls out these limits very well - 4 gotchas with Apex Future Calls

So use Future calls carefully. In my example we are using a visualforce page. We will change it to use a future call. However the result cannot be sent back to the visualforce page, so i will show you the result via System debug logs. Remember based on your business requirement, you may choose to write the result to a field or capture the result in a different way to inform your users.

In my visualforce page in the post - Integrating Salesforce with Soap web service Part 1  I already had a button Convert Future that was calling a controller method - callFutureMethod.  We are going to use this button now.

In the controller, lets code to take the input Fahrehiet value and call a future method -

In the above code -
    • callFutureMethod is just calling the future call and passing the value of the input parameter to it. It calls the the future method and returns control to the user in the visualforce page.
    • getCelciusFuture is annotated with @Future to indicate it executes in a future thread. It is also marked as a callout as it will be invoking a webservice callout. Now when this method is called, it will execute in the future in a separate thread. Here all we do is get the result and log it in the system log. We could have probably written it to a object field if required. However we have no way to pass it back to the visual force page
Now let me click the Convert Future button on the UI and see what happens -
The UI doesnt change as I explained. But if you look at the logs, there will be two -
  • The actual apex call triggered by the button click where you see the actual callout does not occur.

  • A FutureHandler log where you will see the actual callout and result being fetched.

So thats a future call. Now you can use it as your business use case determines it.

That covers all the posts I set out to write on synchronous and asynchronous apex callouts for now. I definitely enjoyed it and hope some of you get some value of it as well!

Comments

Popular posts from this blog

Workaround to bypass Salesforce SSO

One of the best practices for implementing Single Sign On for your Salesforce org is always ensure there is a way your System administrator can login via the standard login page side-stepping the SSO configuration.  Reason for this is if ever something goes wrong with your Idp provider's service and the SSO authentication responses are not coming as expected, all your users are unable to login. That is, if you have setup your My domain to prevent logins via standard Salesforce login urls (login.salesforce.com). This includes the System administrator as well. Only if your system administrator can somehow login, then he or she can disable the SSO settings for your domain and allow login via the normal login page as a temporary measure. What do you do in such a situation? Well Salesforce has built a workaround for this which is not well documented anywhere (probably for a good reason :) ). I found out about it from a colleague at work. If your my domain url is - https://Com

Salesforce Big Objects - Key learnings

I remember reading about Salesforce Big Objects before they became available and thought maybe it is an option to backup data present in regular objects. That is, instead of taking a backup via an ETL tool or data loader, maybe this will be an option to backup the data in the Force.com platform itself. Now that it is GA and I am reading more about it, i think the use cases for this are more varied. Some use cases I can think of are –  Archival of key data objects within the platform: You may want to use this option if you dont use any other means to backup key data. Also this may be an attractive option for non-large enterprise customers who dont themselves invest on large data warehouses or data lakes in their enterprise architecture. Ex: customer history (if present in tasks and activities) will become huge over years but this is useful information for reporting & customer analysis. Store key information which is large volume in quantity and also high volume in transa

DBAmp for Salesforce - salesforce integration for SQL Server DBAs

Recently i got the opportunity to explore a tool called DBAmp for integration with Salesforce. I found it to be a very useful tool which will help with any data manipulation requirements in Salesforce. Following are my learnings from the exercise. Hope it helps any of you who may need to work with this tool -  DBAmp is a SQL Server package that can be used to integrate with Salesforce. The site where this software is available is - http://www.forceamp.com/ Overview: It essentially installs on top of an existing SQL Server database and provides an OLE DB connector that can be used to connect to Salesforce. Behind the scenes, it executes API calls against Salesforce for any SQL server command it receives. Thus we can create a connection in SQL server to Salesforce org and pull data into tables in the database just as if we are querying against Salesforce tables directly. Use cases for DBAmap + Salesforce: Many use cases exist for using this tool against Salesforce. Pr