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
Abdul Khan 18Abdul Khan 18 

SelectOption and For loop problem....Class and Controller

I have exhausted all of 3 days trying to research how to do this but due to my limitations being new to the developing world I am afraid I really need some help. Any help is really appreciated. 
 
Controller

public with sharing class Editpositions {
    
    public Position__c myPosition;
    public Position__c editpos {get;set;}
    public Job_Posting__c editpost {get;set;}
    
    
    public Editpositions(ApexPages.StandardController controller) {
        this.editpos = (Position__c) controller.getRecord();
        
    }
    public Position__c getPosition() {
        return myPosition;
        
    }
    Public List <SelectOption> getitems() {
        <SelectOption>() options = new List<SelectOption>();
        //   for (Employment_Website__c mySite = [SELECT Id, Name FROM Employment_Website__c]);      
        //  options.add(new SelectOption(mySite.Id,mySite.Employment_Website__c)) 
        // Loop through the list and update the Name field  
        //    return options; 
    }
}
The issue is where I have put the code in comments // because it was giving me all kinds of errors I just have no idea what I am doing at this point all I know is that after <SelectOption>() options = new List<SelectOption>(); I need to specify to for loop my employment website to selecOption or something like that. 
VF Page

<apex:page standardController="Position__c" extensions="Editpositions">  
    <apex:sectionHeader title="{!Position__c.Name}" subtitle="Edit Records"/>
    <apex:form >
        <apex:pageBlock title="Create and edit Job Positions">
            
            <apex:pageBlockButtons location="both">
                <apex:commandButton action="{!save}" value="Save Record"/>
                <apex:commandButton action="{!cancel}" value="Cancel"/>
            </apex:pageBlockButtons>
            <apex:pageMessages />
            
            <apex:pageBlockSection title="Create New Position" columns="2">
                <apex:inputField value="{!Position__c.Name}"/>
                <apex:inputField value="{!Position__c.Responsibilities__c}"/>
                <apex:inputField value="{!Position__c.Job_Description__c}"/>
                <apex:inputField value="{!Position__c.Skills_Required__c}"/>
                <apex:inputField value="{!Position__c.Educational_Requirements__c}"/>
                <apex:inputField value="{!Position__c.Java__c}"/>
                <apex:inputField value="{!Position__c.Apex__c}"/>
                <apex:inputField value="{!Position__c.C__c}"/>
                <apex:inputField value="{!Position__c.Javascript__c}"/> 
                <apex:inputField value="{!Position__c.Travel_Required__c}"/>
                <apex:inputField value="{!Position__c.Location__c}"/>
                <apex:inputField value="{!Position__c.Open_Date__c}"/>
                <apex:inputField value="{!Position__c.Hire_By__c}"/>
                <apex:inputField value="{!Position__c.Min_Pay__c}"/>
            </apex:pageBlockSection>
            
            
            <apex:dataList value="{!Position__c}" var="Position__c">
                <apex:outputText value="{!Position.Name}"/>
            </apex:dataList>
            
            
        </apex:pageBlock>
    </apex:form>
</apex:page>

Allso I don't know how to let the two communicate like what code I need to put into the VF Page to run that. 
Once the process is it complete it should let me have a picklist in the Standard object Position where I can choose employment websites to choose to post job postings to. But I have to be able to save more than one website multi picklist type. Once I complete this it should work hopefully.

Thank you for any and all help!
 
Tarun J.Tarun J.
Hello Abdul,

Below is your update code:
Class-
public List<selectOption> getsite(){
	List<selectOption> options = new List<selectOption>();	
	for (Employment_Website__c mySite : [SELECT Id, Name FROM Employment_Website__c]){
		options.add(new selectOption(mySite.Id,mySite.Employment_Website__c));
	}
	return options;
}
VF page:
<apex:selectList id="web" value="Website" size="1" title="Website">
	<apex:selectOptions value="{!site}"></apex:selectOptions>
</apex:selectList>


-Thanks,
TK

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.
Abdul Khan 18Abdul Khan 18
Hey Tarun Thank you for the help.. So Far this is where I am with my updated code from getting help here and there..I think either code will work in this case but the error I get is the same when I try to run it. 

Invalid field Employment_Website__c for SObject Employment_Website__c for the Line I have underlined..what do you think it could be I can't figure it out unfortunately. 
 
public with sharing class Editpositions {
    
    public Position__c myPosition;
    public Position__c editpos {get;set;}
    public Job_Posting__c editpost {get;set;}
    public List<SelectOption> options {get;set;}
    
    public Editpositions(ApexPages.StandardController controller) {
        this.editpos = (Position__c) controller.getRecord();
    }
    public Position__c getPosition() {
        return myPosition;
    }
    public List <SelectOption> getitems() {
   
    List<SelectOption> options = new List<SelectOption>();
    
    Employment_Website__c mySite = [SELECT Id, Name
                                    FROM Employment_Website__c];      
    
    options.add(new SelectOption(mySite.Id, 'Id'));
    options.add(new SelectOption(mySite.Name, 'Name'));
    options.add(new SelectOption(mySite.Employment_Website__c, 'WebSite')); 
    
    return options; 
}

}


and 
 
<apex:page standardController="Position__c" extensions="Editpositions">  
    <apex:sectionHeader title="{!Position__c.Name}" subtitle="Edit Records"/>
    <apex:form >
        <apex:pageBlock title="Create and edit Job Positions">
            
            <apex:pageBlockButtons location="both">
                <apex:commandButton action="{!save}" value="Save Record"/>
                <apex:commandButton action="{!cancel}" value="Cancel"/>
            </apex:pageBlockButtons>
            <apex:pageMessages />
            
            <apex:pageBlockSection title="Create New Position" columns="2">
                <apex:inputField value="{!Position__c.Name}"/>
                <apex:inputField value="{!Position__c.Responsibilities__c}"/>
                <apex:inputField value="{!Position__c.Job_Description__c}"/>
                <apex:inputField value="{!Position__c.Skills_Required__c}"/>
                <apex:inputField value="{!Position__c.Educational_Requirements__c}"/>
                <apex:inputField value="{!Position__c.Java__c}"/>
                <apex:inputField value="{!Position__c.Apex__c}"/>
                <apex:inputField value="{!Position__c.C__c}"/>
                <apex:inputField value="{!Position__c.Javascript__c}"/> 
                <apex:inputField value="{!Position__c.Travel_Required__c}"/>
                <apex:inputField value="{!Position__c.Location__c}"/>
                <apex:inputField value="{!Position__c.Open_Date__c}"/>
                <apex:inputField value="{!Position__c.Hire_By__c}"/>
                <apex:inputField value="{!Position__c.Min_Pay__c}"/>
            </apex:pageBlockSection>
            <apex:selectList id="web" value="Website" size="1" title="Website">
                <apex:selectOptions value="{!site}"></apex:selectOptions>
            </apex:selectList>
            
            <apex:dataList value="{!Position__c}" var="Position__c">
                <apex:outputText value="{!Position.Name}"/>
            </apex:dataList>
            
            
        </apex:pageBlock>
    </apex:form>
</apex:page>

 
Tarun J.Tarun J.
Employment_Website__c is an object. You cannot use it like this. You can add values from fields of this object not the object itself like you have added for name and Id.

options.add(new SelectOption(mySite.Name, 'Name'));
Abdul Khan 18Abdul Khan 18
I think I get what your saying and mySite is a list as well ok Thank you let me try your method
Abdul Khan 18Abdul Khan 18
ok Tarun so here is what I managed so far I think I figured it out atleast no errors but I think there is just a little bit of an issue with it..I have a save function for the position but mySite is not on the position object so the multi select picklist when I select the websites it does not save that information and does not show up on the position page in the jop posting section.  I think I need to create a save function return - page reference I think holds the url /id  

create job posting for each id using the idvidual id from list and the id of position - does that make sense?

Final product since you last helped me.
 
Vf Page

 <apex:page standardController="Position__c" extensions="Editpositions">  
<apex:sectionHeader title="{!Position__c.Name}" subtitle="Edit Records"/>
<apex:form >
    <apex:pageBlock title="Create and edit Job Positions">

        <apex:pageBlockButtons location="both">
            <apex:commandButton action="{!save}" value="Save Record"/>
            <apex:commandButton action="{!cancel}" value="Cancel"/>
        </apex:pageBlockButtons>
        <apex:pageMessages />

        <apex:pageBlockSection title="Create New Position" columns="2">
            <apex:inputField value="{!Position__c.Name}"/>
            <apex:inputField value="{!Position__c.Responsibilities__c}"/>
            <apex:inputField value="{!Position__c.Job_Description__c}"/>
            <apex:inputField value="{!Position__c.Skills_Required__c}"/>
            <apex:inputField value="{!Position__c.Educational_Requirements__c}"/>
            <apex:inputField value="{!Position__c.Java__c}"/>
            <apex:inputField value="{!Position__c.Apex__c}"/>
            <apex:inputField value="{!Position__c.C__c}"/>
            <apex:inputField value="{!Position__c.Javascript__c}"/> 
            <apex:inputField value="{!Position__c.Travel_Required__c}"/>
            <apex:inputField value="{!Position__c.Location__c}"/>
            <apex:inputField value="{!Position__c.Open_Date__c}"/>
            <apex:inputField value="{!Position__c.Hire_By__c}"/>
            <apex:inputField value="{!Position__c.Min_Pay__c}"/>

            <apex:selectList multiselect="true" id="web" value="{!Website}" size="4" title="Website">
                <apex:selectOptions value="{!site}"></apex:selectOptions>
            </apex:selectList>
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:form>
 
Controller

public with sharing class Editpositions {

    public Position__c myPosition;
    public Position__c editpos {get;set;}
    public Job_Posting__c editpost {get;set;}
    public string[] Website {get;set;}

    public PageReference save() {

        return null;
    }

    public Editpositions(ApexPages.StandardController controller) {
        this.editpos = (Position__c) controller.getRecord();
    }
    public Position__c getPosition() {
        return myPosition;
    }

   public List<selectOption> getsite(){
    List<selectOption> options = new List<selectOption>();  
    for (Employment_Website__c mySite : [SELECT Id, Name FROM Employment_Website__c]){
        options.add(new SelectOption(mySite.Id,mySite.Name));
    }
    return options;
}
}