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
kminevkminev 

Write text to a text field based on picklist selection

Hi,

 

I need to have a  picklist with values for example: ( Template 1, Template 2, Template 3 ) Anytime time this pick list's selection is changed I need to place different text value in a text field corresponding to a custom field.

 

I tried with an s-control and some javascript, however I was wondering if anyone managed to do this with a visual force code?

Best Answer chosen by Admin (Salesforce Developers) 
TehNrdTehNrd

I would do something like this. I just whipped this up in notepad real quick so there may be some typos.

 

 

public class MyController{

	public String selectedValue {get; set;}
	public String inputField {get; set;}
		
	public void changeValue(){
		inputField = selectedValue;
	}

}


<apex:page controller="MyController">
    <apex:form>
        <apex:selectList size="1">
            <apex:actionSupport onchange="{!changeValue}" rerender="input"/>
            <apex:selectionOption itemLabel="--None--" itemValue=""/>
            <apex:selectionOption itemLabel="1" itemValue="#1 was selected"/>
            <apex:selectionOption itemLabel="2" itemValue="#2 was selected"/>
            <apex:selectionOption itemLabel="3" itemValue="#3 was selected"/>
        </apex:selectList><br/>
        <apex:inputText value="{!inputText}" id="input"/>
    </apex:form>
</apex:page>

 

 

All Answers

TehNrdTehNrd

Where is the text value coming from? Is it stored in an object or hardcoded?

gv007gv007

Why you are using a text field.What did't used a picklist data type.If you used picklist data type it is simple getting label values through apex and scontrols(Regarding scontrols check the documentation still i also have doughty it is compiling or not)

MikeGinouMikeGinou

It's absolutely possible using an actionSupport control.

 

 

<apex:selectList ...>
  <apex:actionSupport event="onChange" action={!myaction} rerender="fldDescription" />
</apex:selectList>

<apex:outputField id="fldDescription" value="Account.Description" />

 

 

Here I'm using a selectList (I didn't specify the exact attributes of the selectList, but they are largely irrelevant), but it should also work fine as an outputField attached to a picklist field as well.

 

Note that the rerender attribute on the actionSupport is vital as without a rerender attribute, a full page rerender occurs (actually the action method returns a null PageReference, which amounts to the same thing).

 

The controller would need to have the referenced action method:

 

public void myAction () {
  //Do Something clever....
  //Probably something affecting Account.Description
  //Which can be make accessible via a local member variable
}

 

-Mike

 

kminevkminev

Thanks for everyones' input.

 

I will try to re-phrase my question I am not sure if people understood my question.

 

I need to have a picklist with certain values for example: Sample 1, Sample 2, Sample 3, when a selection is made on this picklist each value corresponds to a text string for exampkle Sample 1 = This is sample template text, Sample 2 = This is another sample text.

 

I have a field on the same page that need to be updated with the sample text depending on the picklist value.

 

Thanks once again.

gv007gv007

After getting the text just update the  field based on condition.

TehNrdTehNrd

I understand the question. I am asking where you are storing the sample text so I can give you a full example.

kminevkminev

Your help is very much appreciated.

 

The text could be stored/hard coded to a variable for now.

 

Thank you.

TehNrdTehNrd

I would do something like this. I just whipped this up in notepad real quick so there may be some typos.

 

 

public class MyController{

	public String selectedValue {get; set;}
	public String inputField {get; set;}
		
	public void changeValue(){
		inputField = selectedValue;
	}

}


<apex:page controller="MyController">
    <apex:form>
        <apex:selectList size="1">
            <apex:actionSupport onchange="{!changeValue}" rerender="input"/>
            <apex:selectionOption itemLabel="--None--" itemValue=""/>
            <apex:selectionOption itemLabel="1" itemValue="#1 was selected"/>
            <apex:selectionOption itemLabel="2" itemValue="#2 was selected"/>
            <apex:selectionOption itemLabel="3" itemValue="#3 was selected"/>
        </apex:selectList><br/>
        <apex:inputText value="{!inputText}" id="input"/>
    </apex:form>
</apex:page>

 

 

This was selected as the best answer