+ Start a Discussion
yarramyarram 

Urgent: How to create custom object and custom object fields using Apex code

Hi,

 

How to create custom object and custom object fields using Apex code.

 

Is this possible?

 

Thanks,

Yarram.

gbu.varungbu.varun

Hi,

 

You can not create custom object and its field by apex. You can only retrive object and field information using apex.

hitesh90hitesh90

Hi,

 

Yes, You can create custom object and custom object fields using Apex code.

 

Step 1: You have to first insert MetadataService and MetadataServiceTest controller in your organization.

Link for That classes are as below In developer console.

MetadataService controller

MetadataService Test controller

 

Step 2: Then execure below code.

see below example.

 

Create Object using apex Code:

MetadataService.MetadataPort service = new MetadataService.MetadataPort();  
service.SessionHeader = new MetadataService.SessionHeader_element();
service.SessionHeader.sessionId = UserInfo.getSessionId();

List<MetadataService.Metadata> fields = new List<MetadataService.Metadata>();
MetadataService.CustomObject  customobject = new MetadataService.CustomObject();
customobject.fullName = 'custom_create__c';
customobject.label = 'Custom created object';
customobject.pluralLabel = 'Custom created objects';
customObject.nameField = new MetadataService.CustomField();
customobject.nameField.type_x = 'Text';
customobject.nameField.label = 'Custom created field';
customobject.deploymentStatus = 'Deployed';
customObject.sharingModel = 'ReadWrite';
fields.add(customobject);

MetadataService.AsyncResult[] results = service.create(fields);

 

 

Create Field using apex Code:

MetadataService.MetadataPort service = new MetadataService.MetadataPort();  
service.SessionHeader = new MetadataService.SessionHeader_element();
service.SessionHeader.sessionId = UserInfo.getSessionId();

List<MetadataService.Metadata> fields = new List<MetadataService.Metadata>();
MetadataService.CustomField customField = new MetadataService.CustomField();
customField.fullName = 'custom_create__c.custom_create_field__c';
customField.label = 'Custom created field';
customField.defaultvalue = 'false';
//customField.sharingModel = 'ReadWrite';
customField.type_x = 'Checkbox';
fields.add(customField);

MetadataService.AsyncResult[] results = service.create(fields);

 

 

Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator & Advanced Administrator & Sales cloud consultant
My Blog:- http://mrjavascript.blogspot.in/

aswanijagadesh1.397398535585991E12aswanijagadesh1.397398535585991E12
Hai hitesh90,
  By following this code i created a custom field in Account object,but comming to display purpose i added this in page layout ,but it is not shown in account standared page .how can i see this field in standared account page. 
aswanijagadesh1.397398535585991E12aswanijagadesh1.397398535585991E12
i got it by putting  Set Field-Level Security for that field.
Ravi NarayananRavi Narayanan
I know its pretty old post. but the above controller contains more than 9500 lines and salesforce gets hanged when i try to paste. can someone help me on this please? 
Sumit jaitlySumit jaitly
Hi

i am getting below mentioned error, when try to use above steps to create a Custom Object

Compile error at line 15 column 41 Method does not exist or incorrect signature: [MetadataService.MetadataPort].create(List<MetadataService.Metadata>)


Please suggest me what kind of setting is required to execute above steps and please note i am using Eclipse 3.6 force.com IDE.

Regards
Sumit
Kenoye NakuKenoye Naku
The method should not be 'create' change it to createMetadata and it should work fine. Also you may get an error for IO Exception and will need to do Remote Site Settings to enable it. 
Shivi SrivastavaShivi Srivastava
Hi All...
Just referred this thread for creating custom settings automatically on sandbox refresh, and it runs perfectly using below code-

For custom Settings-
List<MetadataService.Metadata> fields = new List<MetadataService.Metadata>();
MetadataService.CustomObject  customobject = new MetadataService.CustomObject();
customobject.fullName = 'custom_settings1__c';
customobject.label = 'Custom settings1';
customobject.customSettingsType='List';
fields.add(customobject);

MetadataService.SaveResult[] results = service.createMetadata(fields);
system.debug(results);
For custom object-
List<MetadataService.Metadata> fields = new List<MetadataService.Metadata>();
MetadataService.CustomObject  customobject = new MetadataService.CustomObject();
customobject.fullName = 'custom_create__c';
customobject.label = 'Custom create object';
customobject.pluralLabel = 'Custom created objects';
customObject.nameField = new MetadataService.CustomField();
customobject.nameField.type_x = 'Text';
customobject.nameField.label = 'Custom created field';
customobject.deploymentStatus = 'Deployed';
customObject.sharingModel = 'ReadWrite';
fields.add(customobject);

MetadataService.SaveResult[] results = service.createMetadata(fields);
system.debug(results);
Anurag Reddy KanchimireddyAnurag Reddy Kanchimireddy
Hello when i tried to create custon oject using the above code. its throwing error as "Variable does not exist: service".
Artem Zamarajev 15Artem Zamarajev 15
Hi hitesh90,
how to insert controllers into the organization?
Thanks