0

I am using two custom object Parent (Part )and Child (Material) in visualforce page now my requirement is that i want to get Material Name in one picklist and also want to show its related machine name (which is a field on other custom object) in another picklist. i am using below code but its not working please help me to fix out this issue.

Here is my code

Visualforce page:

Material Names

     <apex:selectList value="{!selectedPartId}" size="1">                                 
        <apex:selectOptions value="{!MaterialNames}"/>
        <apex:actionSupport event="onchange" reRender="a"/>
     </apex:selectList>

Machine Names

         <apex:selectList value="{!selectedMaterialId}" size="1"  id="a">
            <apex:selectOptions value="{!MachineNames}"/>
            <apex:actionSupport event="onchange" reRender="b"/> 
         </apex:selectList>

Apex class:

           public List<SelectOption> getMaterialNames() {
                  List<SelectOption> materialOptions= new List<SelectOption>();
                  materialOptions.add( new SelectOption('','--Select--'));
                  for( Part__c pc : [select Id,name,Material__c from Part__c ] ) {
                          materialOptions.add( new SelectOption(part.id,part.Material__c));
                  }
                 return materialOptions;
           }

           public List<SelectOption> getMachineNames() {
                  System.debug('Entered ContactNames account id...........'+selectedPartId );
                  List<SelectOption> machineOptions= new List<SelectOption>();
                  List<SelectOption> options = new List<SelectOption>();
                    if(selectedPartId != null)
                    {     
                       for( Machine__c machine : [select Id,name from Machine__c where id=:selectedPartId ] ) {
                          machineOptions.add( new SelectOption(machine.Id,machine.name));
                       }
                    }                  
                    else
                    {
                        machineOptions.add( new SelectOption('--None--','--None--'));
                    }
                 return machineOptions;
           } 
  • Don't you have any action on your <apex:actionSupport> ? – highfive Sep 22 '15 at 6:45
  • yes i am using action support there but there is some problem in soql query not able to fitech material name and its related machine name. – user24524 Sep 22 '15 at 7:01
0

I think you need following corrections. First in first for loop replace part.id,part.Material__c with pc.id,pc.Material__c.

Your code

  for( Part__c pc : [select Id,name,Material__c from Part__c ] ) {
          materialOptions.add( new SelectOption(part.id,part.Material__c));
  }

Should be

  for( Part__c pc : [select Id,name,Material__c from Part__c ] ) {
          materialOptions.add( new SelectOption(pc.id,pc.Material__c));
  }

Second correction is -- In the below query I think you have to user partid in where clause. something like select Id,name from Machine__c where partid__c=:selectedPartId depending upon your field name.

   for( Machine__c machine : [select Id,name from Machine__c where id=:selectedPartId ] ) {
      machineOptions.add( new SelectOption(machine.Id,machine.name));
   }
0

Your query on machine should filter for Part instead of Id

for( Machine__c machine : [select Id,name from Machine__c where PartId=:selectedPartId ] ) {
0

I think that you have a problem in the rerender engine. Try this

<apex:selectList value="{!selectedPartId}" size="1">                                 
        <apex:selectOptions value="{!MaterialNames}"/>
        <apex:actionSupport event="onchange" reRender="materials"/>
     </apex:selectList>


<apex:outputPanel id="materials" layout="block">
    <apex:selectList value="{!selectedMaterialId}" size="1">
            <apex:selectOptions value="{!MachineNames}"/>
            <apex:actionSupport event="onchange" reRender="nexPanel"/> 
         </apex:selectList>
</apex:outputPanel>
0

My Updated code:

public List<SelectOption> getMaterialNames() {
                      List<SelectOption> materialOptions= new List<SelectOption>();
                      materialOptions.add( new SelectOption('','--Select--'));
                      for( Part__c pc : [select Id,name,Material__c from Part__c ] ) {
                              materialOptions.add( new SelectOption(pc.Id,pc.name));

                      }
                     return materialOptions;
                    // System.debug('value of Material__c'+Material__c);
                    // System.debug('value of pc'+pc);
               }

               public List<SelectOption> getMachineNames() {
                      System.debug('Entered ContactNames account id...........'+selectedPartId );
                      List<SelectOption> machineOptions= new List<SelectOption>();
                      List<SelectOption> options = new List<SelectOption>();
                        if(selectedPartId != null)
                        {     
                           for( Machine__c machine : [select Id,name,Part__c from Machine__c where Id=:selectedPartId ] ) {
                              machineOptions.add( new SelectOption(machine.Id,machine.name));
                             // System.debug('value of machine'+machine);
                           }
                        }                  
                        else
                        {
                            machineOptions.add( new SelectOption('--None--','--None--'));
                        }
                     return machineOptions;
               }

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.