0

I am trying to do a callout on a button click from my LWC comp using apex Continuations. In the debug log, I see no callout happened. But when I do the callout using setTimeout(), then its working fine. Can someone please help me where I am doing wrong? I have used the below code.

HTML: <lightning-button variant="brand" label="Fetch Contacts" onclick={findContacts} ></lightning-button>

import { LightningElement,track } from 'lwc';
import startContinuation from '@salesforce/apexContinuation/ContinuationCustom.startContinuation';

export default class CustomContactScreen extends LightningElement {

    findContacts(){
         
        startContinuation().then(result => {      
            console.log('result='+JSON.stringify(result))      
        })
        .catch(error => {
           
        });  
    }
}

apex class:

public with sharing class ContinuationCustom{
    // Test variable used for holding a reference to the Continuation Request
    @TestVisible
    private static String continuationLabel;
    // Test variable used for holding the value of the Continuation.state attribute
    @TestVisible
    private static String continuationState;
    
    // The function that fires the continuation
    @AuraEnabled(continuation=true cacheable=true)
    public static Object startContinuation() {
        // Create the callout Request
        HttpRequest req = new HttpRequest();
        req.setMethod('GET');
        req.setEndpoint('URL for callout');

        // Create the continuation with a 40s timeout
        Continuation con = new Continuation(40);
        con.ContinuationMethod = 'continuationCallback';
        
        // Store the reference to the HttRequest and make it accessible for a test-context
        continuationLabel = con.addHttpRequest(req);
        
        // Store data to be sent to the callback function
        continuationState = 'Some data here...';
        con.state = continuationState;
        System.debug('req=='+req+'===con=='+con);
        return con;
    }

    // The function that processes the continuation callback
    @AuraEnabled(cacheable=true)
    public static Object continuationCallback(List<String> labels, Object state) {
        HttpResponse response = Continuation.getResponse(labels[0]);
        Integer statusCode = response.getStatusCode();
        // TODO: handle the different status codes
        
        return response.getBody();
    }
}

1 Answer 1

1

There is no triggering point for findContacts in your code.

You should call that from connectedCallback, if you want the callout to happen right after component loads.

connectedCallback() {
   this.findContacts();

}

findContacts(){

    startContinuation().then(result => {
        console.log('result='+JSON.stringify(result))
    })
        .catch(error => {

        });
}

When you give a setTimeout, JavaScript automatically runs the code after the specified time, so there you have a triggering point.

Update:

Even with the above code it works fine :

enter image description here

6
  • there is a triggering point. i mentioned that on click of a button .<lightning-button variant="brand" label="Fetch Contacts" onclick={findContacts} ></lightning-button>
    – Anujit Das
    Nov 9, 2021 at 16:56
  • Then ot should work without any issue. Also change the name of the imported method. import startContinuationLWC from '@salesforce/apexContinuation/ContinuationCustom.startContinuation'; Nov 9, 2021 at 17:05
  • its not working. thats why I posted here ryt? the names are same everywhere. @nagendra
    – Anujit Das
    Nov 9, 2021 at 18:06
  • I tried with same code and it works fine. May be some cache issue, try hard refresh in the browser. Updated my answer with screenshot. It may be delayed due to network latency but the code looks fine and it should work. Nov 9, 2021 at 18:22
  • not working for me. Cleared the cache. still Number of callouts: 0 out of 100
    – Anujit Das
    Nov 9, 2021 at 18:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .