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
logontokartiklogontokartik 

Picklist is resetting when trying to save a record.

Hi.

 

I am trying to save a record into a custom object from a picklist that I generated for a list of fields. When i try to click the save action, the picklist is resetting and I am not able to get the value from selected picklist. Please let me know what is that I am doing wrong with the code. 

<apex:page controller="testobjfields">
   <apex:form>
   <apex:pageBlock>
   <apex:commandButton value="Save" action="{!save}" />
   <apex:PageBlockSection columns="3" >     
               
            <apex:pageBlockSectionItem >                  
                <apex:outputPanel layout="block" styleClass="requiredInput" >
                <apex:outputPanel layout="block" styleClass="requiredBlock" />  
                  <apex:SelectList value="{!savedsearchFields.Fieldname__c}" size="1" >
                     <apex:selectOptions value="{!objfields}"></apex:selectOptions>

                  </apex:SelectList>
                </apex:outputPanel>            
             </apex:pageBlockSectionItem>
     
         <apex:pageBlockSectionItem >
            <apex:outputPanel layout="block" styleClass="requiredInput" >
            <apex:outputPanel layout="block" styleClass="requiredBlock" />  
               <apex:inputField value="{!savedsearchFields.Operator__c}" style="border:{!nameErr};"/>
            </apex:outputPanel>
         </apex:pageBlockSectionItem>
       
         <apex:pageBlockSectionItem >
           <apex:outputPanel layout="block" styleClass="requiredInput" >
            <apex:outputPanel layout="block" styleClass="requiredBlock" />  
               <apex:inputField value="{!savedsearchFields.DefaultValue__c}" style="border:{!nameErr};"/>
            </apex:outputPanel>
          </apex:pageBlockSectionItem>
           
                
       </apex:PageBlocksection> 
       </apex:pageBlock>
       </apex:form>
</apex:page>

 My controller is 

 

public with sharing class testobjfields{

    public SavedSearchField__c savedsearchfields{get;set;} 
    
    public List<SavedSearchField__c> multisavedfields{get;set;} 
    
    public String listObject{get;set;}
    public String fieldname{get;set;}
    
    //CSS error formatting vars
    
     public string nameErr{get;set{this.nameErr=value;}}
    public string visibilityErr{get;set{this.visibilityErr=value;}}
    
    public testObjfields() {   
     
   // initialize the objects for inserts

        savedsearchFields = new SavedSearchField__c();
     
        multisavedfields = new List<SavedSearchField__c>();
        multisavedfields.add(new SavedSearchField__c());        
        
          }
    
        
        
   
    public void addrow(){
        system.debug('Fieldname: ' + savedsearchFields.name);
        multisavedfields.add(savedsearchFields);
        
       }
        
    public PageReference save() { 

        try {
       insert savedSearchFields;
       system.debug('fieldname' + savedsearchfields.fieldname__c);
       system.debug('operator' + savedsearchfields.operator__c);
       /*  for (Integer i = 0 ; i<multisavedfields.size(); i++) {
        multisavedfields[i].SavedSearch__c = selectedsearch.Id;
        system.debug('Field Name' + multisavedfields[i].Name);
         }
         insert multisavedfields;
      
      /*   selectedList.addAll(selectedNames);
         for (Integer i = 0 ; i<selectedList.size(); i++) {
           system.debug('selected' + selectedList[i]);  
           SavedSearchColumn__c savedColumn = new SavedSearchColumn__c();
           savedColumn.name = selectedList[i];
           savedColumn.SequenceNumber__c = i;
           savedColumn.SavedSearch__c = selectedsearch.Id;
           multisavedcolumns.add(savedColumn);       
         }
        insert multisavedcolumns; */
       
        }
        catch(Exception e){
        system.debug('No record inserted');
        
        return null;
        }
        return null;}
    
     
    public List<SelectOption> getObjfields() { 
   
      List<SelectOption>  options = new List<SelectOption>();
       options.add(new SelectOption('','--Select Field --'));
        Map<String, Schema.SobjectField> fields =
        Schema.SobjectType.Location__c.fields.getMap();
        for (String s : fields.keySet()) {
            Schema.SobjectField so = fields.get(s);
            DescribeFieldResult f = so.getDescribe();
            if (s != 'Name') { // name is always displayed       
            options.add(new SelectOption('',f.getlabel())); 
        }
    }
            return options;
 }  
         
 }

 Any help is appreciated. 

 

Thanks.

Kartik

 

 

 

Sandeep123Sandeep123
Problem is with your picklist value whenever you are adding value.

you are not appending Picklist value to option, please use below code
public List<SelectOption> getObjfields() {

List<SelectOption> options = new List<SelectOption>();
options.add(new SelectOption('','--Select Field --'));
Map<String, Schema.SobjectField> fields =
Schema.SobjectType.Location__c.fields.getMap();
for (String s : fields.keySet()) {
Schema.SobjectField so = fields.get(s);
DescribeFieldResult f = so.getDescribe();
if (s != 'Name') { // name is always displayed
options.add(new SelectOption(f.getvalue(),f.getlabel()));
}
}
return options;
}

let me know if it is work for you...