+ Start a Discussion
Wayne Solomon 2016Wayne Solomon 2016 

How to create visualforce "dependent picklist" on a master-detail relationship

Hi, I have a master-detail relationsip between a site and a venue field.  With my current code below the page preloads the picklists with these values.
User-added image

How to I get the ID of the selected site object so that I can populate the venues related to that particular site? 

Markup*
                    <apex:selectList required="true" label="Site" value="{!selectedSite}" multiselect="false" size="1" >
                        <apex:selectOptions value="{!SitesOptions}"></apex:selectOptions>
                    </apex:selectList>
                    
                    <apex:selectList required="true" label="Venue" value="{!selectedVenue}" multiselect="false" size="1" id="myVenues">
                        <apex:selectOptions value="{!VenueOptions}" ></apex:selectOptions>
                    </apex:selectList>

ControllerCode*
    public User currentUser = [select id, name, business_unit__c from user where id =: userInfo.getUserId()];
    public String currentUserBusinessUnit = currentUser.Business_Unit__c;
    public String selectedSite{get;set;}
    public String selectedVenue{get;set;}
    
    public List<selectOption> getSitesOptions(){
        List<selectOption> siteOptions = new List<selectOption>();
        List<Site__c> siteList = new List<Site__c>();
        siteList = [SELECT Id,Name FROM Site__c WHERE business_unit__c =: currentUserBusinessUnit];
        
        for(Site__c s : siteList){
            siteOptions.add(new selectOption(s.Id,s.Name) );
        }
        return siteOptions;
    }
    
    public List<selectOption> getVenueOptions(){
        List<selectOption> venueOptions = new List<selectOption>();
        List<Venue__c> venueList = new List<Venue__c>();
        venueList = [SELECT Id,Name FROM Venue__c];
        
        for(Venue__c v : venueList){
            venueOptions.add(new selectOption(v.Id,v.Name) );
        }
        return venueOptions;
    }
    
 
Best Answer chosen by Wayne Solomon 2016
Wayne Solomon 2016Wayne Solomon 2016
Hi, I made that my selectOption stores the IDm which solved the issue

All Answers

NagendraNagendra (Salesforce Developers) 
Hi Wayne,

There is literally no API for this at all. You cannot query the ID via metadata API, SOAP API, Rest API, Apex Code describe methods, etc. You have to actually log in to the application and take a look at the URL, as you've just pointed out. If you're trying to build a PageReference to a standard page that includes the field's values (which is the only reason I know of why anyone would want this information), you're going to have to work for it.

The only method that I can see of doing this programmatically is to perform screen-scraping (e.g. PageReference /ui/setup/Setup, getContent(), parse the links to find the correct link for the object, then PageReference the object's detail or field pages, and getContent(), then parse that page).

The only way I can see perhaps is setting a PageReference to the custom object page and try to get it from the page source or something.

Please mark this as solved if it helps.

Best Regards,
Nagendra.P
Wayne Solomon 2016Wayne Solomon 2016
Hi, I made that my selectOption stores the IDm which solved the issue
This was selected as the best answer