-1

I have this below method where i am inserting contact and calling a future method which will send the values to external system. I need on successful creation i should redirect to the created contact detail page.

How i can do that as future is having static method -

public Contact newCon;

public pagereference saveAndSend(){

        newCon.LastName = newCon.LastName;
        insert newCon;

        paramMap = new Map<String,String>();
        paramMap.put('fname', newCon.FirstName);
        paramMap.put('lname', 'AU');
        paramMap.put('origin', 'SALESFORCE');

        DoCallout(paramMap);

        return null

    }

    @future(callout=true)
    private static void DoCallout(Map<String, String> params)
    {
        Helper helper = new Helper();
        HTTPResponse res = helper.register(params);
        System.debug('Status Code--'+res.getStatusCode());
        System.debug('Status --'+res.getStatus());

        if (res.getStatusCode() == 201 && res.getStatus() == 'Created'){

        }

    }

3 Answers 3

3

There're two ways to do it. First one is via javascript - create a remote action, call it from javascript and on success redirect the VF page. Second one is to use Continuation object.

1

If you do a callout in @future, then it's executed in a separate context and you will not be able to come back in it. Try Remote Actions instead (see JavaScript Remoting docs), you should be able to execute a redirect with JS during the callback.

1

From documentation:

Methods with the future annotation must be static methods, and can only return a void type. The specified parameters must be primitive data types, arrays of primitive data types, or collections of primitive data types. Methods with the future annotation cannot take sObjects or objects as arguments.

Future methods are designed to run in a different context from the calling controller as per the resource availability. That's why it has a void return type. And because of void you can't redirect to any other VF page.

Go through the documentation and understand it's purpose and modify your logic accordingly

You must log in to answer this question.

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