Visualforce Creator:
This is definitely useful when you want to use standard functionality in your custom visualforce page.
public class OutputController {
//variables and getter/setter
public String output {get;set;}
public String hide {get;set;}
//action method of the button
public PageReference create() {
output = hide;
return null;
}
//test method
public static testMethod void testPage(){
Test.setCurrentPage(Page.VisualforceCodeCreator);
Test.startTest();
OutputController oc = new OutputController();
oc.create();
Test.stopTest();
}
}
<apex:page controller="OutputController">
<apex:form >
<script type="text/javascript">
var __sfdcSessionId = '{!GETSESSIONID()}';
</script>
<script src="../../soap/ajax/19.0/connection.js" type="text/javascript"></script>
<script type="text/javascript">
//var output='';
function setupPage() {
var obj = document.getElementById("obj");
var opt = document.getElementById("opt");
var rct = document.getElementById("rct");
var result;
try{
if(rct.value != '' && rct.value != null){
result = sforce.connection.describeLayout(obj.value, new Array(rct.value));
}else {
result = sforce.connection.describeLayout(obj.value);
alert(result);
alert('please note that you have not provided record type id. if the object has more than one record type and different page layout assignments, you will get the code with all the page layouts');
}
//call methods edit or details
if(opt.value == 'Edit')editLayoutResults(result, obj);
else detailLayoutResults(result, obj);
}catch(error){
var er = new String(error);
if(er.indexOf('INVALID_TYPE') != -1)alert('please check object api name');
document.getElementById("{!$Component.hide}").value = '';
}
}
//edit*****************************************layout
function editLayoutResults(result, obj) {
output = '';
var layouts = result.getArray("layouts");
output += '<' + 'apex' + ':' + 'page standardController=' + '"' + obj.value + '"' + '>';
output += '\n';
output += '<' + 'apex' + ':' + 'sectionHeader' + ' title=' + '"' + obj.value + ' Edit' + '"' + ' subtitle=' + '"' + '{' + '!' + obj.value + '.name}' + '"' + '/>';
output += '\n';
output += '<' + 'apex' + ':' + 'form' + '>';
output += '\n';
output += '<' + 'apex' + ':' + 'pageBlock title=' + '"' + obj.value + ' Edit' + '"' + ' mode=' + '"edit">';
output += '\n';
output += '\n';
output += '<' + 'apex' + ':' + 'pageBlockButtons location=' + '"top">';
output += '\n';
output += '<' + 'apex:commandButton value=' + '"' + 'Save' + '" ' + 'action=' + '"' + '{' + '!' + 'save' + '}"' + '/>';
output += '\n';
output += '<' + 'apex:commandButton value=' + '"' + 'Save & New' + '"' + ' action=' + '"' + '{' + '!save}" />';
output += '\n';
output += '<' + 'apex:commandButton value="Cancel" action=' + '"' + '{' + '!cancel}' + '"/>';
output += '\n';
output += '<' + '/apex:pageBlockButtons>';
output += '\n';
output += '\n';
output += '<' + 'apex' + ':' + 'pageBlockButtons location=' + '"bottom">';
output += '\n';
output += '<' + 'apex:commandButton value=' + '"' + 'Save' + '" ' + 'action=' + '"' + '{' + '!' + 'save' + '}"' + '/>';
output += '\n';
output += '<' + 'apex:commandButton value=' + '"' + 'Save & New' + '"' + ' action=' + '"' + '{' + '!save}" />';
output += '\n';
output += '<' + 'apex:commandButton value="Cancel" action=' + '"' + '{' + '!cancel}' + '"/>';
output += '\n';
output += '<' + '/apex:pageBlockButtons>';
//adding fields and sections
var allTheLayouts = result.getArray("layouts");
for (var i = 0; i < allTheLayouts.length; i++){
var layout = allTheLayouts[i];
if (layout.editLayoutSections != null){
var elSections = layout.getArray("editLayoutSections");
for (var j = 0; j < elSections.length; j++){
var els = elSections[j];
output += '\n';
output += '\n';
output += '<' + 'apex:pageBlockSection title=' + '"' + els.heading + '" ' + 'columns=' + '"' + els.columns + '"' + '>';
output += '\n';
var allTheLayoutRows = els.getArray("layoutRows");
for (var k = 0; k < allTheLayoutRows.length; k++){
var lr = allTheLayoutRows[k];
var lis = lr.getArray("layoutItems");
for (var h = 0; h < lis.length; h++){
var li = lis[h];
//only in case of Lead and Contact First Name, which includes Salutation also
if (li.layoutComponents != null && li.layoutComponents.length == 2){
output += '<' + 'apex:inputField value=' + '"' + '{' + '!' + obj.value + '.' + li.layoutComponents[1].value + '}' + '" ' + 'required=' + '"' + li.required.toString() + '"' + '/>';
output += '\n';
}
//for all other fields
else if (li.layoutComponents != null){
output += '<' + 'apex:inputField value=' + '"' + '{' + '!' + obj.value + '.' + li.layoutComponents.value + '}' + '" ' + 'required=' + '"' + li.required.toString() + '"' + '/>';
output += '\n';
}
}
}
output += '<' + '/apex:pageBlockSection>';
output += '\n';
}
}
}
output += '\n';
output += '<' + '/apex:pageBlock>';
output += '\n';
output += '<' + '/apex:form>';
output += '\n';
output += '<' + '/apex:page>';
document.getElementById("{!$Component.hide}").value = output;
}
//details**********************************layout
function detailLayoutResults(result, obj) {
var layouts = result.getArray("layouts");
var output = '';
output += '<' + 'apex' + ':' + 'page standardController=' + '"' + obj.value + '"' + '>';
output += '\n';
output += '<' + 'apex' + ':' + 'sectionHeader' + ' title=' + '"' + obj.value + '"' + ' subtitle=' + '"' + '{' + '!' + obj.value + '.name}' + '"' + '/>';
output += '\n';
output += '<' + 'apex' + ':' + 'pageBlock title=' + '"' + obj.value + '"' + '>';
output += '\n';
//adding fields and sections
var allTheLayouts = result.getArray("layouts");
for (var i = 0; i < allTheLayouts.length; i++){
var layout = allTheLayouts[i];
if (layout.editLayoutSections != null){
var elSections = layout.getArray("editLayoutSections");
for (var j = 0; j < elSections.length; j++){
var els = elSections[j];
output += '\n';
output += '<' + 'apex:pageBlockSection title=' + '"' + els.heading + '" ' + 'columns=' + '"' + els.columns + '"' + '>';
output += '\n';
var allTheLayoutRows = els.getArray("layoutRows");
for (var k = 0; k < allTheLayoutRows.length; k++){
var lr = allTheLayoutRows[k];
var lis = lr.getArray("layoutItems");
for (var h = 0; h < lis.length; h++){
var li = lis[h];
//only in case of Lead and Contact First Name, which includes Salutation also
if (li.layoutComponents != null && li.layoutComponents.length == 2){
output += '<' + 'apex:outputField title=' + '"' + li.label + '" value="' + '{' + '!' + obj.value + '.' + li.layoutComponents[1].value + '}' + '"' + '/>';
output += '\n';
}
//for all other fields
else if (li.layoutComponents != null){
output += '<' + 'apex:outputField title=' + '"' + li.label + '" value="' + '{' + '!' + obj.value + '.' + li.layoutComponents.value + '}' + '"' + '/>';
output += '\n';
}
}
}
output += '<' + '/apex:pageBlockSection>';
output += '\n';
}
}
}
output += '\n';
output += '<' + '/apex:pageBlock>';
output += '\n';
output += '<' + '/apex:page>';
document.getElementById("{!$Component.hide}").value = output;
}
</script>
<p><b>Enter the object API Name and record type Id associated to create visualforce code of the page layout for the selected page type.</b></p>
<table>
<tr >
<td><b>Object API Name</b></td>
<td><input type="text" id="obj"/></td>
</tr>
<tr >
<td><b>Record Type Id</b></td>
<td><input type="text" id="rct"/></td>
</tr>
<tr >
<td><b>Page Type</b></td>
<td><select id="opt">
<option value="Edit">Edit</option>
<option value="Detail">Detail</option>
</select></td>
</tr>
</table>
<center><apex:commandButton id="but" value="Create Page Code" onclick="setupPage()" action="{!create}" rerender="pan" status="pageStatus"/></center>
<apex:inputHidden id="hide" value="{!hide}"/>
<br><br></br></br>
<apex:actionStatus id="pageStatus" startText="Getting page code..." stopText="Page Code"/>
<table >
<tr>
<td>
<apex:outputPanel id="pan">
{!output}
</apex:outputPanel>
</td>
</tr>
</table>
</apex:form>
</apex:page>