0
private class StateInfo {
        Account acc {get; set;}

        StateInfo(Account acc){
            this.acc = acc;
        }

        Contact cont {get; set;}
        StateInfo(Account acc, Contact cont) {
            this.acc = acc;
            this.cont = cont;
        }
    }

I am updating this class in my method which is AuraEnabled like this:

Opportunity opp = [Select Id, AccountId From Opportunity Where Id =: oppId];
            Child_Service.StateInfo stateInfo = new Child_Service.StateInfo([Select Id, Billing_Account_No__c From Account Where Id =: opp.AccountId]);

            Child_Service.StateInfo stateInfo2 = new Child_Service.StateInfo(stateInfo.acc, [Select Id, Email, FirstName, LastName From Contact Where accountId =: stateInfo.acc.ID]);

            Continuation con = new Continuation(60);
            con.state = stateInfo2;
            con.continuationMethod = 'sendEmail';

I am able to access the values of account object in this method, but when I try to access the contact email value in my sendEmail method which is also AuraEnabled I am not able to get the contact email value,

@AuraEnabled(continuation=true cacheable=true)
    public static String sendEmail(Object state){
        Child_Service.StateInfo stateInfo = (Child_Service.StateInfo)state;
        try {
            System.debug('account id: ' + stateInfo.acc.Id);
            String email = String.valueOf(stateInfo.cont.email);
            System.debug('Email::: ' + email);

        } catch(Exception e){
            System.debug('Exception occured at: ' + e.getLineNumber() + ' is: ' + e.getMessage());
        }
        return stateInfo.cont.email;
    }

I get 'undefined' response in lightning, is it because I am not setting any parameter for the sendEmail method from lightning controller, or how do I do that.

Also is this the right approach for not querying the object more than once, or should I try to make an object type attribute in my lightning component and then update that and send again to my apex to access the field value as and when required.

0

Not sure why you are implementing continuation as you are not even using Labels and importantly you are returning something from database and not from external system.

However, you will need labels in response method sendEmail and also addHttpRequest in request method to complete continuation flow.

Below is the sample continuation code which works fine:

@AuraEnabled(continuation=true cacheable=true)
public static Object check(){
    StateInfo stateInfo = new StateInfo([Select Id, Name From Account Where Id ='00128000009j45qAAA']);

    StateInfo stateInfo2 = new StateInfo(stateInfo.acc, [Select Id, Email, FirstName, LastName 
                                                         From Contact 
                                                         Where accountId =: stateInfo.acc.ID
                                                         LIMIT 1]);

    Continuation con = new Continuation(60);
    con.state = stateInfo2;
    con.continuationMethod = 'sendEmail';
    Http http = new Http();
    HttpRequest request = new HttpRequest();
    request.setEndpoint('callout:herokuAnimals');
    request.setMethod('GET');
    con.addHttpRequest(request);
    return con;
}

@AuraEnabled(continuation=true cacheable=true)
public static Object sendEmail(List<String> labels, Object state){
    StateInfo stateInfo = (StateInfo)state;
    try {
        HttpResponse response = Continuation.getResponse(labels[0]);
        String result = response.getBody();
        System.debug('result: ' + result);
        System.debug('account id: ' + stateInfo.acc.Id);
        String email = String.valueOf(stateInfo.cont.email);
        System.debug('Email::: ' + email);

    } catch(Exception e){
        System.debug('Exception occured at: ' + e.getLineNumber() + ' is: ' + e.getMessage());
    }
    return stateInfo.cont.email;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.