ads

Thursday, 18 August 2016

Junction Object Creation and Dependent Picklist Using Junction Object



Step 1 :- Create First object  with name "Teacher" and Add some Records







Step 2 :- Create Second object  with name "Subject" and Add some Records










Step 3:- Create Third object with name "Junction"  and have master- detail relationship with Two                     above objects and  then Add some Records








Step 4 :- Making Dependent Picklist 


Controller code :-


public class junction3 {

    public string searchteach{get; set;}
    public string searchsubject{get; set;}
    
    public list<selectoption> gettechlist(){
        list<selectoption> selectteacher= new list<selectoption>();
        list<teacher__c> techlist1= [select name from teacher__c];
        for(teacher__c e:techlist1){
            selectteacher.add(new selectoption(e.name,e.name));
        }
        return selectteacher;
    }
    
    public list<selectoption> getsublist(){
        list<selectoption> selectsubject= new list<selectoption>();
        list<junction__c> junlist1= [select name, teacher__r.name,subject__r.name  from junction__c where teacher__r.name=:searchteach];
        for(junction__c e:junlist1){
            selectsubject.add(new selectoption(e.subject__r.name,e.subject__r.name));
        }
        return selectsubject;
    }
    
    
    
    
    
    public list<selectoption> getsublist1(){
        list<selectoption> selectteacher1= new list<selectoption>();
        list<subject__c> subjlist1= [select name from subject__c];
        for(subject__c e:subjlist1){
            selectteacher1.add(new selectoption(e.name,e.name));
        }
        return selectteacher1;
    }
    
    public list<selectoption> gettechlist1(){
        list<selectoption> selectsubject2= new list<selectoption>();
        list<junction__c> junlist2= [select name, teacher__r.name,subject__r.name  from junction__c where subject__r.name=:searchsubject];
        for(junction__c e:junlist2){
            selectsubject2.add(new selectoption(e.teacher__r.name,e.teacher__r.name));
        }
        return selectsubject2;
    }
    
    
}




Visualforce Page :-


<apex:page controller="junction3">
    <apex:form >
        <apex:pageBlock title="Teacher to Subject Objects" mode="edit">
            <apex:pageBlockSection title="It's a simple Teacher to Subject relationship...">
                <apex:outputPanel style="float:middle">
                    <apex:selectList value="{!searchteach}" size="1">
                        <apex:selectOptions value="{!techlist}"/>
                        <apex:actionSupport event="onchange" reRender="refresh"/>
                    </apex:selectList> 
                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <apex:selectList id="refresh" size="1">
                        <apex:selectOptions value="{!sublist}"/>
                    </apex:selectList> 
                </apex:outputPanel>
            </apex:pageBlockSection>
        </apex:pageBlock>
        
        <apex:pageBlock title="Subject to Teacher Junction Objects" mode="edit">
            <apex:pageBlockSection title="It's a simple Subject to Teacher relationship...">
                <apex:outputPanel style="float:middle">
                    <apex:selectList value="{!searchsubject}" size="1">
                        <apex:selectOptions value="{!sublist1}"/>
                        <apex:actionSupport event="onchange" reRender="refreshed"/>
                    </apex:selectList> 
                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <apex:selectList id="refreshed" size="1">
                        <apex:selectOptions value="{!techlist1}"/>
                    </apex:selectList> 
                </apex:outputPanel>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Output Screen 1  :-


Output Screen 2  :-


No comments:

Post a Comment