Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I have commandbutton which sends post request to external webservice. But when I click button it re-renders same page without sending request

Visualforce code:

   <apex:inputText label="key" value="{! key}"/>
   <apex:commandButton action="{! sendActivationKey}" immediate="true" image="{! $Resource.authBtn }" style="border:none;background:none;padding:0px;vertical-align:top;">
   <apex:param name="akey" assignTo="{!akey}" value="{!key}"/>
   </apex:commandbutton> 

Apex code:

   public static void sendActivationKey() {
 String akey = System.currentPagereference().getParameters().get('akey');

 HttpRequest req = new HttpRequest();
    HttpResponse res = new HttpResponse();
    Http http = new Http(); 
    req.setMethod('POST'); // Method Type
    req.setEndpoint('http://sfdc.datadoctorit.com/authenticate'); // Server Url
    req.setHeader('Content-Type', 'application/x-www-form-urlencoded'); // Content Type
    req.setBody('key=' + akey); // Request Parameters
        res = http.send(req);      
  }

I don't understand where is the problem

share|improve this question
    
Put a page message tag to see if any errors are coming due to validation.Also why is immediate=true ?You want to skip some validations ? –  Mohith Shrivastava Dec 27 '12 at 12:54
    
try action="{!sendActivationKey}" instead of leaving a space ? –  Mohith Shrivastava Dec 27 '12 at 12:57
    
no luck with removing space. Removed 'immediate'. –  Bhushan Lodha Dec 27 '12 at 12:59
    
Ok please use pagereference method for your action call and say retun null. –  Mohith Shrivastava Dec 27 '12 at 13:07
    
samething with return null –  Bhushan Lodha Dec 27 '12 at 13:09

2 Answers 2

up vote 3 down vote accepted

The solution is very simple. Just add reRender="none" to your button, because without that the page will be reloaded and your parameter is lost. I would also recommend to use "PageReference" method instead of "static void".

share|improve this answer
    
Nice! I wasn't aware of that. It worked in my simple test case and way simpler. :) Do you know why his code would work with apex:commandLink without using reRender="none", but the reRender="none" is necessary when an apex:commandButton is used? Also, I'd think that as long as he is rerendering his command button and the nested apex:param, the apex:param assignment would not be lost. –  Peter Knolle Dec 27 '12 at 14:52

First, if the value that you actually want to use for the key is the key that is input in the apex:textInput that you have you can just use that value explicitly as it will be submitted with everything else within the form tag. If you want to narrow down what is submitted with the form then you can surround the code with an action:region.

Getting back to why your code, as it is, doesn't work is that I think that this is a bug in apex:param and apex:commandButton being used together in some situations. I have had issues with them before. Basically it doesn't set the value, so your akey will be null. If you switch the apex:commandButton to be an apex:commandLink it should work, although you'll now have a link instead of nice styled button.

You could also try an apex:inputHidden in the form, but outside of the apex:commandButton. For example:

<apex:inputHidden value="{!key}"/>

However, that leads me to the following...

If you already have the {!key} value available in your Visualforce page from your Controller you don't need to keep it on the page as a separate parameter. If you have {!key} as a property in the Controller then it will be available to you when the view state is reconstructed. If you need to store it in a separate property for some reason then you can create another one and store it in that.

Also, you'd have to change your method to not be static so that it has access to the controller's instance properties.

public String key { get; set; }
public String akey { get; set; } // you may or may not want this one.
public YourControllerConstructor() {
    key = //set the key somehow
}

// changed method signature
public PageReference sendActivationKey() {
    // Don't need to use the parameter map, 
    // just access the properties/variables directly
    req.setBody('key=' + key);  // from the key property
}

Check out the apex component reference for documentation on the various tags.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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