The easiest way I've found to do this is using window.setTimeout()
with an abitrary low delay
value which calls an apex:actionFunction
that rerenders a dummy apex:outputPanel
within an apex:actionRegion
.
<script>
window.setTimeout(function() { setParams("param1Value", "param2Value"); }, 1);
</script>
<apex:actionRegion>
<apex:actionFunction action="{!setParams}" name="setParams" rerender="dummy">
<apex:param name="param1" assignTo="{!param1}" value="" />
<apex:param name="param2" assignTo="{!param2}" value="" />
</apex:actionFunction>
<apex:outputPanel id="dummy"/>
</apex:actionRegion>
The apex:actionRegion
and the dummy apex:outputPanel
prevent the whole page refreshing when the apex:actionFunction
is called which will result in your JavaScript being called everytime and ending up in a refresh loop.
The apex:actionRegion
also prevents the following error occurring when this is used on a page with an apex:inputFile
component.
apex:inputFile
can not be used in conjunction with an action component, apex:commandButton
or apex:commandLink
that specifies a rerender
or oncomplete
attribute
Your controller will looks something like this:
public class ParameterPageController
{
public String param1 { get; set; }
public String param2 { get; set; }
public PageReference setParams()
{
return null;
}
}
Edit:
You don't actually need the window.setTimeout()
call at all and can simply call your apex:actionFunction
directly, however you must put your call to your apex:actionFunction
after (although not necessarily directly after) it's declaration for this to work.
<apex:actionRegion>
<apex:actionFunction ... >
...
</apex:actionFunction>
..
</apex:actionRegion>
|
|
|
<script>
setParams("param1Value", "param2Value");
</script>
Best Answer
You can't do this with an outputlink - that simply generates an anchor tag, so when the user clicks the link their browser will carry out an HTTP GET request to the target URL. Any user input will be discarded. If you want to use a link rather than a button, you should use an apex:commandLink tag, as this will generate a postback of the form containing the user input, and the selected date will automatically be bound to the
SFDC_Employee__c.Month__c
field.Depending on what you are trying to achieve with the Generate link, you may need an action method bound to the commandlink, if you want to save the SFDC_Employee__c record, for example you'd have the following:
Page:
Controller: