salesforce – Retaining selected list item value when editing visualforce page

Hoping someone can help with this, I have apex code which generates a select list on a Visualforce page, this works fine and the value is saved, the issue I have is that when going back to edit the page the selected value is defaulting to the first value in the list whereas it should default to the value selected.

VF page

    <apex:page standardController="Service__c" extensions="ServiceParameterExtension,ServiceBroadbandController">

    <style type="text/css">
        .commentSize { width: 90%}
    </style>


    <apex:sectionheader title="{!$ObjectType.Service__c.label} Edit" subtitle="{!IF(ISNULL(Service__c.Name), 'New Service',Service__c.Name)}"
    />
    <apex:pagemessages id="editPageMessage"></apex:pagemessages>
       <apex:form rendered="{!contains(Service__c.Type__c, 'Change')}">
            <apex:pageblock mode="edit" title="{!$ObjectType.Service__c.label} Edit">
                <apex:pageBlockButtons >
                    <apex:commandButton value="Save" action="{!Save}" />
                    <apex:commandButton value="Cancel" action="{!Cancel}" />
                </apex:pageBlockButtons>
                <apex:outputPanel >
                    <apex:pageBlockSection title="Information" showheader="true" columns="2" id="InformationChange">
                        <apex:inputField value="{!Service__c.Product__c}" />
                        <apex:inputField value="{!Service__c.Status__c}" />
                        <apex:inputField value="{!Service__c.Type__c}" />
                        <apex:inputField value="{!Service__c.Associated_MSA__c}" />
                        <apex:selectList value="{!changetypeselected}" label="Change Type" size="1">
                            <apex:selectOptions value="{!items}"/>
                        </apex:selectList>
                        <apex:inputField value="{!Service__c.C_I_Number__c}" label="OMS Order No." />
                        <apex:outputField value="{!Service__c.RecordTypeId}"/>
                        <apex:inputField value="{!Service__c.Ordered_Circuit_ID__c}" label="Openreach Ref." />
                        <apex:inputField value="{!Service__c.Product_Variant__c}" />
                        <apex:inputField value="{!Service__c.New_Account__c}" />
                        <apex:inputField value="{!Service__c.Static_IP__c}" label="Static IP Address required?"/>
                        <apex:inputField value="{!Service__c.Static_IP_2__c}" label="Static IP Address" />
                        <apex:inputField value="{!Service__c.Cancellation_Reason__c}" />
                        <apex:inputField value="{!Service__c.L2S__c}" />                        
                        <apex:inputField value="{!Service__c.Openreach_Cablelink__c}" />                        
                        <apex:inputField value="{!Service__c.eir_NGQ__c}" />
                        <apex:inputField value="{!Service__c.OMS_VIM_Circuit_ID__c}" label="OMS Circuit Ref." />
                        <apex:inputField value="{!Service__c.Completion_Date__c}" />                                              
                    </apex:pageBlockSection>
                    <apex:pageBlockSection title="Order Details" showHeader="true" columns="2" id="OrderDetailsChange">
                        <apex:inputField value="{!Service__c.Quantity__c}" />
                        <apex:inputField value="{!Service__c.Account_Number__c}" />
                        <apex:outputLabel value=""/>
                        <apex:inputField value="{!Service__c.Existing_IPVPN_Circuit_ID__c}"/>
                    </apex:pageBlockSection>
                    <apex:pageBlockSection title="Comments" showheader="true" columns="1" id="CommentsChange">
                        <apex:inputField value="{!Service__c.Comments__c}" styleClass="commentSize"/>
                    </apex:pageBlockSection>
                    <apex:pageBlockSection title="Contracting Part Details" showHeader="true" columns="2" id="ContractingPartDetailsChange">
                        <apex:inputField value="{!Service__c.Site_Contact_Name__c}" required="true"/>
                        <apex:inputField value="{!Service__c.Welcome_Pack_Email_new__c}" />
                        <apex:inputField value="{!Service__c.Site_Contact_Phone_No_2__c}" />
                        <apex:inputField value="{!Service__c.Welcome_Pack_Email_2_new__c}" />
                        <apex:inputField value="{!Service__c.Site_Address__c}" style="width: 60%;"/>
                        <apex:inputField value="{!Service__c.Welcome_Pack_Email_3_new__c}" />
                    </apex:pageBlockSection>
                    <apex:pageBlockSection title="Admin Area" showheader="true" columns="2" id="AdminAreaChange">
                        <apex:outputLabel value=""/>
                        <apex:inputField value="{!Service__c.Approved__c}"/>
                    </apex:pageBlockSection>
                    <apex:pageBlockSection title="System Information" showHeader="true" columns="1" id="SystemInformationChange">
                        <apex:outputField value="{!Service__c.RecordTypeId}"/>
                    </apex:pageBlockSection>
                </apex:outputPanel>
            </apex:pageblock>
        </apex:form>
</apex:page>

Apex Controller

public with sharing class ServiceBroadbandController {
    public String changetypeselected;
    Service__c thisRecord;
    public ServiceBroadbandController(ApexPages.standardController controller) {
        thisRecord = (Service__c)controller.getRecord();
        thisRecord.Static_IP__c="No";
    }
    public PageReference Save(){
        if (validations()) {
            thisRecord.Change_Type__c = changetypeselected;
            Database.UpsertResult saveResult = Database.upsert(thisRecord, false);
            if(saveResult.isSuccess()){
                PageReference savePage = new PageReference("https://stackoverflow.com/" + thisRecord.id);
                savePage.setRedirect(true);
                return savePage;
            }else{
                for(Database.Error err : saveResult.getErrors()){
                    ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, err.getMessage()));                
                }              
            }  
        }         
        return null;    
    }
    public List<SelectOption> getItems(){
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('','--None--'));
        options.add(new SelectOption('Add_static_IP','Add Static IP'));
        options.add(new SelectOption('Remove_static_IP','Remove Static IP'));
        options.add(new SelectOption('Change_SLA','Change SLA'));
        options.add(new SelectOption('Change_Bandwidth','Change Bandwidth'));
        return options;
    }
    public String getchangetypeselected(){
        return changetypeselected;
    }
    public void setchangetypeselected(String changetypeselected){
        this.changetypeselected=changetypeselected;
    }


}

Leave a Comment