•  

    Continuation in Aura and LWC Salesforce

     
    An asynchronous callout is a callout that is made for which the response is returned through a callback method. An asynchronous callout is also referred to as a continuation.
     Asynchronous callouts don’t count toward the Apex limit of 10 synchronous requests that last longer than five seconds. As a result, you can make more long-running callouts and you can integrate with complex back-end assets.
     
    Apex Code :
    Create a Method using Using @AuraEnabled annotation and setting (Continuation=true) property.
    public static Object MakeContinuationCall(Map<String,Object> Args){
    
       
    
        //Step1 – Create Continuation Object
    
              Continuation con = new Continuation (40);
    
    // Step 2 Define the Call back Method          
    
     con.continuationMethod='MycallbackFunction';
    
               
    
        //Step 3 -Create HTTP Request with Method and End Point URL
    
                HttpRequest req = new HttpRequest();
    
              req.setEndpoint(‘Set your Endpoint Here’)
    
                req.setMethod('GET');
    
               
    
                // Step 4-Add HTTP Request to Continuation
    
                Con.state = con.addHttpRequest(req);
    
    // The return should be Continuation Object. However You can wrap the Response in Call back fuction            
    
    return con;
    
        } 
       
    // Due to limitation in Salesforce Test Framework we have to this Method Overloading for code coverage.
    // The Test Frame work expect a Callback function without Argument, However we need to pass the return type of con.addHttpRequest(req) to Continuation.getResponse method in Callback function to receive the response.
    //The work around is to create a variable(here currentState) a private static variable
    //store the state in that variable and access it in the Argument less method.
     
       @AuraEnabled(continuation=true)
    
        public static Object MycallbackFunction (Object state) {
    
            currentState = (String)state;
    
            return MycallbackFunction ();
    
        }
    
       
    
        @AuraEnabled(continuation=true)
    
        public static object MycallbackFunction (){
    
    // your own wrapper
    
            MyResponseWarpper resWrapper= new MyResponseWarpper();
    
            // Recieve the Response
    
            if (!String.isBlank(currentState)){
    
                    HttpResponse response = Continuation.getResponse(currentState);
    
                    // deserialize to wrapper
    Hope you liked the article.Please use the comment section for any clarification.
     

Comments

  •  
    icon

    Cách vệ sinh điều hòa Mitsubishi says (Nov 20, 2019):

    Trong điều hoà các cánh quạt rất dễ bám bẩn. http://2goose.online/user/LaurieConybeare/

  •  
    icon

    Akshat Ajmera says (Jan 10, 2020):

    How can I write a test class for this as I am not able to call the function from the class, since the "invokeContinuationMethod" only allows the instance of the class to be called

Post Comments