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.
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.
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?
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.
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.
Hello @Suraj Tripathi , thanks for the code you provided. im using same code, but not able to create object as well as fields. im getting error for object as [ Illegal assignment from List<MetadataService.SaveResult> to List<MetadataService.AsyncResult> ] and for fields im getting error as [ Illegal assignment from List<MetadataService.SaveResult> to List<MetadataService.AsyncResult> ]. any specific change need to done. not able to find solution for this. thanks in advance
Hi,
You can not create custom object and its field by apex. You can only retrive object and field information using apex.
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:
Create Field using apex Code:
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/
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.
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
Just referred this thread for creating custom settings automatically on sandbox refresh, and it runs perfectly using below code-
For custom Settings- For custom object-
how to insert controllers into the organization?
Thanks
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
[ Illegal assignment from List<MetadataService.SaveResult> to List<MetadataService.AsyncResult> ]
and for fields im getting error as [ Illegal assignment from List<MetadataService.SaveResult> to List<MetadataService.AsyncResult> ]. any specific change need to done. not able to find solution for this.
thanks in advance