Create Multiple Records of Salesforce Objects from a Single Page
Presently we can create records by click on tabs of their related objects but through the below-given piece of code, we can create records of any object in the org either a standard or a custom object from a single Visualforce page only. Follow the below code to achieve the task.
Let's have a piece of code that you need to save to your org for achieving this.
Visualforce Page Code:
<apex:page controller="FieldSelectSobjectTypeClass"> <apex:form id="od"> <apex:pageblock> <apex:pagemessages rendered="true"></apex:pagemessages> <apex:pageblocksection> <apex:pageblocksectionitem> <apex:outputlabel value="Object List"></apex:outputlabel> <apex:selectlist size="1" value="{!selectedObj}"> <apex:selectoptions value="{!objects}"></apex:selectoptions> <apex:actionsupport event="onchange" action="{!details}" rerender="od"></apex:actionsupport> <!-- for selecting any other object in picklist--> </apex:selectlist> </apex:pageblocksectionitem> <apex:pageblocksectionitem> <apex:panelgrid columns="3"> <apex:panelgroup style="width: 40%;"> <b>Available Fields</b> <apex:selectlist value="{!sfields}" multiselect="true" style="width: 120px; height: 120px;"> <apex:selectoptions value="{!fsnOption}"></apex:selectoptions> </apex:selectlist> </apex:panelgroup> <apex:panelgroup style="width: 20%; height: 120px;"> <table height="120px"> <tbody> <tr> <td> <apex:commandbutton value="Add" action="{!addFields}" style="width: 80px;" rerender="od"></apex:commandbutton></td> </tr> <tr> <td> <apex:commandbutton value="Remove" action="{!removeFields}" style="width: 80px;" rerender="od"></apex:commandbutton></td> </tr> </tbody> </table> </apex:panelgroup> <apex:panelgroup> <b>Selected Fields</b> <apex:selectlist value="{!rfields}" multiselect="true" style="width: 120px; height: 120px;"> <apex:selectoptions value="{!fsOption}"></apex:selectoptions> </apex:selectlist> </apex:panelgroup> </apex:panelgrid> </apex:pageblocksectionitem> <apex:commandbutton value="Click Here" action="{!queryMydata}" rerender="od"></apex:commandbutton> <apex:pageblocksectionitem rendered="{!pbtflag}"> <apex:pageblocksection> <apex:repeat value="{!listWrapper}" var="f"> <apex:repeat value="{!f.fields}" var="l"> <apex:inputfield value="{!f.sObj[l]}"></apex:inputfield> </apex:repeat> </apex:repeat> </apex:pageblocksection> </apex:pageblocksectionitem> <apex:pageblocksectionitem> <apex:commandlink action="{!save}" target="_blank"> <apex:commandbutton value="Create Record"></apex:commandbutton> </apex:commandlink> <apex:commandbutton value="Reset All" action="{!reset}" immediate="true" html-formnovalidate="formnovalidate"></apex:commandbutton> </apex:pageblocksectionitem> </apex:pageblocksection> </apex:pageblock> </apex:form> </apex:page>
Apex Class Code:
public class FieldSelectSobjectTypeClass { public List objList {set; get;} public List objects {set; get;} public String selectedObj {set; get;} public Map<string,schema.SobjectType> mobjects {set; get;} public String objDescription {set; get;} //for fields public List fields {set; get;} public Set selectedfld {set; get;} public Set notSelectedfld {set; get;} public Map<String,Schema.SObjectField> mfields; // {set; get;} public List fsOption {set; get;} public List fsnOption {set; get;} public List sfields {set; get;} public List rfields {set; get;} //To display fields or query results on pageBlockTable public string query {set; get;} public List myData{set; get;} public List myfields{set; get;} public Boolean pbtflag {set; get;} //To display input fields Public list listWrapper {get;set;} //public String message {get; set;} //constructors public FieldSelectSobjectTypeClass() { pbtflag = false; myData = new List(); myfields = new List(); objList = new List(); objects = new List(); mobjects = Schema.getGlobalDescribe(); //returns objects(string) and & their properties like label,plurals etc(schema.SobjectType) objList.addAll(mobjects.keySet()); objList.sort(); //System.debug('objList::' +objList); //To make '-None-' available in picklist SelectOption p=new SelectOption('None','-None-'); objects.add(p); //System.debug('Objects::' +objects); //For fields mfields = new Map<String,Schema.SObjectField>(); fields = new List(); selectedfld = new Set(); notSelectedfld = new Set(); fsOption = new List(); fsnOption = new List(); sfields = new List(); rfields = new List(); for(String s: objList) { SelectOption op=new SelectOption(s,s); //to display in picklist options objects.add(op); //System.debug('Objects New::' +objects); } } //To fetch details about the selected object public void details() { Selectedfld.clear(); sfields.clear(); rfields.clear(); if(selectedObj=='None') { //reset(); } else { Schema.DescribeSObjectResult myObj= mobjects.get(selectedObj).getDescribe(); //System.debug('myObj::::'+myObj); objDescription = ''+myObj; //System.debug('objDescription ::::'+objDescription); //fields fetch from selected object mfields = mobjects.get(selectedObj).getDescribe().fields.getMap(); //System.debug('mfields ::::'+mfields); fields.clear(); fields.addAll(mfields.keySet()); fields.sort(); //System.debug('fields ::::'+fields); notSelectedfld.clear(); notSelectedfld.addAll(fields); //System.debug('notSelectedfld::::'+notSelectedfld); show(); } } public void show() { fsnOption.clear(); fsOption.clear(); for (String s : notSelectedfld) { SelectOption op = new SelectOption(s,s); fsnOption.add(op); } for (String s1 :selectedfld){ selectoption op1= new selectoption(s1,s1); fsOption.add(op1); } } public void addFields() { notSelectedfld.removeAll(sfields); selectedfld.addAll(sfields); //System.debug('selectedfld::' +selectedfld); show(); } public void removeFields() { Selectedfld.removeAll(rfields); notselectedfld.addAll(rfields); show(); } public void queryMydata() { if(!selectedfld.isEmpty()) { pbtflag = true; listWrapper = new list(); Schema.SObjectType convertType = Schema.getGlobalDescribe().get(selectedObj); Sobject genericObject = convertType.newSObject(); innerWrapperClass obj = new innerWrapperClass(genericObject,selectedfld); listWrapper.add(obj); } else { ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL,'Please add fields to Selected Fields box'); ApexPages.addMessage(myMsg); // system.debug('myMsg::::'+myMsg); } } public PageReference save() { list insertThis = new list(); for(innerWrapperClass wrap : listWrapper) { insertThis.add(wrap.sObj); } insert insertThis; PageReference pg = new PageReference('/'+insertThis[0].id); pg.setRedirect(true); return pg ; } public PageReference reset() { //System.debug('In Reset Method'); PageReference newpage = new PageReference(System.currentPageReference().getURL()); newpage.setRedirect(true); return newpage; } public class innerWrapperClass { public SObject sObj {get;set;} public List fields{get;set;} public innerWrapperClass(SObject sObj , Set fields) { this.sObj = sObj; this.fields = new List(); this.fields.addAll(fields); } } }
To know more about how to handle this code in your Salesforce org please Click Here For Demo
Thanks For Reading
Happy Salesforce!!