[SalesForce] How to set defaults when creating a record from a visualforce page button/link

I can create a link or button in a visualforce page that references an object's links or buttons with a line like this:

<apex:commandButton action="{!URLFOR($Action.SiteProgram__c.New_Holidaybtn)}" ...

but when I do this, any default values that I've set via constructs like this "{!SiteProgram__c.Name}" in the original button or link show up blank. Here's the code for the button. I've checked the ID ("00N00000008igMg") and it is correct for the field in the new object.

/a0e/e?CF00N00000008igMg={!SiteProgram__c.Name}&CF00N00000008igMg_lkid={!SiteProgram__c.Id}&retURL={!SiteProgram__c.Id}

The Visualforce page uses the object's standard controller. The object is a custom object. What have I missed? Can this be done with a standard controller? I realize it could be done with a controller extension, but I'm hoping to keep this simple(r).

Here's the full VF page code.

<apex:page standardController="SiteProgram__c"  docType="html-5.0" standardStylesheets="false" showheader="false" sidebar="false">
<apex:form >
<apex:outputLabel value="Site: " for="SiteOF"/>
<apex:outputField label="Site" id="SiteOF" value="{!SiteProgram__c.Name}"/><br/>
<apex:outputLabel value="District: " for="DistrictOF"/>
<apex:outputField label="District" id="DistrictOF" value="{!SiteProgram__c.SchoolDistrictProgram__c}"/><br/> 
<apex:outputLabel value="Start Date: " for="StartDateOF"/>        
<apex:outputField id="StartDateOF" value="{!SiteProgram__c.Start_Date__c}"/><br/>   
<apex:outputLabel value="End Date: " for="EndDateOF"/>               
<apex:outputField id="EndDateOF" value="{!SiteProgram__c.End_date__c}"/> <br/>

<apex:commandButton action="{!URLFOR($Action.SiteProgram__c.New_Holidaybtn)}" value="Create New Holiday" id="whatevs"></apex:commandButton>
<apex:commandButton action="{!URLFOR($Action.SiteProgram__c.New_Holidaylnk)}" value="Create New Holiday" id="whatevs6"></apex:commandButton>

</apex:form>
</apex:page>

edited to add I'm not going to add my own answer yet as I'd still like to know how to use URLFOR with $Action and passed inputs.

I tried many variations on the following including using commandlink instead of commandbutton:

<apex:commandButton action="{!URLFOR($Action.SiteProgram__c.New_Holiday,null,[CF00N00000008igMg=SiteProgram__c.Name,CF00N00000008igMg_lkid=SiteProgram__c.ID,saveURL=SiteProgram__c.ID])}" value="Create New Holiday" id="whatevs66"></apex:commandButton>

as there seems to be some disagreement out there about quotes and param names I also tried

<apex:commandButton action="{!URLFOR($Action.SiteProgram__c.New_Holiday,null,['CF00N00000008igMg'=SiteProgram__c.Name,'CF00N00000008igMg_lkid'=SiteProgram__c.ID,'saveURL'=SiteProgram__c.ID])}" value="Create New Holiday" id="whatevs66"></apex:commandButton>

In all cases, the resulting URL had the parameter names but blank values. I suspect the parameter names came from the underlying action, but I haven't tested that out yet.

Solution – replaces a previous answer based on straight URL hacking

The key point here is that the values between the square brackets "[]" (URLFOR parameter 3) have their own syntax. The URLFOR call is within the bounds of the the curly brackets "{}", so I suspect the rule switch is necessary to avoid confusion. Text needs to quoted (eg, 'CF00N00000008igMg_lkid') and variables don't need any special decoration. The equals sign does not need quotes. Concatenating values requires a "&".

<apex:commandButton action="{!URLFOR($Action.Holiday__c.New,null,['CF00N00000008igMg'=SiteProgram__c.Name,'CF00N00000008igMg_lkid'=SiteProgram__c.ID,'saveURL'='apex/SimpleSiteProgram?id='&SiteProgram__c.ID])}" value="Create New Holiday" id="holidaybtn"></apex:commandButton>

Best Answer

Found a nice blog that explains the URLFOR Method:

{!URLFOR(target, id, [inputs], [no override])}

Parameters shown in brackets ([]) are optional.

target: You can replace target with a URL or action, s-control or static resource.
id: This is id of the object or resource name (string type) in support of the provided target.
inputs: Any additional URL parameters you need to pass you can use this parameter. you will to put the URL parameters in brackets and separate them with commas ex: [param1="value1", param2="value2"]
no override: A Boolean value which defaults to false, it applies to targets for standard Salesforce pages. Replace "no override" with "true" when you want to display a standard Salesforce page regardless of whether you have defined an override for it elsewhere.

Basically, you just call the existing new holiday button and set the parameters in the URLFOR like this...

<apex:commandButton action="{!URLFOR($Action.Holiday__c.New,null
,['CF00N00000008igMg'=SiteProgram__c.Name,'CF00N00000008igMg_lkid'=SiteProgram__c.Id, 'retURL'=SiteProgram__c.Id])}" value="Create New Holiday" id="theButton"/>
Related Topic