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;
}
Best Answer
I think you need following corrections. First in first for loop replace
part.id,part.Material__c
withpc.id,pc.Material__c
.Your code
Should be
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.