function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Greg CooganGreg Coogan 

Redirect on HttpRequest POST

I need to do a HTTP POST where I set parameters in the headers. This will be triggered via custom button, but it cannot be a Javascript button. I cannot use PageReference for a redirect since it would b e a seperate operation from the POST. How can I get the POST operation to open/redirect to an external website as part of the POST operation?

I've reviewed existing documentation and also other forum posts, but none of them address my specific scenario.

I've got my Apex code written that is executed when the custom button loads a Visualforce page that passes data to the Apex code. I am just unable to get it to direct the user as part of the POST operation.
Best Answer chosen by Greg Coogan
Andy on CloudAndy on Cloud
How about in your VF page you submit a form using JavaScript instead of using Action method.  On that VF page, you construct a form that contains all the info either using hidden fields or using JavaScript, then when page is loaded, it automatically submits that form.  But if that is the case, you may be able to submit that form in your Custom Button (not sure if you could submit form in a custom button though but you could try).

All Answers

Andy on CloudAndy on Cloud
I want to understand your requirement before I answer, are you trying to use a custom button to perform some functions in Apex, then redirect the user from current page to another page when process is done?
Greg CooganGreg Coogan
On a Contact page, a user clicks a custom button that performs the following:
1. Retrieves a token from an external authentication page (Not what I'm needing help with)
2. Performs a POST that passes the token and some Contact fields to an external page

As part of step 2, we want to user to be directed to the page that is being posted to. The data passed using the POST will determine what information is displayed to the user.
Andy on CloudAndy on Cloud
Thanks for clarifications.  How about the custom button forward to a Visualforce page first, then in constructor of that controller, perform #2, then in your action method, it will redirect the user.  Does this work?
Greg CooganGreg Coogan
That was my first attempt, but the action method (used PageReference redirect) was a seperate operation from the POST. So, when the user was redirected, the destination didn't have the data passed along like we did with the POST.

I am still using a Visualforce page to use a custom controller with an action method at this time.
Andy on CloudAndy on Cloud
How about in your VF page you submit a form using JavaScript instead of using Action method.  On that VF page, you construct a form that contains all the info either using hidden fields or using JavaScript, then when page is loaded, it automatically submits that form.  But if that is the case, you may be able to submit that form in your Custom Button (not sure if you could submit form in a custom button though but you could try).
This was selected as the best answer
Greg CooganGreg Coogan
If I'm understanding correctly, you are suggesting I no longer use my Apex code, but use Javascript on the Visualforce page instead?
Andy on CloudAndy on Cloud
For redirect, yes.  For other logic, you can either do it at the Custom Button using JavaScript or within the same VF page that you do redirect.
Greg CooganGreg Coogan
Okay. I want it to be compatible with Lightning, so a Custom Button using Javascript isn't an option. I'll try out this suggestion and let you know.
Greg CooganGreg Coogan
I accomplished by using plain HTML form, instead of Apex:form. I used Javascript on the VF page to retrieve my token too. Thanks for your help! I'll still consider your answer of using Javascript a valid approach. I did start with it, but ended up with simplying putting HTML.
Andy on CloudAndy on Cloud
As long as it works for you, that's great!  Have fun coding!