希望有人可以帮助解决这个问题,我有 apex 代码在 Visualforce 页面上生成一个选择列表,这工作正常并且 value 被保存,我遇到的问题是当返回编辑页面时selected value 默认为列表中的第一个 value 而它应该默认为选中的 value。
音频页面
<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 控制器
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('/' + 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;
}
}
回答1
哪个选项列表,<apex:selectList value="{!changetypeselected}" label="Change Type" size="1">
?
尝试将其放入控制器中:changetypeselected = thisRecord.thisRecord.Change_Type__c;
它可能会抛出一些关于“字段在没有查询的情况下被检索”的信息,在这种情况下,您可以将 https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/apex_ApexPages_StandardController_addFields.htm 作为构造函数的第一行。
但是我怀疑您的代码过于复杂,您可以通过更改 VF 中的引用以明确提及该字段来减少很多 apex : <apex:selectList value="{!Service__c.Change_Type__c}" label="Change Type" size="1">