Page:
<apex:page controller="objectcreator" sidebar="false" rendered="true" docType="html-5.0" tabStyle="ObjectCreator__tab" >
<div style="float:right" >
<!-- <apex:image value="{!$Resource.objectcreatorlogo}" height="100" width="1250" /> -->
<apex:image value="{!$Resource.ObjectCreatorlogo2}" />
</div>
<div >
<apex:form id="form" >
<apex:pageBlock id="pgBlckId" >
<h1>Enter Object Details </h1><br/><br/>
<apex:panelGrid columns="2">
<apex:outputLabel style="padding-left:22px;" styleClass="outputLabel" value="Object Name:" />
<apex:inputText styleClass="inputText" value="{!Name}" id="ObjectName"/>
<apex:outputLabel style="padding-left:75px;" styleClass="outputLabel" value="Label:"/>
<!-- <apex:inputText styleClass="inputText" value="{!Name}" id="Label" onclick="val()" /> -->
<apex:input type="text" style="padding-left:19px;" styleClass="inputText" id="Label" value="{!Label}"/>
<apex:outputLabel styleClass="outputLabel" style="padding-left:16px;" value="Record Name:" />
<apex:inputText styleClass="inputText" value="{!rname}" id="rname" html-placeholder="Account Name"/>
<apex:outputLabel style="padding-left:47px;" styleClass="outputLabel" value="DataType:" />
<apex:selectList value="{!DataType}" multiselect="false" size="1" >
<apex:selectOption itemValue="Text" itemLabel="Text"/>
<apex:selectOption itemValue="AutoNumber" itemLabel="Auto Number"/>
<apex:actionSupport event="onchange" reRender="form" action="{!find}"/>
</apex:selectList>
<apex:outputLabel styleClass="outputLabel" style="padding-left:8px;" value="Display Format:" rendered="{!flag}" />
<apex:inputText styleClass="inputText" value="{!dformat}" id="dformat" html-placeholder="A-{0000}" rendered="{!flag}" />
<apex:outputLabel styleClass="outputLabel" value="Starting Number:" rendered="{!flag}"/>
<apex:inputText styleClass="inputText" value="{!sno}" id="sno" rendered="{!flag}" />
</apex:panelGrid>
<div style="padding-left:110px;" >
<apex:commandButton style="background:black;width:150px;height:40px;color:white;font-size:12pt;" action="{!createobject}" value="Create an Object" reRender="form" status="actStatusId" oncomplete="options('{!str}');" id="theButton"/>
<apex:actionStatus id="actStatusId" >
<apex:facet name="start" >
<img src="{!$Resource.Searching}" />
</apex:facet>
</apex:actionStatus>
</div>
<apex:pageMessages id="msg" ></apex:pageMessages>
</apex:pageBlock>
<p id="out"></p>
<p id="demo" style="color:red;font-size:18pt;"></p>
<p id="refresh"></p>
</apex:form>
</div>
<script>
function options(hi){
if(hi.includes('errors=null')){
document.getElementById("demo").innerHTML='<h2>To Go to Object List page </h2><a style="text-decoration: none;" href="javascript:redirect();"><h3>clickhere</h3></a>';
document.getElementById("refresh").innerHTML='<h2>To Create New object </h2><a style="text-decoration: none;" href="javascript:refresh();"><h3>clickhere</h3></a>';
}
}
</script>
<script>
function redirect(){
window.open("/p/setup/custent/CustomObjectsPage?setupid=CustomObjects");
}
</script>
<script>
function refresh() {
window.location.reload();
}
</script>
<style>
h1{
color:#04cef7;
width:150px;
clear:left;
font-size:15pt;
}
.outputLabel{
font-size:12pt;
}
h2 {
font-weight:bold;
font-size:20px;
color:green;
clear: left;
text-align:center;
width:500px;
}
a,h3{color:red;
font-weight:bold;
font-size:18px;
text-decoration: none;}
</style>
</apex:page>
Controller:
public with sharing class objectcreator{
public String sno { get; set; }
public String dformat { get; set; }
public String rname { get; set; }
Public String Name {get; set;}
Public String Label {get; set;}
Public String DataType {get; set;}
Public List<MetadataService.SaveResult> results {get;set;}
public string str {get;set;}
public Boolean flag {get;set;}
public void createObject(){
MetadataService.MetadataPort service = MetadataServiceExamples.createService();
MetadataService.CustomObject customObject = new MetadataService.CustomObject();
if(Name.length()<=0||Name=='') ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Please enter Object Name '));
else if(Label=='') ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Please enter Label'));
else if(rname=='') ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Please enter Record Name'));
else{
customObject.fullName =Name+'__c';
customObject.label = Label ;
customObject.pluralLabel = Label+'s';
customObject.nameField = new MetadataService.CustomField();
customObject.nameField.type_x = DataType;
customObject.nameField.label = rname;
customObject.deploymentStatus = 'Deployed';
customObject.sharingModel = 'ReadWrite';
try{
results = service.createMetadata(new MetadataService.Metadata[] { customObject });
system.debug('-------------'+results);
MetadataServiceExamples.handleSaveResults(results[0]);
str=string.valueof(results[0]);
if(str.contains('errors=null')) { ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.CONFIRM, 'Object Created Successfully!')); }
}catch(exception e){
string err=string.valueof(e);
if(err.contains('DUPLICATE_DEVELOPER_NAME')) ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,'Object name is already in use'));
else if(err.contains('FIELD_INTEGRITY_EXCEPTION')) ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,'Invalid Object name'));
// ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,'Invalid ObjectName or Label'));
}
}
}
public void find() {
if(DataType == 'AutoNumber') {
flag=true;
} else if(DataType == 'Text') {
flag=false;
}
}
}
This code will work only if you have MetadataService class in your Org.
You can download it from this link:MetadataService.cls
<div style="float:right" >
<!-- <apex:image value="{!$Resource.objectcreatorlogo}" height="100" width="1250" /> -->
<apex:image value="{!$Resource.ObjectCreatorlogo2}" />
</div>
<div >
<apex:form id="form" >
<apex:pageBlock id="pgBlckId" >
<h1>Enter Object Details </h1><br/><br/>
<apex:panelGrid columns="2">
<apex:outputLabel style="padding-left:22px;" styleClass="outputLabel" value="Object Name:" />
<apex:inputText styleClass="inputText" value="{!Name}" id="ObjectName"/>
<apex:outputLabel style="padding-left:75px;" styleClass="outputLabel" value="Label:"/>
<!-- <apex:inputText styleClass="inputText" value="{!Name}" id="Label" onclick="val()" /> -->
<apex:input type="text" style="padding-left:19px;" styleClass="inputText" id="Label" value="{!Label}"/>
<apex:outputLabel styleClass="outputLabel" style="padding-left:16px;" value="Record Name:" />
<apex:inputText styleClass="inputText" value="{!rname}" id="rname" html-placeholder="Account Name"/>
<apex:outputLabel style="padding-left:47px;" styleClass="outputLabel" value="DataType:" />
<apex:selectList value="{!DataType}" multiselect="false" size="1" >
<apex:selectOption itemValue="Text" itemLabel="Text"/>
<apex:selectOption itemValue="AutoNumber" itemLabel="Auto Number"/>
<apex:actionSupport event="onchange" reRender="form" action="{!find}"/>
</apex:selectList>
<apex:outputLabel styleClass="outputLabel" style="padding-left:8px;" value="Display Format:" rendered="{!flag}" />
<apex:inputText styleClass="inputText" value="{!dformat}" id="dformat" html-placeholder="A-{0000}" rendered="{!flag}" />
<apex:outputLabel styleClass="outputLabel" value="Starting Number:" rendered="{!flag}"/>
<apex:inputText styleClass="inputText" value="{!sno}" id="sno" rendered="{!flag}" />
</apex:panelGrid>
<div style="padding-left:110px;" >
<apex:commandButton style="background:black;width:150px;height:40px;color:white;font-size:12pt;" action="{!createobject}" value="Create an Object" reRender="form" status="actStatusId" oncomplete="options('{!str}');" id="theButton"/>
<apex:actionStatus id="actStatusId" >
<apex:facet name="start" >
<img src="{!$Resource.Searching}" />
</apex:facet>
</apex:actionStatus>
</div>
<apex:pageMessages id="msg" ></apex:pageMessages>
</apex:pageBlock>
<p id="out"></p>
<p id="demo" style="color:red;font-size:18pt;"></p>
<p id="refresh"></p>
</apex:form>
</div>
<script>
function options(hi){
if(hi.includes('errors=null')){
document.getElementById("demo").innerHTML='<h2>To Go to Object List page </h2><a style="text-decoration: none;" href="javascript:redirect();"><h3>clickhere</h3></a>';
document.getElementById("refresh").innerHTML='<h2>To Create New object </h2><a style="text-decoration: none;" href="javascript:refresh();"><h3>clickhere</h3></a>';
}
}
</script>
<script>
function redirect(){
window.open("/p/setup/custent/CustomObjectsPage?setupid=CustomObjects");
}
</script>
<script>
function refresh() {
window.location.reload();
}
</script>
<style>
h1{
color:#04cef7;
width:150px;
clear:left;
font-size:15pt;
}
.outputLabel{
font-size:12pt;
}
h2 {
font-weight:bold;
font-size:20px;
color:green;
clear: left;
text-align:center;
width:500px;
}
a,h3{color:red;
font-weight:bold;
font-size:18px;
text-decoration: none;}
</style>
</apex:page>
Controller:
public with sharing class objectcreator{
public String sno { get; set; }
public String dformat { get; set; }
public String rname { get; set; }
Public String Name {get; set;}
Public String Label {get; set;}
Public String DataType {get; set;}
Public List<MetadataService.SaveResult> results {get;set;}
public string str {get;set;}
public Boolean flag {get;set;}
public void createObject(){
MetadataService.MetadataPort service = MetadataServiceExamples.createService();
MetadataService.CustomObject customObject = new MetadataService.CustomObject();
if(Name.length()<=0||Name=='') ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Please enter Object Name '));
else if(Label=='') ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Please enter Label'));
else if(rname=='') ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Please enter Record Name'));
else{
customObject.fullName =Name+'__c';
customObject.label = Label ;
customObject.pluralLabel = Label+'s';
customObject.nameField = new MetadataService.CustomField();
customObject.nameField.type_x = DataType;
customObject.nameField.label = rname;
customObject.deploymentStatus = 'Deployed';
customObject.sharingModel = 'ReadWrite';
try{
results = service.createMetadata(new MetadataService.Metadata[] { customObject });
system.debug('-------------'+results);
MetadataServiceExamples.handleSaveResults(results[0]);
str=string.valueof(results[0]);
if(str.contains('errors=null')) { ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.CONFIRM, 'Object Created Successfully!')); }
}catch(exception e){
string err=string.valueof(e);
if(err.contains('DUPLICATE_DEVELOPER_NAME')) ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,'Object name is already in use'));
else if(err.contains('FIELD_INTEGRITY_EXCEPTION')) ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,'Invalid Object name'));
// ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,'Invalid ObjectName or Label'));
}
}
}
public void find() {
if(DataType == 'AutoNumber') {
flag=true;
} else if(DataType == 'Text') {
flag=false;
}
}
}
This code will work only if you have MetadataService class in your Org.
You can download it from this link:MetadataService.cls
No comments:
Post a Comment