Salesforce.com apex class to describe page layouts.
Download this repo and use eclipse or jenkins to deploy the src folder's content.
After you install, you must make sure the salesforce instance server where you are login is included in the remote site settings.
- Go to setup
- Then in the left menu, click on "Security Controls" -> "Remote Site Settings" option
- If you dont have an entry of your salesforce instance, click "New Remote Site" button
- Put a name name to that entry in the "Remote Site Name" field, could be something like "salesforce sandbox" or "salesforce prod"
- In the "Remote Site URl" type in the correct url of the salesforce instance you are loggin, mine looks like "https://na17.salesforce.com" without quotes
- Finally make sure the "Active" checkbox is checked and save the record.
- Navigate to Setup -> Session Settings -> Ensure "Lock sessions to the IP address from which they originated" is turned off.
- If 7, was deselected, you will need to log out and log in again for this change to take effect.
Just do the following in your visualforce page controller
public class EditAccountController {
//expose public property, this property will be listing each layout section
public List < LayoutDescriberHelper.LayoutSection > layoutSections {
get;
set;
}
//...... more members of your controller class.......
public EditAccountController(ApexPages.StandardController controller) {
// Create list of fields to add to controller for object
/* dynamically get all fields for the Account object and add them to the controller */
List<String> fieldList = new List<String>(Schema.getGlobalDescribe().get('Account').getDescribe().fields.getMap().keyset());
// Add fields to controller. This is to avoid the SOQL error in visualforce page
controller.addFields(fieldList);
sObject obj = controller.getRecord();
//.... more code to do things
/************************************************************************/
//getting the default record type
//if we want an specific layout we must provide the appropriate record type id
id theRecordTypeIdToDescribe = LayoutDescriberHelper.getDefaultRecordType(obj);
//get the layout section items
layoutSections = LayoutDescriberHelper.describeSectionWithFields(theRecordTypeIdToDescribe, 'Account');
/***************************************************************************/
}
}
Where layoutSections is a public property in your controller and 'Account' is your sobject type. Make sure you select the fields of the object before referencing them otherwise you'll get an annoying sfdc standard error.
See the following visualforce page example which will generate an edit form for the Account object:
<apex:page standardController='Account' extensions="EditAccountController" >
<apex:form>
<apex:pageblock >
<apex:pageBlockButtons >
<apex:commandButton action="{!save}" value="save"/>
<apex:commandButton action="{!cancel}" value="cancel"/>
</apex:pageBlockButtons>
<!-- Iterate the layoutSections, which is a list of sections -->
<apex:repeat value="{!layoutSections}" var="layoutSection">
<apex:pageBlockSection title="{!layoutSection.Name}" collapsible="{!layoutSection.allowCollapse}" columns="{!layoutSection.columns}">
<!--Each section has layoutFields, let's iterate them as well-->
<apex:repeat value="{!layoutSection.layoutFields}" var="layoutField">
<apex:inputField value="{!Account[layoutField.ApiName]}" rendered="{!not(layoutField.isPlaceHOlder)}" required="{!layoutField.required}" />
<apex:pageblocksectionitem rendered="{!layoutField.isPlaceHolder}" >
</apex:pageblocksectionitem>
</apex:repeat>
</apex:pageBlockSection>
</apex:repeat>
</apex:pageblock>
</apex:form>
</apex:page>
### Override Standard Buttons (optional)
If you want, you can override the standard action/button in salesforce classic mode by going to your object and then go to the "Buttons, Links, and Actions" section, there you edit the edit or new action to replace the standard with your custom visualforce page.