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

Create New Custom Fields from Apex?

Is there any way to create new custom fields from Apex?



Nope.  You may be able to write and call a webservice that could modify the sobject's markup file and deploy to server.


Ok, thanks Wizrad, that would have been really nice.  Do you know is there is an IdeaExchange entry for this yet?


Also, are there any workarounds?  Maybe calling the metadata API from javascript?


Chamil MadusankaChamil Madusanka



The Summer ‘11 release has a new pilot feature called dynamic Visualforce components. Dynamic Visualforce components offer a way to create Visualforce pages that render according to a variety of states, such as a user's permissions or actions. Rather than using standard markup, dynamic Visualforce components are designed
in Apex.


The dynamic Visualforce component is defined in Apex like this:




For example, <apex:dataTable> becomes Component.Apex.DataTable. Visualforce components that are
dynamically represented in Apex operate like regular classes. Every attribute that exists on a standard Visualforce
component is available as a property in the corresponding Apex representation with get and set accessors. For example,

you could manipulate the value attribute on an <apex:outputText> component like this:


Component.Apex.OutputText otField = new Component.Apex.OutputText();
otField.value='Some wild output text!';


Note: The Standard Component Reference contains the dynamic representation for all valid Visualforce
For more information, see Dynamic Visualforce Components in the Visualforce Developer’s Guide.


If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.


Chamil's Blog



I have several 100 custom fields to add to an object.  Can you post an example of tapping into the API to create the additional fields for a managed package object?  Theres got to be a better way then using the online UI.