0

I am working creating a vf page, that has a selectlist showing list of Sobjects. Once anyone sobject is selected it has to display the fields in another selectlist. I tried but I am unable to get the expected output. Dont know how to fix it. Kindly help.

Controller:

public class Workbenchlike {
public string selectedobj {get; set;}
public map<string, schema.sobjectType> selectobjs {get; set;}
public map<string, schema.sobjectField> fieldnam {get; set;}

 public workbenchlike(){
    selectobjs = schema.getGlobalDescribe();
    system.debug('selectedobject' +selectedobj);
 }

public pagereference getfieldnames(){
  schema.sobjecttype obj = schema.getglobaldescribe().get(selectedobj); 
  fieldnam = obj.getDescribe().fields.getMap(); 
  return null;
 }
}

Vf page:

<apex:page controller="Workbenchlike" >
<style>
 td{
     text-align:center;
 }
</style>
<apex:form >

<div style="text-align:center;font-size:20px">

    Select from the list  :-
<apex:outputPanel >
  <apex:selectList size="1" value="{!selectedobj}" >
  <apex:selectOptions value="{!selectobjs}"/>
      <apex:actionSupport event="onchange" action="{!getfieldnames}" reRender="table"/>

   </apex:selectList>
</apex:outputPanel>

</div>
   <div style="text-align:center; font-size:15px">
    <table id="table" >
    <tr> 
       <td>
           FieldNames:<br/>
         <apex:selectList size="4" value="{!fieldnam}" ></apex:selectList>
        </td>
           <td>
              Selected Values:<br/>
             <apex:selectList ></apex:selectList>
           </td>
       </tr>
 </table>
 </div>
  </apex:form>
 </apex:page>
1

I finally did it! thought of sharing this so that it would be helpful for someone!

VF page:

    <style>
        td{
        text-align:center;
        }
    </style>
    <apex:form >
<apex:outputPanel id="error">
        <apex:messages />
</apex:outputPanel>
        <apex:pageBlock >

            <apex:pageBlockSection columns="1" collapsible="false">
                <apex:pageBlockSectionItem >
                     <apex:outputLabel value="Select from the list  :-" />
                    <apex:selectList size="1" value="{!strObjectName}" multiselect="false" >
                        <apex:selectOptions value="{!lstObjNames}"/>
                        <apex:actionSupport action="{!retriveFieldNames}" event="onchange" reRender="filedname,error"/>  
                    </apex:selectList>
                </apex:pageBlockSectionItem>

            </apex:pageBlockSection>
        </apex:pageBlock>
        <apex:pageBlock >
        <apex:outputPanel id="filedname">
            <div style="text-align:center; font-size:15px"  >
                <table>
                    <tr> 
                        <td>
                           <b>FieldNames:</b><br/>
                            <apex:selectList size="6" value="{!getfields}" multiselect="true" id="getfie" >
                            <apex:selectOptions value="{!lstfieldNms}" />
                            <apex:actionSupport event="onchange" action="{!getquery}" reRender="values,button"/>
                            </apex:selectList>
                        </td>
                        <td >
                            <b>Selected Values:</b><br/>
                            <apex:selectList size="6" value="{!selval}" id="values"  >
                            <apex:selectOptions value="{!selvalues}"></apex:selectOptions>
                            </apex:selectList>
                        </td>
                        <td>
                        <apex:outputPanel id="button">
                        <apex:commandButton value="clear selected" action="{!clearsel}"  reRender="values,button" rendered="{!test}"/>
                        <apex:commandButton value="Query" action="{!query}" rendered="{!test}" reRender="input" />
                        </apex:outputPanel>
                        </td>
                    </tr>
                </table>
                <apex:outputPanel id="input">

                <apex:inputTextarea value="{!inputrec}" style="font-size:20px; height:100px; width:500px" />

                </apex:outputPanel>

            </div>
        </apex:outputPanel>
        </apex:pageBlock>
    </apex:form>
</apex:page>
[![The output would look like this][1]][1]
**And the Controller**

public class Workbenchlikes {
    public string strObjectName {get;set;}
    public list<SelectOption> lstObjNames {get; set;}
    public list<selectOption> lstfieldNms {get; set;}
    public list<string> getfields {get; set;} 
    public string selval {get; set;}
    public list<selectOption> selvalues {get; set;}
    public boolean test {get; set;}
    public string inputrec {get; set;}
    public workbenchlikes(){
        retriveObjectNames();
    }

    public void retriveObjectNames() {
        lstObjNames = new list<SelectOption>();
        lstObjNames.add(new selectOption('==Select One==', '==Select One==', false));
        map<string, schema.sobjectType> selectobjs = schema.getGlobalDescribe();
        for ( schema.sobjecttype objIterator : selectobjs.values()) {
            Schema.DescribeSObjectResult objResult = objIterator.getDescribe();
            lstObjNames.add(new SelectOption(objResult.getname(), objResult.getlabel()));
        }
               system.debug('lstObjNames ===>  '+lstObjNames);
    }

    public void retriveFieldNames(){
    lstfieldNms = new list<selectOption>();

        system.debug('strObjectName ======>  ' +strObjectName);


        schema.sobjecttype obj = schema.getglobaldescribe().get(strObjectName);
        if(obj !=null){
        Map<string, schema.sobjectField> objfield = obj.getdescribe().fields.getMap();
        for(schema.sobjectfield fiIterator : objfield.values()){
        schema.describefieldresult fieldres =fiIterator.getDescribe();
        lstfieldNms.add(new selectoption(fieldres.getname(), fieldres.getname()));
        }    
        }else{
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please select An Object'));
        }

    }
    public void clearsel(){
    if(selvalues != null){

     selvalues.clear();
     test = false;
     }
    }
    public void getquery(){
    selvalues = new list<selectOption>();
    if(getfields.size() == 0){
    selvalues.clear();
    }else{
    test = true;
    system.debug('test'+test);
    for(integer i=0; i< getfields.size(); i++){
selvalues .add(new selectOption(getfields[i], getfields[i]));
    }
    }
    }
    public void query(){
    map<integer,string> strl = new map<integer,string>();
    for(integer i=0 ; i<getfields.size(); i++){
    strl.put(i,getfields[i]);
    }
    system.debug('strl===>' +strl);
    inputrec = 'select '+string.join(strl.values(),',')+' from ' +strObjectName;
    }
    }```


  [1]: https://i.stack.imgur.com/OquiZ.png
| improve this answer | |
0

give an Id to you another selectlist (fieldnam) and reRender that onchange of your first selectList.

To Add .. Look at the following code ..

<apex:page controller="YourCustomCntrller"> <apex:form > <apex:pageBlock> <apex:pageBlockSection columns="2"> <apex:pageblockSectionItem> <apex:outputLabel value="Object Names"/> </apex:pageblockSectionItem>
<apex:pageblockSectionItem>
<apex:selectList size="1" value="{!selectedobj}"> <apex:selectOptions value="{!selectobjs}"/> <apex:actionSupport event="onchange" reRender="shippingOption"/> </apex:selectList>
</apex:pageblockSectionItem> <apex:pageblockSectionItem> <apex:outputLabel value="Field Names"/> </apex:pageblockSectionItem>
<apex:pageblockSectionItem> <apex:selectList size="1" value="{!fieldname}" id="shippingOption"> <apex:selectOptions value="{!fieldnames}"/> </apex:selectList> </apex:pageblockSectionItem>
</apex:pageBlockSection>
</apex:pageBlock> </apex:form>

Now form Your code....

Call your apex method on render of SelectList not from actionSupport, if you have written a getter method, be careful not use get with your method name in VF page...

As your apex menthod name is getfieldnames, you have to call fieldnames in your VF page.

Hope this will work for you.

| improve this answer | |
  • Hi Avinash, It didn't work. The method itself is not called from action support. I put system.debug inside the getfieldnames() method and found that the method itself not called for onchange event. – S.Sujendran Feb 20 '19 at 11:16
  • Please check the edits, hope that will help @S.Sujendran – Avinash Kumar Feb 20 '19 at 11:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.