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
dza84dza84 

Set default value for multiple fields (using controller extension)

Hi all,

 

I have an Apex page which only renders certain pageblock sections according to the user's selection.

 

In some cases, required fields will be hidden (per the below code snippet).

 

<apex:page standardController="opportunity" extensions="OppExt">

=== other code goes here == <apex:inputHidden value="{!opportunity.name}" id="name"/> <apex:inputHidden value="{!opportunity.Channel__c}" id="channel"/> <apex:inputHidden value="{!opportunity.closedate}" id="closedate"/> <apex:inputHidden value="{!opportunity.stagename}" id="stagename"/> <apex:inputHidden value="{!opportunity.probability}" id="probability"/>

== rest of my code goes here ==

</apex:page>

 

In order to be able to save the record, I need to ensure that required fields are populated with something in the background (ie a default value). So I do this via a controller extension.

 

However, the only success I've had is with setting a default value for the opportunity name. I've had no luck with the other fields.

 

public class OppExt {

    public OppExt(ApexPages.StandardController controller)
    {Opportunity opp = (Opportunity) controller.getRecord(); opp.name = 'Default value goes here';}

}

 

Can anyone help me with assigning default values to the remaining hidden fields (channel, stagename, closedate, probability)?

 

- Chris

 

Best Answer chosen by Admin (Salesforce Developers) 
harsha__charsha__c

Use

 

date.valueof(date.newinstance(date.today().addYears(2).year(), 12, 31))

All Answers

harsha__charsha__c

Did you try giving some default vaues to the other fields?

 

If tried which error did you get?

 

 

Note: You shoud pass the values of same type of the fields.

 

if the field is a number field you should pass only number and same for date fields, etc..

dza84dza84

Hi Harsha

 

I can set all the other defaults fine without any errors. I'm struggling with closedate though.

 

I'm trying to set the default close date to 31 Dec in 2 years from the current year (so if it's 2013, then set to Dec 31, 2015. If it's 2014, then set to Dec 31 2016 etc.)

 

public class OppExt {

    public OppExt(ApexPages.StandardController controller)
    {Opportunity opp = (Opportunity) controller.getRecord(); opp.name = 'Default'; opp.stagename = 'inflow: initial review'; opp.amount = 0; opp.probability = 0; opp.closedate = ??????????;}


}

 

harsha__charsha__c

Hi use this to get the date as you specified.

 

date.newinstance(date.today().addYears(2).year(), 12, 31)

 

Assign this to your close date

 

Dont forget to give kudos if this helped you.

dza84dza84

Hi Harsha

 

OK great this works, however I think there is a date locale issue. When I go to save the record I get the following error

 

"Value 'Thu Dec 31 00:00:00 GMT 2015' cannot be converted from Text to Date"
harsha__charsha__c

Use

 

date.valueof(date.newinstance(date.today().addYears(2).year(), 12, 31))

This was selected as the best answer
dza84dza84

Still had the same error but figured it out

 

Changed closedate from an input hidden value to

 

<apex:outputText value="{0,date,short}" rendered="false">
<apex:param value="{! Opportunity.CloseDate}" />
</apex:outputText>

 

and it works

 

Thanks for all your help!