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
bpl3792bpl3792 

Need help building a list populated by soql statement

I'm trying to populate a list from a soql statment. It's currently populating 1 item but I'm not sure where the loop should go.

  <apex:selectList id="Modelbox" value="{!Models}" size="1" title="Type" onchange="searchServer(this.options[this.selectedIndex].text)">                                
  <apex:selectOptions value="{!items}"></apex:selectOptions>                              
  </apex:selectList>

 Here's the controller

    //Variables
    String[] models = new String[]{};

    
    //sobject
    public IT_Asset__c ITA {get;set;}
     
   
    public AssetManController() 
    {
     ITA=[SELECT Workstation_Model__c FROM IT_Asset__c]; 
        
    }
    public List<SelectOption> getItems() 
        {
        List<SelectOption> options = new List<SelectOption>();
        for(integer i=0;i<30;i++)
        {
        options.add(new SelectOption(ITA.Workstation_Model__c,ITA.Workstation_Model__c));
        }
        
        
        return options;
        }

       public string[] getModels()
       {
           return models;
       }
      public void setModels(string[] models)
       {
           this.models=models;
       }

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
ColinKenworthy2ColinKenworthy2
//Variables
    
String models;
    
    //sobject
    public 
List<IT_Asset__c>
 ITA {get;set;}
     
   
    public AssetManController() 
    {
     ITA=[SELECT Workstation_Model__c FROM IT_Asset__c]; 
        
    }
    public List<SelectOption> getItems() 
        {
        List<SelectOption> options = new List<SelectOption>();
        for(integer i=0;i< 
ITA.size()
;i++)
        {
        options.add(new SelectOption( 
ITA[i]
.Workstation_Model__c, 
ITA[i]
.Workstation_Model__c));
        }
        
        
        return options;
        }

       public 
string
 getModels()
       {
           return models;
       }
      public void setModels( 
string
 models)
       {
           this.models=models;
       }

This should help

All Answers

ColinKenworthy2ColinKenworthy2
//Variables
    
String models;
    
    //sobject
    public 
List<IT_Asset__c>
 ITA {get;set;}
     
   
    public AssetManController() 
    {
     ITA=[SELECT Workstation_Model__c FROM IT_Asset__c]; 
        
    }
    public List<SelectOption> getItems() 
        {
        List<SelectOption> options = new List<SelectOption>();
        for(integer i=0;i< 
ITA.size()
;i++)
        {
        options.add(new SelectOption( 
ITA[i]
.Workstation_Model__c, 
ITA[i]
.Workstation_Model__c));
        }
        
        
        return options;
        }

       public 
string
 getModels()
       {
           return models;
       }
      public void setModels( 
string
 models)
       {
           this.models=models;
       }

This should help

This was selected as the best answer
bpl3792bpl3792

Still only displaying 1 record. It's strange because the record it's displaying isn't the first record or the last...it's like 1/4 through.

 

    //Variables
    
    String models;
    
    //sobject
    public List<IT_Asset__c> ITA {get;set;}
   
     
   
    public AssetManController() 
    {
     ITA=[SELECT Workstation_Model__c FROM IT_Asset__c]; 
        
    }
    public List<SelectOption> getItems() 
        {
        List<SelectOption> options = new List<SelectOption>();
        for(integer i=0;i<ITA.size();i++)
        {
        options.add(new SelectOption(ITA[i].Workstation_Model__c,ITA[i].Workstation_Model__c));
        }
        
        
        return options;
        }

      public string getModels()
       {
           return models;
       }
      public void setModels(string models)
       {
           this.models=models;
       }

 

ColinKenworthy2ColinKenworthy2

This works for me

                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Record Type of new record"/>
                    <apex:selectList value="{!selectedRecordType}" title="Record Type of new record" size="1">
                        <apex:selectOptions value="{!recTypeSelections}"/>
                    </apex:selectList>
                </apex:pageBlockSectionItem>

 and in the controller

 

    // allows user to select the record type
    Public List<RecordType>   recTypeList          {get;set;}
    public List<SelectOption> recTypeSelections    {get;set;}
    public String             selectedRecordType   {get;set;}

    public CONSTRUCTORMETHOD()
    {
        // record types for picklist selection on vf page
        recTypeSelections = new List<SelectOption>();
        recTypeList = [Select Name from RecordType WHERE ... AND isActive = true   Order By Name ];
        for (RecordType rt : recTypeList) {
            recTypeSelections.add( new SelectOption(rt.Name, rt.Name) );
        }

    }

 I think its the same apart from the onchange in your VF

 

 

 

bpl3792bpl3792

Your code works exactly the way it supposed to. The failure was on my end. I've just started playing with visualforce and apex so my understanding is a little off. I thought my code actually pulled data down from the "Workstation_Model__c" object. After talking to one of our salesforce devs I found out that all I needed to do is call the standard controller for that object. So after all that all I needed to do was    <apex:inputfield value="{!IT_Asset__c.Workstation_Model__c}"/>

 

Thanks for the help!

ColinKenworthy2ColinKenworthy2

If Workstation Model is a picklist field then <apex : inputfield ...  is exactly what you should use.

 

What you were trying to do would only display the values that had been stored in the IT Asset records.