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
jessie gracejessie grace 

is there any way to add a custom field to an object through visualforce page?

NagendraNagendra (Salesforce Developers) 
Hi Jessie,

You can create custom fields to an object into a custom object by using metadata API

Please find the sample code snippet below.
MetadataService.CustomField customField = new MetadataService.CustomField();
customField.fullName = 'Test__c.TestField__c';
customField.label = 'Test Field';
customField.type_x = 'Text';
customField.length = 42;
MetadataService.AsyncResult[] results =   service.create(new List<MetadataService.Metadata> { customField });
Please check with below link for more information. Please mark this as solved if the information helps.

Best Regards,
Nagendra.
 
Ramssf70Ramssf70
Hi grace,
we can add field from  controller but i  am not aware of from visuval force page.if you  wannt to add field from apex then i can help you.you can refer this link for creating field from apex
http://salesforce.stackexchange.com/questions/56421/create-custom-fields-with-apex
Anilkumar KotaAnilkumar Kota

Hi Grace,

I've uploaded the code and a write up to Github, include pros and cons of this approach.
 
MetadataService.CustomObject customObject = new MetadataService.CustomObject();
customObject.fullName = 'Test__c';
customObject.label = 'Test';
customObject.pluralLabel = 'Tests';
customObject.nameField = new MetadataService.CustomField();
customObject.nameField.type_x = 'Text';
customObject.nameField.label = 'Test Record';
customObject.deploymentStatus = 'Deployed';
customObject.sharingModel = 'ReadWrite';
MetadataService.AsyncResult[] results = service.create(new List<MetadataService.Metadata> { customObject });
 
Enjoy!
 
https://github.com/financialforcedev/apex-mdapi