Invoke Future Methods through Apex Trigger for web service callout

By | October 30, 2014

While developing applications in force.com, sometimes we need to consume external web service in apex trigger. But, callouts cannot be made from apex triggers as that restricts database transaction until the callout is completed and the time limit for this is up to 120 seconds.You can find other callout limits here.

The only way to execute a callout from a trigger is to run it asynchronously and this can be achieved by executing a method with the @future method. Future methods execute asynchronously i.e. one does not need to wait for a response. The thing to keep in mind is, a future method can have native calls, as well as, they can make a call out to an external web service and hence, in order to enable the future method to allow callouts, an extra parameter “Callout=true” needs to be passed.

For example: –

You have a requirement to call a web service after updating the contact record. Web service callout method (in this example callWebService(Contact cnt) ) is in the Util class which is getting consumed in apex after update trigger.

Apex trigger’s code:

Param passed to function cnt.Id
Apex class code:

changed to string contact ID
When the above trigger is fired it will throw an error as “Callout from triggers are currently not supported.

To resolve this you need to make a small change in your Apex code. Just add @future annotation before the apex method as shown below –

Future annotation changed to string contactIDAbout Us
Greytrix as a Salesforce Product development partner offers a wide variety of integration products and services to the end users as well as to the Partners across the globe. We offers Consultation, Configuration, Training and support services in out-of-the-box functionality as well as customizations to incorporate custom business rules and functionalities that requires apex code incorporation into the Salesforce platform.

Greytrix has some unique solutions for Cloud CRM such as Salesforce integration with Sage Enterprise Management (Sage X3), Sage Intacct, Sage 100 and Sage 300 (Sage Accpac). We also offer best-in-class Cloud CRM Salesforce customization and development services along with services such as Salesforce Data Migration, Integrated App development, Custom App development and Technical Support to business partners and end users.

Greytrix GUMU™ integration for Sage ERP – Salesforce is a 5-star app listed on Salesforce AppExchange.

For more information, please contact us at salesforce@greytrix.com. We will be glad to assist you.