0

I have a VF page in which I have created a picklist using SelectList and SelectOption. I am now facing final hurdle: Selected value is getting changed to default one once I click on Save . I am not sure how should I code so that the value of SelectList doesn't revert back to default one and remain the selected one when I click on Save.

This is some part of my VF page:

<apex:pageBlockSection title="Technical Approval" id="techapprovalsection" >
         <apex:pageBlock> 
           <apex:pageBlockSectionItem >
               <apex:outputLabel value="Technical Approver 1"></apex:outputLabel>
               <apex:selectList multiselect="false" required="true" value="{!TechnicalApprover1}" size="1">
                <apex:selectOptions value="{!TechnicalAppList}"/>
               </apex:selectList>
             </apex:pageBlockSectionItem>
           </apex:pageBlock>

         <apex:pageBlock >
           <apex:pageBlockButtons location="bottom">
             <apex:commandButton value="Save" action="{!dosave}"/>
           </apex:pageBlockButtons>
         </apex:pageBlock>

Below is some portion of my Controller class:

public class SubmitforApprovalController {
public SubmitforApprovalController() {
searchTechApproval(OfferType, SalesPrice, MOMargin, objMO);
}

public List<SelectOption> searchTechApproval(String OfferType, Decimal SalesPrice, Decimal MOMargin, Custom1__c objMO)
{
 QueriedTechApprovers= [Select Id, Name, Approval_Level__c, GID__c, Mail__c, Offer_Type__c, 
                        SalesPrice_Range__c, Signature__c, TotalMargin_Range__c,
                        Max_Stages__c, Stage__c, First_Name__c, Last_Name__c from Approver__c 
                        Where Sales_Volume_LB__c <= :TotalPrice AND Sales_Volume_UB__c > :TotalPrice];
if(QueriedTechApprovers.size()>0){
            TechnicalApprover1 = QueriedTechApprovers[0].Name;

if(QueriedSalesApprovers.size()>0){
        for(Approver__c app : QueriedSalesApprovers){
          lstSalesApprovers.add(new SelectOption (app.Name,app.Name));
 }
}

public pageReference dosave(){
if(!String.isEmpty(TechnicalApprover1) && QueriedTechApprovers.size()>0){
       Approver__c TechApproverChange1 = [Select Id, Name, Stage__c,First_Name__c, Last_Name__c from Approver__c WHERE ID =: TechIDs AND Name =: TechnicalApprover1 LIMIT 1];
        Approval__c apptech1 = new Approval__c();
        apptech1.Main_Offer_Name__c = objMO.Id;
        apptech1.Approver_Name__c = TechApproverChange1.Id;
        apptech1.Stage__c = TechApproverChange1.Stage__c;
        insert apptech1;
        isTechApprover = true;
     }

Whenever I click on Save button, page refreshes and value of picklist changes to default one set by controller. I want it to remain as it is.

1
  • Please do not repost closed questions. – David Reed Apr 29 '20 at 13:42
0

I am assuming you have a String variable named TechnicalApprover1 with a getter/setter. Since there is no rerender attribute in your command button, the page refreshes.

Step 1: Add an id to one of the pageBlock and rerender that id in commandbutton.

 <apex:pageBlock id="selectlist">
    <apex:pageBlockSectionItem >
        <apex:outputLabel value="Technical Approver 1"></apex:outputLabel>
        <apex:selectList multiselect="false" required="true" value="{!TechnicalApprover1}" size="1">
            <apex:selectOptions value="{!TechnicalAppList}"/>
        </apex:selectList>
    </apex:pageBlockSectionItem>
</apex:pageBlock>
<apex:pageBlock >
    <apex:pageBlockButtons location="bottom">
        <apex:commandButton value="Save" action="{!dosave}" rerender="selectlist"/>
    </apex:pageBlockButtons>
</apex:pageBlock>

Also, in the save method return null. I think you are doing that already but is is not visible in your code.

public pageReference dosave(){

// Your code
return null;
}
3
  • I have tried that but it still refreshed the pageblock which in turn calls the method searchTechApproval and again default value for TechnicalApprover1 is set when I click on Save. – Rahul Apr 29 '20 at 15:02
  • Then you should keep a check in your code which only sets the value for TechnicalApprover1 if it is not blank. You can do it where you are setting the value TechnicalApprover1 = QueriedTechApprovers[0].Name; put this inside an if. – manjit5190 Apr 29 '20 at 15:04
  • That was such a simple yet effective solution. How did I not think of this? Thanks a lot for you help Manjit and stay Safe. – Rahul Apr 29 '20 at 16:59

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.