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
prageethprageeth 

Dynamically add new field to a sObject

Hi all;
  Is there a way to create a new field in a sObject by using Apex code.
  For an example I want to create a field named "OpportunityNumber" at run time.
  
 
I want something similar to following code.
  
  Opportunity.createNewField('OpportunityNumber'); 
FaridKognozFaridKognoz

As far as I know you can't create a field from apex code.

One possible aproach is to have a service on an external server that is called from whitin apex. this service calls salesforce using the Metadata API and creates the records you need at run time. This is not "THE" perfect solution for your case because you need a server and code programmed in another platform (like php,c#, java) but is doable and the code needed is pretty simple.

prageethprageeth

Thanks Farid.