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
DevilJDevilJ 

Need help with selectlist and onchange request.

Hello,

 

I am needing help on a bit of code.  I am trying to make a call to a controller method from a apex:selectList drop down menu.  I want the code to send the value chosen in the dropdown menu (parameter) to the method on a "onchange" event.  I read a blog that showed how to make a call to an apex:actionFunction and then there forward the parameter to the controller method.  This works fine if I pas a test string, however, I have no idea on how to capture my dropdown selectionlist item into a variable and send it to the function.  If anyone could help, it would be appreciated.  Here is my code:

<apex:page tabstyle="Transportation_Services__c" controller="goGetScheduleQuery">
    <apex:form >
    <apex:actionFunction name="update" action="{!updateSchedule}" rerender="jsvalues">
            <apex:param name="phone" value="" />
                    </apex:actionFunction>
    <apex:pageblock > 
    <apex:pageBlockButtons >
        <apex:commandButton value="Schedule A Pickup" action="{!schedulePickup}"/>
        
        <apex:commandButton value="View the Map" action="{!mapPage}"/>
        </apex:pageBlockButtons>
        <apex:pageblocktable value="{!schedule}"  var="event" columnsWidth="75px,75px,75px,75px,75px,300px">
            <apex:column value="{!event.Pickup_Time__c}"/>
            <apex:column value="{!event.Client__c}"/>
            <apex:column value="{!event.Pickup_Location__c}"/>
            <apex:column value="{!event.Drop_off_Location__c}"/>
            <apex:column headerValue="Phone">
            <apex:selectList id="phoneChoice" value="{!event.Phone_Name__c}"  size="1" title="Phones" onchange="update('test selection')">
            <apex:selectOptions value="{!phones}" ></apex:selectOptions>
            </apex:selectList>
            </apex:column>
            <apex:column value="{!event.Notes_Transportation__c}"/>
            <apex:column value="{!event.Name}" rendered="no"/>
        </apex:pageblocktable>
        <apex:outputPanel id="jsvalues">
            <apex:outputText value="Value one: {!valueOne}" /><br/>
            <apex:outputText value="Value two: {!valueTwo}" /><br />           
        </apex:outputPanel>
    </apex:pageblock>
    </apex:form>
</apex:page>

 

 

bob_buzzardbob_buzzard

You'll need to access the selected item via javascript syntax.

 

I think the following should do it:

 

 

<apex:selectList id="phoneChoice" value="{!event.Phone_Name__c}"  size="1" title="Phones" onchange="update(this.options[this.selectedIndex])">

 

 

DevilJDevilJ

Bob,

 

Thanks for the reply.  I did get this to work using "this.value".  I am wondering if you could help me a little further (I'm a newbie).  Beside the value of the selectList, I need to also pass a parameter of another field in that record list.  Essentually what I am trying to do is create a table of events and allowing the user to select different variables in one field (selectList) and then update that particular record with the user's choice.  Here is a view of my interface:

 

 

What I need my app to do is, if the user chooses, say "Neptune", the record for the event with a Transportation Services Name of "Tran-00015" will have its Phone_Name__c field updated with "Neptune". This will occur when the selectList fires a onchange event.  My thinking is to have the onchange sent two parameters to a "updateSchedule" method in the controller that will do an Upsert SOQL for that record and update the Phone_Name__c field of that event.  Does that make sense?

Her is my updated code:

<apex:page tabstyle="Transportation_Services__c" controller="goGetScheduleQuery">
    <apex:form >
    <apex:actionFunction name="update" action="{!updateSchedule}" rerender="jsvalues">
            <apex:param name="phone" value="" />
            <apex:param name="name" value="" />
        </apex:actionFunction>
    <apex:pageblock > 
    <apex:pageBlockButtons >
        <apex:commandButton value="Schedule A Pickup" action="{!schedulePickup}"/>
        
        <apex:commandButton value="View the Map" action="{!mapPage}"/>
        </apex:pageBlockButtons>
        <apex:pageblocktable value="{!schedule}"  var="event" columnsWidth="75px,75px,75px,75px,75px,30px,100px">
            <apex:column value="{!event.Pickup_Time__c}"/>
            <apex:column value="{!event.Client__c}"/>
            <apex:column value="{!event.Pickup_Location__c}"/>
            <apex:column value="{!event.Drop_off_Location__c}"/>
            <apex:column headerValue="Phone">
            <apex:selectList id="phoneChoice" value="{!event.Phone_Name__c}"  size="1" title="Select assigned Phone" onchange="update(this.value,event.eventName.value)">
            
                        <apex:selectOptions value="{!phones}" ></apex:selectOptions>
                    </apex:selectList>
            </apex:column>
            <apex:column value="{!event.Notes_Transportation__c}"/>
            <apex:column id="eventName" value="{!event.Name}"/>
        </apex:pageblocktable>
        <apex:outputPanel id="jsvalues">
            <apex:outputText value="Value one: {!valueOne}" /><br/>
            <apex:outputText value="Value two: {!valueTwo}" /><br />           
        </apex:outputPanel>
    </apex:pageblock>
    </apex:form>
</apex:page>

 

 

Here is my controller method:

 

 

public PageReference updateSchedule() {
        valueOne = Apexpages.currentPage().getParameters().get('phone');
        valueTwo = Apexpages.currentPage().getParameters().get('name');
        Transportation_Services__c updateX = [Select Phone_Name__c from Transportation_Services__c Where Name = :valueTwo];
        updateX.Phone_Name__c = valueOne;
        update updateX;
        return null;
    }
    

 Any help would be appreciated. 

 

Thanks.

 

backupbackup

Hi DevilJ

I have done a similar thing.

In order to update the phone__c field to watever is selected in the selectlist menu into the respective record Tran-000015,

you have to perform a task similar to save button, only difference here is that instead of button we are going to use onchange event in the selectlist.

So use a SOQL query to get the record Id from the object using list and the update the value in the phone field with the value in the selectlist and then upsert the variable..

Hope it works.. 

arun sharma 37arun sharma 37
Thanks to bob_buzzard!!