0

I have a class from which I do a callout in apex. After the response of the callout I want to redirect the user to an external url how can I achieve this?.

I have only been able to redirect independent of the callout. In my example below I want to redirect to returnURl instead of 'https://www.goole.com'

public class leadgen {
    private final Lead weblead;  
    public Payment_Hub(ApexPages.StandardController stdController) {
        weblead = (Lead)stdController.getRecord();
    }
    public PageReference saveLead() {
        try {
            insert(weblead);
            PostPayment(weblead.id); 
        }
        catch(System.DMLException e) {
            ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error'));
            return null;
        }
        PageReference p = new PageReference('https://www.goole.com');
        p.setRedirect(true);
        return p;
    }

    @future(callout=true)
    public static void PostPayment(Id LeadId) {
        //Catch is not needed since soql's cant be empty in this case 
        List<obj> ..

        Http http = new Http();
        HttpRequest request = new HttpRequest();
        ..
        HttpResponse response = http.send(request);
        // Parse the JSON response
        if (response.getStatusCode() != 201) {
            System.debug('The status code returned was not expected: ' +
                         response.getStatusCode() + ' ' + response.getStatus());
        } else {
            System.debug('response.getBody()'+response.getBody());
            JSON payClass = JSON.parse(response.getBody());

            system.debug('returnURl: '+ payClass.links.checkout.href);
            string returnURl = payClass.links.checkout.href;
        } 
    }
}
  • Is it a VF page? And is the logic called on click of button? – Pranay Jaiswal Mar 20 at 9:32
  • @PranayJaiswal Yes form a VF used as site external page from there I call saveLead(). – Thomas Mar 20 at 9:33
2

Well, first of all you have to ditch the future. :) Considering its a VF Page.

Split the operation in 2 chunks that can happen in independently.

1) DML

2) Callout without future.

Now define an action function that does the callout.

Apex Command Button has an oncomplete attribute which invokes JS when the result of an AJAX update request completes on the client.

So on click of commandbutton you save the record, and then oncomplete calls the actionfunction which does the callout. As you have broken the transaction by coming back to JS . You can then do callout as its a different transaction.

To end user, it would just look like a normal button click.

EDIT : On googling i got an example of this on this blog.

  • Do You have an example of this. 1st I tried without the future but get an authorisation required error in my site page. I checked all the permissions on Guest profile. 2nd Still how do I do the redirect from the result of the callout? – Thomas Mar 20 at 9:48
  • You sure you got authorisation error and not uncommited work pending error? – Pranay Jaiswal Mar 20 at 9:49
  • Yes shure If I directry us the same call I get you error. I saw you example an think I got it thanks! – Thomas Mar 20 at 9:55

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.