{"id":4486,"date":"2015-05-22T04:23:17","date_gmt":"2015-05-22T04:23:17","guid":{"rendered":"http:\/\/www.jitendrazaa.com\/blog\/?p=4486"},"modified":"2022-06-11T11:01:26","modified_gmt":"2022-06-11T15:01:26","slug":"continuation-object-in-apex-asynchronous-callouts-for-long-running-request-live-demo","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/continuation-object-in-apex-asynchronous-callouts-for-long-running-request-live-demo\/","title":{"rendered":"Continuation object in Apex &#8211; Asynchronous callouts for long running request &#8211; Live Demo"},"content":{"rendered":"\n<p>Check the below video first if you are planning to use Continuation<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"Continuation not needed anymore in Salesforce - Change My Mind\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/ya2N9EX9dmg?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n<p style=\"text-align: justify;\">We may run into\u00a0scenario in Salesforce project, where we need call external web service but no need to\u00a0wait for response. Response from external web service can be processed asynchronously and once processed it can be presented in Visualforce page.<\/p>\n<p style=\"text-align: justify;\">We can accomplish\u00a0this by using <a href=\"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/salesforce-interview-question-part-14\/\">Actionsupport <\/a>or <a href=\"https:\/\/www.jitendrazaa.com\/blog\/tag\/javascript-remoting\/\">Javascript remoting<\/a>. However, this is possible with using <a href=\"https:\/\/www.salesforce.com\/us\/developer\/docs\/apexcode\/Content\/apex_continuation_overview.htm\">Continuation <\/a>method\u00a0as well. \u00a0In Continuation approach, we can call external web service and inform that which callback method should be used to \u00a0process response. After execution of callback method, data is presented in visualforce page.<\/p>\n<figure id=\"attachment_4490\" aria-describedby=\"caption-attachment-4490\" style=\"width: 550px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/05\/apex_continuations_diagram.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"wp-image-4490\" src=\"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/05\/apex_continuations_diagram.png?resize=550%2C196&#038;ssl=1\" alt=\"Execution Flow of an Asynchronous Callout - Image from Salesforce documentation\" width=\"550\" height=\"196\" srcset=\"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/05\/apex_continuations_diagram.png?w=768&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/05\/apex_continuations_diagram.png?resize=300%2C107&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/05\/apex_continuations_diagram.png?resize=624%2C222&amp;ssl=1 624w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><\/a><figcaption id=\"caption-attachment-4490\" class=\"wp-caption-text\">Execution Flow of an Asynchronous Callout &#8211; Image from Salesforce documentation<\/figcaption><\/figure>\n<p style=\"text-align: justify;\">Below code demonstrate complete usage of this.<\/p>\n<p><!--more--><\/p>\n<p><strong>Apex code<\/strong><\/p>\n<pre><pre class=\"brush: java; title: ; notranslate\" title=\"\">\n\/**\n* \t@Author\t:\tJitendra Zaa\n * \t@Web\t:\thttps:\/\/jitendrazaa.com\n *\n * *\/\npublic class ISOtoCodeService {\n    public String countryISO {get;set;}\n    public String response {get;set;} \n\n\tprivate String baseSericeURL = 'http:\/\/services.groupkt.com\/country\/get\/iso2code\/';\n    private String returnedContinuationId ;\n\n    public ISOtoCodeService()\n    {\n        countryISO = 'IN';\n    } \n\n    public Object requestService(){\n\n        \/\/Timeout in seconds, 60 is limit\n        Continuation con = new Continuation(60);\n\n        \/\/ Set callback method\n        con.continuationMethod='renderResponse';\n\n        \/\/ Create callout request\n        HttpRequest req = new HttpRequest();\n        req.setMethod('GET');\n        req.setEndpoint(baseSericeURL+countryISO);\n\n        returnedContinuationId = con.addHttpRequest(req);\n\n        return con;\n    }\n\n    public Object renderResponse() {\n      \/\/ Get the response by using the unique label\n      HttpResponse httpRes = Continuation.getResponse(returnedContinuationId);\n      \/\/ Set the result variable that is displayed on the Visualforce page\n      response = httpRes.getBody();\n      \/\/ Return null to re-render the original Visualforce page\n      return null;\n    }\n\n}\n<\/pre><\/pre>\n<p><strong>Visualforce code<\/strong><\/p>\n<pre><pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n&amp;lt;apex:page tabStyle=\"Opportunity\" controller=\"ISOtoCodeService\" docType=\"html-5.0\" sidebar=\"false\" showHeader=\"false\" showChat=\"false\"&amp;gt;\n    &amp;lt;apex:form &amp;gt;\n        &amp;lt;apex:pageBlock title=\"Continuation Demo\"&amp;gt;\n            Country ISO :\n            &amp;lt;apex:input label=\"Country ISO\" value=\"{!countryISO}\"\/&amp;gt;\n            &amp;lt;apex:commandButton action=\"{!requestService}\" value=\"Request Service\" reRender=\"responseBlock\"\/&amp;gt;\n\n        &amp;lt;\/apex:pageBlock&amp;gt;\n\n        &amp;lt;apex:pageBlock title=\"Response from Webservice\" id=\"responseBlock\"&amp;gt;\n\n&amp;lt;pre&amp;gt; {!response} &amp;lt;\/pre&amp;gt;\n\n        &amp;lt;\/apex:pageBlock&amp;gt;\n    &amp;lt;\/apex:form&amp;gt;\n&amp;lt;style type=\"text\/css\"&amp;gt;\n        pre{\n         \tfont-size : 1.7em;\n        }\n    &amp;lt;\/style&amp;gt;\n\n&amp;lt;\/apex:page&amp;gt;\n<\/pre><\/pre>\n<p>Make sure to add URL &#8220;<em><span style=\"text-decoration: underline;\">http:\/\/services.groupkt.com<\/span><\/em>&#8221; in remote site settings.<\/p>\n<p><strong>Live Demo :<\/strong><\/p>\n<p><iframe loading=\"lazy\" style=\"width: 100%; height: 400px;\" src=\"https:\/\/jitendrazaa2-developer-edition.ap1.force.com\/\" width=\"300\" height=\"150\"><\/iframe><\/p>","protected":false},"excerpt":{"rendered":"<p>Check the below video first if you are planning to use Continuation We may run into\u00a0scenario in Salesforce project, where we need call external web service but no need to\u00a0wait for response. Response from external web service can be processed asynchronously and once processed it can be presented in Visualforce page. We can accomplish\u00a0this by [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"jz_research_post":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[9],"tags":[337,297,314,331],"class_list":["post-4486","post","type-post","status-publish","format-standard","hentry","category-salesforce","tag-apex","tag-asynchronous-apex","tag-continuation","tag-salesforce"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":6101,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/limitless-chaining-of-continuation-object-in-salesforce\/","url_meta":{"origin":4486,"position":0},"title":"Limitless Chaining of Continuation object in Salesforce","author":"Jitendra","date":"May 31, 2017","format":false,"excerpt":"How to create a Continuation Server in Salesforce with the help of JavaScript remoting and Continuation Object","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Chaining of Continuation Object in Salesforce","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/05\/Continuation-Object-GIF.gif?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/05\/Continuation-Object-GIF.gif?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/05\/Continuation-Object-GIF.gif?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/05\/Continuation-Object-GIF.gif?resize=700%2C400&ssl=1 2x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/05\/Continuation-Object-GIF.gif?resize=1050%2C600&ssl=1 3x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/05\/Continuation-Object-GIF.gif?resize=1400%2C800&ssl=1 4x"},"classes":[]},{"id":6113,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/design-continuation-server-in-salesforce\/","url_meta":{"origin":4486,"position":1},"title":"Design Continuation Server in Salesforce","author":"Jitendra","date":"May 31, 2017","format":false,"excerpt":"How to implement Continuation Server in Salesforce with the help of Action Function and Continuation Object","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Continuation Server in Salesforce - Demo","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/05\/Continuation-Demo.gif?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/05\/Continuation-Demo.gif?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/05\/Continuation-Demo.gif?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/05\/Continuation-Demo.gif?resize=700%2C400&ssl=1 2x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/05\/Continuation-Demo.gif?resize=1050%2C600&ssl=1 3x"},"classes":[]},{"id":6244,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/a-tale-of-governor-limits-and-solutions-in-salesforce\/","url_meta":{"origin":4486,"position":2},"title":"A Tale of Governor Limits and Solutions in Salesforce","author":"Jitendra","date":"November 21, 2017","format":false,"excerpt":"Thrilling story of Salesforce Technical Architect on a quest to solve application problems and avoid governor limit errors","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"A Tale of Governor Limits and Solutions in Salesforce","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/09\/A-Tale-of-Governor-Limits-and-Solutions-in-Salesforce.jpg?fit=900%2C417&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/09\/A-Tale-of-Governor-Limits-and-Solutions-in-Salesforce.jpg?fit=900%2C417&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/09\/A-Tale-of-Governor-Limits-and-Solutions-in-Salesforce.jpg?fit=900%2C417&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/09\/A-Tale-of-Governor-Limits-and-Solutions-in-Salesforce.jpg?fit=900%2C417&ssl=1&resize=700%2C400 2x"},"classes":[]},{"id":6939,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/podcast-how-to-achieving-hyper-threading-in-salesforce\/","url_meta":{"origin":4486,"position":3},"title":"Podcast &#8211; How to Achieving Hyper-Threading in Salesforce","author":"Jitendra","date":"January 2, 2020","format":false,"excerpt":"Total number of Asynchronous Apex which includes Batch Apex, Future Method, Queueable & Scheduled job is 250k or 200 x Total User license whichever is greater. Now lets do some fact check : Execute method of Batch Apex can only call 1 Queueable Scheduler can call 50 QueueableWe can have\u2026","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Podcast - Hyper Thread iN Salesforce","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2020\/01\/Podcast-Hyper-Thread-iN-Salesforce.png?fit=1200%2C675&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2020\/01\/Podcast-Hyper-Thread-iN-Salesforce.png?fit=1200%2C675&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2020\/01\/Podcast-Hyper-Thread-iN-Salesforce.png?fit=1200%2C675&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2020\/01\/Podcast-Hyper-Thread-iN-Salesforce.png?fit=1200%2C675&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2020\/01\/Podcast-Hyper-Thread-iN-Salesforce.png?fit=1200%2C675&ssl=1&resize=1050%2C600 3x"},"classes":[]},{"id":5562,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/summer-16-top-features\/","url_meta":{"origin":4486,"position":4},"title":"Salesforce Summer 16 &#8211; My favorite top 20 features","author":"Jitendra","date":"June 28, 2016","format":false,"excerpt":"List of Salesforce Summer 16 features","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"User Switcher in Salesforce Summer 16","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/06\/User-Switcher-in-Salesforce-Summer-16.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":4942,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/salesforce-winter-16-top-features\/","url_meta":{"origin":4486,"position":5},"title":"Salesforce Winter 16 &#8211; Top features","author":"Jitendra","date":"October 28, 2015","format":false,"excerpt":"You can watch recording of my live stream channel as well to know more on Winter 16 features.\u00a0Powerpoint presentation used in livestream can be accessed from here. 1. Object Manager Objects are objects, even if they are standard or custom objects. Salesforce simplified navigation to access Objects, we can access\u2026","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Setup Audit Trail - Salesforce Winter 16","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/10\/Setup-Audit-Trail.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/10\/Setup-Audit-Trail.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/10\/Setup-Audit-Trail.jpg?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/10\/Setup-Audit-Trail.jpg?resize=700%2C400&ssl=1 2x"},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/4486","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/comments?post=4486"}],"version-history":[{"count":7,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/4486\/revisions"}],"predecessor-version":[{"id":7607,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/4486\/revisions\/7607"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=4486"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=4486"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=4486"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}