You need to sign in to do that
Don't have an account?
noor Zabi
Create a Common VF page to insert data to the Student and Teacher Object---Mark all fields as mandatory at page level (not in object).
VIsual Force Page:-
<apex:page controller="StudentController">
<apex:form id="test" >
<apex:pageMessages/>
<apex:pageBlock title="Choose the Object To Create Records:" >
<center>
<apex:selectList value="{!source}" size="1" required="true" >
<apex:selectOptions value="{!sourceValue}" />
<apex:actionSupport event="onchange" action="{!objectDisplay}" reRender="test"/>
</apex:selectList>
</center>
</apex:pageBlock>
<apex:pageBlock rendered="{!flag}">
<apex:pageBlockSection title="Create Student Details" columns="1" >
<apex:inputField value="{!stu1.Salutation__c}" required="true"/>
<apex:inputField value="{!stu1.Name}" required="true"/>
<apex:inputField value="{!stu1.Last_Name__c}" required="true"/>
<apex:inputField value="{!stu1.Gender__c}" required="true"/>
<apex:inputField value="{!stu1.DOB__c}" required="true"/>
<apex:inputField value="{!stu1.Class__c}" required="true"/>
<apex:inputField value="{!stu1.Division__c}" required="true"/>
</apex:pageBlockSection>
</apex:pageBlock>
<apex:pageBlock rendered="{!flag1}">
<apex:pageBlockSection title="Create Teacher Details" columns="2" >
<apex:inputField value="{!tch1.Salutation__c}" required="true" />
<apex:inputField value="{!tch1.Name}" required="true"/>
<apex:inputField value="{!tch1.Last_Name__c}" required="true"/>
<apex:inputField value="{!tch1.Gender__c}" required="true"/>
<apex:inputField value="{!tch1.DOB__c}" required="true"/>
<apex:inputField value="{!tch1.Class__c}" required="true"/>
<apex:inputField value="{!tch1.Division__c}" required="true" />
</apex:pageBlockSection>
</apex:pageBlock>
<apex:pageBlock rendered="{!flag2}">
<apex:pageBlockSection>
<apex:commandButton value="Save" action="{!SaveRecord}" />
<apex:commandButton value="Clear" action="{!clearFunction}" />
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
Class:--
public with sharing class StudentController {
public string source {get;set;}
public List<selectOption> sourceValue {get;set;}
public boolean flag {get;set;}
public boolean flag1 {get;set;}
public boolean flag2 {get;set;}
public Students__c stu1{get;set;}
public Teacher__c tch1{get;set;}
public StudentController()
{
flag = false;
flag1 = false;
flag2 = false;
stu1 = new Students__c();
tch1 = new Teacher__c();
sourceValue = new List<selectOption>();
sourceValue.add(new selectOption('','--select--'));
sourceValue.add(new selectOption('Students','Students'));
sourceValue.add(new selectOption('Teacher','Teacher'));
}
public void objectDisplay()
{
If(source == 'Students'){
flag = true;
flag2= true;
flag1 = false;
}
else if(source == 'Teacher'){
flag1 = true;
flag2= true;
flag = false;
}
}
public void saveRecord() {
if(source=='Students') {
insert stu1;
ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM,' Record Created Successfully.Thank you!'));
stu1 = new Students__c();
}
else {
insert tch1;
ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM,' Record Created Successfully.Thank you!'));
tch1 = new Teacher__c();
}
}
public void clearFunction(){
if(source=='Students') {
stu1 = new Students__c();
}
else{
tch1 = new Teacher__c();
}
}
}
When i use required = true in input fields it is working, but i change the select option from student to teacher or teacher to student. I'm getting this error
<apex:page controller="StudentController">
<apex:form id="test" >
<apex:pageMessages/>
<apex:pageBlock title="Choose the Object To Create Records:" >
<center>
<apex:selectList value="{!source}" size="1" required="true" >
<apex:selectOptions value="{!sourceValue}" />
<apex:actionSupport event="onchange" action="{!objectDisplay}" reRender="test"/>
</apex:selectList>
</center>
</apex:pageBlock>
<apex:pageBlock rendered="{!flag}">
<apex:pageBlockSection title="Create Student Details" columns="1" >
<apex:inputField value="{!stu1.Salutation__c}" required="true"/>
<apex:inputField value="{!stu1.Name}" required="true"/>
<apex:inputField value="{!stu1.Last_Name__c}" required="true"/>
<apex:inputField value="{!stu1.Gender__c}" required="true"/>
<apex:inputField value="{!stu1.DOB__c}" required="true"/>
<apex:inputField value="{!stu1.Class__c}" required="true"/>
<apex:inputField value="{!stu1.Division__c}" required="true"/>
</apex:pageBlockSection>
</apex:pageBlock>
<apex:pageBlock rendered="{!flag1}">
<apex:pageBlockSection title="Create Teacher Details" columns="2" >
<apex:inputField value="{!tch1.Salutation__c}" required="true" />
<apex:inputField value="{!tch1.Name}" required="true"/>
<apex:inputField value="{!tch1.Last_Name__c}" required="true"/>
<apex:inputField value="{!tch1.Gender__c}" required="true"/>
<apex:inputField value="{!tch1.DOB__c}" required="true"/>
<apex:inputField value="{!tch1.Class__c}" required="true"/>
<apex:inputField value="{!tch1.Division__c}" required="true" />
</apex:pageBlockSection>
</apex:pageBlock>
<apex:pageBlock rendered="{!flag2}">
<apex:pageBlockSection>
<apex:commandButton value="Save" action="{!SaveRecord}" />
<apex:commandButton value="Clear" action="{!clearFunction}" />
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
Class:--
public with sharing class StudentController {
public string source {get;set;}
public List<selectOption> sourceValue {get;set;}
public boolean flag {get;set;}
public boolean flag1 {get;set;}
public boolean flag2 {get;set;}
public Students__c stu1{get;set;}
public Teacher__c tch1{get;set;}
public StudentController()
{
flag = false;
flag1 = false;
flag2 = false;
stu1 = new Students__c();
tch1 = new Teacher__c();
sourceValue = new List<selectOption>();
sourceValue.add(new selectOption('','--select--'));
sourceValue.add(new selectOption('Students','Students'));
sourceValue.add(new selectOption('Teacher','Teacher'));
}
public void objectDisplay()
{
If(source == 'Students'){
flag = true;
flag2= true;
flag1 = false;
}
else if(source == 'Teacher'){
flag1 = true;
flag2= true;
flag = false;
}
}
public void saveRecord() {
if(source=='Students') {
insert stu1;
ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM,' Record Created Successfully.Thank you!'));
stu1 = new Students__c();
}
else {
insert tch1;
ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM,' Record Created Successfully.Thank you!'));
tch1 = new Teacher__c();
}
}
public void clearFunction(){
if(source=='Students') {
stu1 = new Students__c();
}
else{
tch1 = new Teacher__c();
}
}
}
When i use required = true in input fields it is working, but i change the select option from student to teacher or teacher to student. I'm getting this error
Use actionRegion for this.
Thanks!