function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
George OsterGeorge Oster 

Can I edit a Page Layout through Apex?

I have several Related Lists which are on the Page Layout for Account.  I would like to programatically add an additional Related List to the Page Layout for Account through Apex alone.  Is that possible?
 
Flint LockwoodFlint Lockwood
In short, no. To change page layouts, you have to use the Metadata API. 
UC InnovationUC Innovation
Hi George,

There is a way to access metadata using the metadata api in salesforce. This post (http://salesforce.stackexchange.com/questions/18843/using-meta-data-api-in-apex) explores this a bit and you might find it useful. The response by Andrew Fawcett is great but there is no use restating it here.

The code (wrapper class) they are refering to is the in the following github repo:
https://github.com/financialforcedev/apex-mdapi

Good luck and hope this helps!

AM
Andrew EchevarriaAndrew Echevarria
The best way to add custom desirables to a page layout would be to do it through adding a VisualForce section and then using apex in the VF page's controller. Would this meet your needs?
SKolakanSKolakan
You can have multiple page layouts (with related lists as needed) associated with record types and you can programatically switch the record type as needed.