{"id":745,"date":"2014-10-30T09:33:01","date_gmt":"2014-10-30T09:33:01","guid":{"rendered":"http:\/\/www.greytrix.com\/blogs\/salesforce\/?p=745"},"modified":"2025-05-30T05:04:15","modified_gmt":"2025-05-30T05:04:15","slug":"invoke-future-methods-through-apex-trigger-for-web-service-callout","status":"publish","type":"post","link":"https:\/\/www.greytrix.com\/blogs\/salesforce\/2014\/10\/30\/invoke-future-methods-through-apex-trigger-for-web-service-callout\/","title":{"rendered":"Invoke Future Methods through Apex Trigger for web service callout"},"content":{"rendered":"\n<p>While developing applications in <a href=\"http:\/\/www.salesforce.com\/platform\/products\/force\/?d=70130000000f27V&amp;internal=true\" target=\"_blank\" rel=\"noopener noreferrer\">force.com<\/a>, 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 <a href=\"https:\/\/developer.salesforce.com\/docs\/atlas.en-us.apexcode.meta\/apexcode\/apex_callouts_timeouts.htm\" target=\"_blank\" rel=\"noopener\">here<\/a>.<\/p>\n\n\n\n<p>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 <strong>@future<\/strong> 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 \u201c<strong>Callout=true<\/strong>\u201d needs to be passed.<\/p>\n\n\n\n<p><strong>For example: &#8211;<\/strong><\/p>\n\n\n\n<p>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.<\/p>\n\n\n\n<p><strong>Apex trigger\u2019s code:<\/strong><\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-content\/uploads\/2014\/10\/Param-passed-to-function-cnt.Id_.png\"><img decoding=\"async\" src=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-content\/uploads\/2014\/10\/Param-passed-to-function-cnt.Id_.png\" alt=\"Param passed to function cnt.Id\" class=\"wp-image-1832\"\/><\/a><\/figure><\/div>\n\n\n<p><br><strong>Apex class code:<\/strong><\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-content\/uploads\/2014\/10\/changed-to-string-contact-ID.png\"><img decoding=\"async\" src=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-content\/uploads\/2014\/10\/changed-to-string-contact-ID.png\" alt=\"changed to string contact ID\" class=\"wp-image-1833\"\/><\/a><\/figure><\/div>\n\n\n<p><br>When the above trigger is fired it will throw an error as \u201c<strong>Callout from triggers are currently not supported.<\/strong>\u201d<\/p>\n\n\n\n<p>To resolve this you need to make a small change in your Apex code. Just <strong>add<\/strong> @future annotation before the apex method as shown below &#8211;<a href=\"http:\/\/www.greytrix.com\/blogs\/salesforce\/wp-content\/uploads\/2014\/10\/Web-service-callout.jpg\"><br><\/a><\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-content\/uploads\/2014\/10\/Future-annotation-changed-to-string-contactID.png\"><img decoding=\"async\" src=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-content\/uploads\/2014\/10\/Future-annotation-changed-to-string-contactID.png\" alt=\"Future annotation changed to string contactID\" class=\"wp-image-1834\"\/><\/a><\/figure><\/div>\n\n\n<p><span style=\"font-size: Medium;\"><strong>About Us<\/strong><\/span><br><a href=\"https:\/\/www.greytrix.com\/\">Greytrix<\/a> 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.<\/p>\n\n\n\n<p><a href=\"https:\/\/www.greytrix.com\/\">Greytrix<\/a> has some unique solutions for Cloud CRM such as Salesforce integration with <a href=\"https:\/\/www.greytrix.com\/product\/sage-x3\/gumu-x3-salesforce-integration\">Sage Enterprise Management (Sage X3<\/a>), Sage Intacct, <a href=\"https:\/\/www.greytrix.com\/product\/sage-100-erp\/gumu-100-salesforce-integration\">Sage 100 <\/a>and <a href=\"https:\/\/www.greytrix.com\/product\/sage-300-erp\/gumu-300-salesforce-integration\">Sage 300 (Sage Accpac)<\/a>. We also offer best-in-class Cloud CRM <a href=\"https:\/\/www.greytrix.com\/product\/crm-development\/salesforce-com\">Salesforce customization and development services<\/a> along with services such as Salesforce <a href=\"https:\/\/www.greytrix.com\/product\/professional-services\/data-migration-services\">Data Migration<\/a>, <a href=\"https:\/\/www.greytrix.com\/product\/professional-services\/integrated-application-development\">Integrated App development<\/a>, <a href=\"https:\/\/www.greytrix.com\/product\/professional-services\/custom-development\">Custom App development<\/a> and <a href=\"https:\/\/www.greytrix.com\/product\/professional-services\/maintenance-support\">Technical Support<\/a> to business partners and end users.<\/p>\n\n\n\n<p>Greytrix GUMU&#x2122; integration for Sage ERP \u2013 Salesforce is a 5-star app listed on <a href=\"https:\/\/appexchange.salesforce.com\/listingDetail?listingId=a0N30000000psM5EAI\" target=\"_blank\" rel=\"noopener\">Salesforce AppExchange<\/a>.<\/p>\n\n\n\n<p>For more information, please contact us at <a href=\"mailto:salesforce@greytrix.com\">salesforce@greytrix.com<\/a>. We will be glad to assist you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/2014\/10\/30\/invoke-future-methods-through-apex-trigger-for-web-service-callout\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[20,21,139,192,416,502,539],"class_list":["post-745","post","type-post","status-publish","format-standard","hentry","category-gumu-salesforce","tag-apex-references-in-salesforce","tag-apex-trigger","tag-future-method","tag-invoke-future-methods","tag-salesforce-technical","tag-trigger-in-salesforce","tag-webservice-callout"],"_links":{"self":[{"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/posts\/745","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/comments?post=745"}],"version-history":[{"count":4,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/posts\/745\/revisions"}],"predecessor-version":[{"id":10847,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/posts\/745\/revisions\/10847"}],"wp:attachment":[{"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/media?parent=745"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/categories?post=745"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/tags?post=745"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}