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
Vasavi VajinepalliVasavi Vajinepalli 

URGENT: How to create custom objects and custom fields dynamically through Java or Apex

Hi,

Can anyone please suggest me how to create custom objects and custom fields dynamically through Java or apex code.
If it is through Java, please suggest me the supported Jars as well. Appreciate if you can share the code for the same.

Thanks,
Vasavi
srlawr uksrlawr uk
If you really want to do this through code, you can engage the "Metadata API" to create objects and fields. You can do this via Java or Apex... though the approaches will be quite different. If you want to do it via Java you will have to overcome OAuth authentication etc. to access the APIs (standard oAuth library for that) if you develop a solution in Apex inside the Salesforce organisation you can just tap up the APIs straight off (I believe!)

So here is a link to the Metadata API create() function: https://developer.salesforce.com/docs/atlas.en-us.api_meta.meta/api_meta/meta_create.htm

And there are examples here of how to use that to send the definition of a new object (using Apex):
https://github.com/financialforcedev/apex-mdapi/blob/master/apex-mdapi/src/classes/MetadataServiceExamples.cls#L35


You should be able to take that example and just fire an object into an Org if you want.. hopefully that will get you going? All you have to do then is copy and paste the code a bunch of times to insert the objects and fields you desire!
rajesh k 10rajesh k 10
Hi,

You can do with metadata API.

Thanka!

 
Suraj Tripathi 47Suraj Tripathi 47
Hi Vasavi,

For creating Object and field using apex code firstly you have to insert  MetadataService controller and MetadataService Test controller
in your organization.

If you find your Solution then mark this as the best answer.

Thank you!

Regards,
Suraj Tripathi