+ Start a Discussion
Brijesh_85Brijesh_85 

Fill Pick List Dynamically

Hi All,

 

I want to fill the data in picklist by using query or want to fill the data from custom objects. how do I do??

And also in My Application there are two picklists. so based on the first selection I want to fill the second Picklist.

I know about "Field Dependency" option.But that is again Static. I want to fill it dynamically.

 

I want to do it By AJAX. Is it possible??

 

 

Thanks in Advance,

 Brijesh Thakur 

Best Answer chosen by Admin (Salesforce Developers) 
SteveBowerSteveBower

I don't think my particular code is going to help you.  This is a snippet from a big chunk of code that allows the user to select a product from a picklist and based on that, populates a variable number of other picklists representing sets of components which the user can choose from.  Default prices are displayed and the user can override the prices as well.  The variable picklists are pre-built and cached so they can just be substituted in.  It's somewhat specific to their needs with some other background processing. I also don't know if the client I did it for would be comfortable with my posting it.

 

The code earlier is just the simple select of the top level picklist.

 

In this case, you need to write a getItems routine that returns a list, have a String called "SelectedItem" with get and set defined.  You want to write onChange routine that does something with your other picklist selection option array and then returns a (possibly null) pageReference.

 

Don't know if this helps.  I've found the documentation to be very light on examples, but good on concepts.  Best, Steve.

All Answers

werewolfwerewolf
Instead of a picklist, why don't you create a custom lookup relationship to your custom object?  That's what those are for.
Brijesh_85Brijesh_85

Hi,

 

True, But based on the requirement I have to use the Picklist. And also..based on the first selection I have to fill second picklist.

 

Please Guide me,

 

 

 

Thanks,

Brijesh Thakur

werewolfwerewolf
No, you can't really do that.  I'd strongly recommend that you use a lookup relationship instead.
Greg HGreg H

By "AJAX" I presume you mean using the salesforce.com AJAX toollkit? If so, here is a sample that should get you on your way...

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Dynamic Picklist Select</title> <!-- Created by: Greg Hacic Last Update: 11 March 2009 Questions?: http://www.InteractiveTies.com/ NOTES: This sControl will dynamically build any picklist as long as the salesforce.com field is a single or multi select picklist. To test this functionality for other picklists simply change the selTable value to the Object you want and change the selField value to a picklist field on the table listed in selTable. --> <link href="/sCSS/15.0/Theme2/default/common.css" type="text/css" media="handheld,print,projection,screen,tty,tv" rel="stylesheet"> <link href="/sCSS/15.0/Theme2/allCustom.css" type="text/css" media="handheld,print,projection,screen,tty,tv" rel="stylesheet"> <script src="/soap/ajax/15.0/connection.js" type="text/javascript"></script> <script language="JavaScript" type="text/javascript"> <!-- //intiates page and loads data function pageInit() { var selTable = "User"; var selField = "TimeZoneSidKey"; var picklistValues = getPickVals(selTable,selField); var plHTML = "<select id=\"picklistSelect\"><option value=\"\" selected>All</option>"; for (var a=0; a<picklistValues.length; a++) { plHTML += "<option value=\""+picklistValues[a]+"\">"+picklistValues[a]+"</option>"; } plHTML += "</select>"; document.getElementById("plVals").innerHTML = plHTML; document.getElementById("fieldName").innerHTML = "<span class=\"requiredMark\">*</span>"+selField; document.getElementById("fieldText").innerHTML = "Field: "+selField+"<span class=\"titleSeparatingColon\">:</span>"; document.getElementById("objectText").innerHTML = "Object: "+selTable; } //returns an array of picklist values for the passed table and field function getPickVals(table,field) { var returnArray = new Array(); //return array holding all values var oTable = sforce.connection.describeSObjects([table]); //describe call for object var oFields = oTable[0].fields; //look at fields for object for (var a=0; a<oFields.length; a++) { //loop through all the fields if (oFields[a].name == field) { //if the field is the requested one var oPicklistVals = oFields[a].picklistValues; //look at all the picklist values for field for (var b=0; b<oPicklistVals.length; b++) { //loop through all picklist values returnArray.push(oPicklistVals[b].value); //push all picklist options to return array } break; //we found field and picklist values so exit loop } } return returnArray; //send back data } //--> </script> </head> <body class="Custom184Tab detailPage" onLoad="pageInit()"> <table border="0" cellspacing="0" cellpadding="0" width="100%"> <tr> <td> <div class="bPageTitle"> <div class="ptBody secondaryPalette"><div class="content"><img src="/servlet/servlet.ImageServer?id=0153000000084iV&oid=00D300000001L5c&lastMod=1179974461000" alt="Home" class="pageTitleIcon userDefinedImage"><h1 class="pageType">iTies Examples<span class="titleSeparatingColon">:</span></h1><h2 class="pageDescription"> Dynamic Picklist</h2><div class="blank">&nbsp;</div></div><div class="links">&nbsp;</div></div> </div> <div class="bPageBlock bEditBlock secondaryPalette"><div class="pbHeader"><table border="0" cellpadding="0" cellspacing="0"><tr><td class="pbTitle"><img src="/s.gif" alt="" width="1" height="1" class="minWidth" title=""><h2 class="mainTitle" id="objectText">&nbsp;</h2></td><td class="pbButton" id="buttonsTop">&nbsp;</td></tr></table></div> <div class="pbBody"> <div class="pbSubheader first tertiaryPalette"><h3 id="fieldText">Information<span class="titleSeparatingColon">:</span></h3></div> <div class="pbSubsection"> <table class="detailList" border="0" cellpadding="0" cellspacing="0"> <tr> <td class="labelCol last requiredInput"><label for="field" id="fieldName"><span class="requiredMark">*</span>Loading...</label></td> <td class="data2Col last" colspan="3"><div class="requiredInput"><div class="requiredBlock"></div><div id="plVals" name="plVals"></div></div></td> </tr> <tr> <td class="labelCol last empty">&nbsp;</td> <td class="dataCol col02 last empty">&nbsp;</td> <td class="labelCol last empty">&nbsp;</td> <td class="dataCol last empty">&nbsp;</td> </tr> </table> </div> </div> <div class="pbBottomButtons"><table border="0" cellpadding="0" cellspacing="0"><tr><td class="pbTitle"><img src="/s.gif" alt="" width="1" height="1" class="minWidth" title="">&nbsp;</td><td class="pbButtonb" id="buttonsBottom">&nbsp;</td></tr></table></div> <div class="pbFooter secondaryPalette"><div class="bg"></div></div> </div> </td> </tr> </table> </body> </html>

 

It doesn't do the dynamic population of a second picklist but from this you should be able to put something together. Let me know if you have questions,

-greg

 

werewolfwerewolf
Yes, but that's in an Scontrol that won't get run on the edit page -- you can only run it separately or embedded on the detail page.  My understanding of the question was that he wants to do it inline on the edit page.
werewolfwerewolf

Also:

 

If you were going to do this in a component like that which Greg submitted, you'd be much better served doing it in Visualforce than in an Scontrol.

Greg HGreg H

I agree 100% with Werewolf's comments. But I was unsure of the context of the original question.

-greg

Brijesh_85Brijesh_85

Hi All,

 

Yes , I am looking for AJAX technology supported by Salesforce.com or not.

 

And why I am pushing to use LookUp . Honestly speaking I dont like it.

 

I am repeating My Question :- I want to fill PickList dynamically. The data for the Picklist should come from Custom Object.

 

My Feeling is that I should write a query and store it in one List using Apex. On the visualsorce page I can get it by "<Apex:page extentions=" "> option &  can iterate it to get the value of that Sobject.

 

In J2ee on JSP page  I can do it by <Option Collection> Tag. How do I do in Salesforce.com

 

Please answer the above question. 

 

Thanks,

Brijesh Thakur

 

Message Edited by Brijesh_85 on 03-14-2009 06:42 AM
Brijesh_85Brijesh_85

Hi all,

 I am able to populate the PickList dynamically by using Apex + <apex:selectOptions> tag.

 

 Below is the solution.

 

Apex Code :- 

 

StudentExtention.cls

 

public List<selectOption> StudentNameOptions {get {
        List<selectOption> studentName =new List<selectOption>();
         studentName.add(new selectOption('None', 'None'));
        for (Students__c st :
            [select Name,Student_Name__c
             from Students__c])
             studentName.add(new selectOption(st.Name, st.Student_Name__c));
        return studentName;
}
private set;}

 

Visualforce Code :-

 

<apex: page standardController="Students__c"  extensions="StudentExtension">

     <apex:selectList value="id" size="1">
                    <apex:selectOptions Value="{!
StudentNameOptions}"></apex:selectOptions>
      </apex:selectList>

</apex: page>

 

Now I am looking for  Ajax call on Picklist's onChange event.so that based on that I can fill another Picklist.

 

Please help me to solve the Above problem.

 

Thanks,

Brijesh Thakur

Message Edited by Brijesh_85 on 03-14-2009 06:58 AM
SteveBowerSteveBower

<apex:pageBlockSection title="Specification" columns="2" id="theSection"> <apex:inputField value="{!Environment_Line_Item__c.Server_Name__c}" id="srv"/> <apex:actionStatus id="theStatus" startText="(Refreshing...)" stopText="" /> <apex:pageBlockSectionItem > <apex:outputLabel value="Item" for="sel1"/> <apex:actionRegion id="region1"> <apex:selectList size="1" value="{!SelectedItem}" required="true" multiselect="false" id="sel1"> <apex:actionSupport event="onchange" immediate="false" rerender="theForm" action="{!onchange}" status="theStatus"/> <apex:selectOptions value="{!Items}"/> </apex:selectList> </apex:actionRegion> </apex:pageBlockSectionItem> </apex:pageBlockSection>

 

Is this what you're looking for?  -Steve

Brijesh_85Brijesh_85

Hi,

 

Thanks...

 

Yes I am looking for the same..

 

Can you please elaborate more ?? means can you please post Apex code also ?

 

 

Brijesh Thakur

 

SteveBowerSteveBower

I don't think my particular code is going to help you.  This is a snippet from a big chunk of code that allows the user to select a product from a picklist and based on that, populates a variable number of other picklists representing sets of components which the user can choose from.  Default prices are displayed and the user can override the prices as well.  The variable picklists are pre-built and cached so they can just be substituted in.  It's somewhat specific to their needs with some other background processing. I also don't know if the client I did it for would be comfortable with my posting it.

 

The code earlier is just the simple select of the top level picklist.

 

In this case, you need to write a getItems routine that returns a list, have a String called "SelectedItem" with get and set defined.  You want to write onChange routine that does something with your other picklist selection option array and then returns a (possibly null) pageReference.

 

Don't know if this helps.  I've found the documentation to be very light on examples, but good on concepts.  Best, Steve.

This was selected as the best answer
RozRoz

Hi there.

 

I understand how to do this using Visualforce. :)

 

However, for a change control app, I'm trying to populate a picklist with the list of custom objects for my org, dynamically.

 

Is it possible?  Is there an all-encompassing object I can query to obtain my list of custom objects? or do I have to just include every custom object in the SOQL SELECT statement?

 

Thanks.

 

Roz

jpwagnerjpwagner
(external) meta-data api integration?