Hai Friends,
I Came with new example that is generating Vfpage dynamically and saving data in to object.
Follow the steps,
1. Create Custom settings----> Dynamic Page Controll.
Create Two fields in custom settings 1. ObjectName 2. Field Name.
2. Enter Records in to Custom settings ,
Object Name Field Name
Account Name
Account Email
3. Copy the following class.
======================================================================
public with sharing class DynamicPageController {
public map<string,sobject> objmap { get; set; }
public map<string,list<string>> fieldmap { get; set; }
public DynamicPageController() {
objmap = new map<string,sobject>();
fieldmap = new map<string,list<string>>();
map<string,schema.sobjecttype> globalDescribe = schema.getglobaldescribe();
for(Dynamic_Page_Controll__c data:[select FieldName__c,ObjectName__c from Dynamic_Page_Controll__c]) {
objmap.put(data.object_name__c,globaldescribe.get(data.object_name__c).newsobject());
if(fieldmap.get(data.object_name__c)==null) {
fieldmap.put(data.object_name__c,new list<string>());
}
fieldmap.get(data.object_name__c).add(data.fieldname__c);
}
}
public void save(){
try{
for(sobject s:objmap.values()){
insert s;
}
}
catch(Exception ex){
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR, ex.getMessage());
ApexPages.addMessage(myMsg);
}
}
}
In the above controller ,we are querying data from custom settings and building two maps,one for fields and another for object name and save method for saving records.
Page:-
==============================================================
<apex:page controller="DynaPageController" >
<apex:form >
<apex:pagemessages />
<apex:pageBlock title="Dynamic Page" >
<!-- Perform the Save Action -->
<apex:pageBlockButtons location="Bottom">
<apex:commandButton value="Save" action="{!save}"/>
</apex:pageBlockButtons>
<!-- To bind both objects and associated field names Dynamically. -->
<apex:pageBlockSection >
<apex:repeat value="{!objmap}" var="obj">
<apex:pageBlock title="{!obj}">
<apex:pageBlockSection >
<apex:repeat value="{!fieldmap[obj]}" var="field">
<apex:inputField value="{!objmap[obj][field]}"/><Br/>
</apex:repeat>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:repeat>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
You can execute the example for multiple objects also.
I Came with new example that is generating Vfpage dynamically and saving data in to object.
Follow the steps,
1. Create Custom settings----> Dynamic Page Controll.
Create Two fields in custom settings 1. ObjectName 2. Field Name.
2. Enter Records in to Custom settings ,
Object Name Field Name
Account Name
Account Email
3. Copy the following class.
======================================================================
public with sharing class DynamicPageController {
public map<string,sobject> objmap { get; set; }
public map<string,list<string>> fieldmap { get; set; }
public DynamicPageController() {
objmap = new map<string,sobject>();
fieldmap = new map<string,list<string>>();
map<string,schema.sobjecttype> globalDescribe = schema.getglobaldescribe();
for(Dynamic_Page_Controll__c data:[select FieldName__c,ObjectName__c from Dynamic_Page_Controll__c]) {
objmap.put(data.object_name__c,globaldescribe.get(data.object_name__c).newsobject());
if(fieldmap.get(data.object_name__c)==null) {
fieldmap.put(data.object_name__c,new list<string>());
}
fieldmap.get(data.object_name__c).add(data.fieldname__c);
}
}
public void save(){
try{
for(sobject s:objmap.values()){
insert s;
}
}
catch(Exception ex){
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR, ex.getMessage());
ApexPages.addMessage(myMsg);
}
}
}
In the above controller ,we are querying data from custom settings and building two maps,one for fields and another for object name and save method for saving records.
Page:-
==============================================================
<apex:page controller="DynaPageController" >
<apex:form >
<apex:pagemessages />
<apex:pageBlock title="Dynamic Page" >
<!-- Perform the Save Action -->
<apex:pageBlockButtons location="Bottom">
<apex:commandButton value="Save" action="{!save}"/>
</apex:pageBlockButtons>
<!-- To bind both objects and associated field names Dynamically. -->
<apex:pageBlockSection >
<apex:repeat value="{!objmap}" var="obj">
<apex:pageBlock title="{!obj}">
<apex:pageBlockSection >
<apex:repeat value="{!fieldmap[obj]}" var="field">
<apex:inputField value="{!objmap[obj][field]}"/><Br/>
</apex:repeat>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:repeat>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
You can execute the example for multiple objects also.
No comments:
Post a Comment