0

My client needs to have the Stage name with a default value, this mean the value should already be there when you click on the New button.

Workflows, Process Builders or Triggers doesn't work because the user will need to set a value for the Stage.

Hacking the URL of the NEW button won't work. I noticed it's possible to override the NEW button with a VisualForce page. I have created one, but I´m still struggling to have the Stage as a default value.

I found some information that it´s required to have a custom controller to set the default value, but I don't know what I´m doing wrong here. I'm new with Visualforce page and APEX code.

<apex:page standardcontroller="Opportunity" extensions="stagePickListController" tabstyle="Opportunity">

<apex:form id="Form">   

    <apex:sectionHeader title="Opportunity Edit" subtitle="{!if(Opportunity.Id==null,'New Pre-Opportunity',Opportunity.Name)}"/>  

    <apex:pageBlock mode="edit" id="oppPB" title="Opportunity Edit">

        <apex:pageBlockButtons >
            <apex:commandButton action="{!save}" value="Save"/>
            <apex:commandButton action="{!cancel}" value="Cancel"/>
        </apex:pageBlockButtons>

        <apex:pagemessages ></apex:pagemessages>

        <apex:pageBlockSection id="OpportunityInformation" title="Pre-Opportunity Information">

            <apex:inputField value="{!Opportunity.OwnerId}"></apex:inputField>
            <apex:inputField value="{!Opportunity.AccountId}" required="true"></apex:inputField>
            <apex:inputField value="{!Opportunity.CloseDate}" required="true"></apex:inputField>
            <apex:selectList value="{!stage}" size="0">
                <apex:selectOptions value="{!Opportunity.StageName}"/>
            </apex:selectList>
            <apex:inputField value="{!Opportunity.Name}" required="true"></apex:inputField>

        </apex:pageBlockSection>

    </apex:pageBlock>    

</apex:form>

And this is my custom controller

    public class stagePickListController {

    private final Opportunity opp;
    public string stage {get; set;}
    public List<SelectOption> lstStages {get; set;}

    public stagePickListController(ApexPages.StandardController stdController) {
        this.opp = (Opportunity)stdController.getRecord();
    }

    public stagePickListController(){
        stage = 'S0 - Pre-Opportunity';
    }

    public List<SelectOption> getStages(){
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('S0 - Pre-Opportunity','S0 - Pre-Opportunity'));
        options.add(new SelectOption('Qualified','Qualified'));
        options.add(new SelectOption('Closed Lost','Closed Lost'));
        return options;
    }

}

1 Answer 1

1

Default constructor is not used to create an instance of extension of the page. you need to move stage = 'S0 - Pre-Opportunity'; to constructor with parameter.

and your extension (stagePickListController) should look like:

public class StagePickListController {
    private static String DEFAULT_STAGENAME = 'S0 - Pre-Opportunity';
    private final Opportunity opp;
    public List<SelectOption> stages {get; set;}

    public StagePickListController(ApexPages.StandardController stdController) {
        this.opp = (Opportunity)stdController.getRecord();
        initFields();
    }

    private void initFields(){
        this.opp.StageName = DEFAULT_STAGENAME;
        initStages();
    }

    private void initStages(){
        stages = new List<SelectOption>();
        // it is better to not hard-code picklist values
        stages.add(new SelectOption('S0 - Pre-Opportunity','S0 - Pre-Opportunity'));
        stages.add(new SelectOption('Qualified','Qualified'));
        stages.add(new SelectOption('Closed Lost','Closed Lost'));
    }
}

VF Page:

<apex:page standardcontroller="Opportunity" extensions="stagePickListController" tabstyle="Opportunity">
<apex:form id="Form">   
    <apex:sectionHeader title="Opportunity Edit" subtitle="{!if(Opportunity.Id==null,'New Pre-Opportunity',Opportunity.Name)}"/>  
    <apex:pageBlock mode="edit" id="oppPB" title="Opportunity Edit">
        <apex:pageBlockButtons >
            <apex:commandButton action="{!save}" value="Save"/>
            <apex:commandButton action="{!cancel}" value="Cancel"/>
        </apex:pageBlockButtons>
        <apex:pagemessages/>
        <apex:pageBlockSection id="OpportunityInformation" title="Pre-Opportunity Information">
            <apex:inputField value="{!Opportunity.OwnerId}"/>
            <apex:inputField value="{!Opportunity.AccountId}" required="true"/>
            <apex:inputField value="{!Opportunity.CloseDate}" required="true"/>
            <apex:selectList value="{!Opportunity.StageName}" size="4" multiselect="false">
                <apex:selectOptions value="{!stages}"/>
            </apex:selectList>
            <apex:inputField value="{!Opportunity.Name}" required="true"/>
        </apex:pageBlockSection>
    </apex:pageBlock>    
</apex:form>
3
  • Thank you so much. I receiving this error when I open my VF Invalid selectOptions found. Use SelectOption type in Apex.
    – Tiago
    Dec 15, 2017 at 13:37
  • @Tiago see my updated version of an answer Dec 15, 2017 at 13:42
  • it worked, thanks. What is your suggestion for the picklist instead of hard-coding? Should I make a SOQL to get the picklist value ?
    – Tiago
    Dec 15, 2017 at 16:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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