Activity › Forums › Salesforce® Discussions › How can I create custom object using the Metadata API from Apex?
-
How can I create custom object using the Metadata API from Apex?
Posted by Piyush on April 29, 2016 at 7:54 AMHow can I create custom object using the Metadata API from Apex class?
Gourav replied 10 years, 1 month ago 4 Members · 6 Replies -
6 Replies
-
Go through http://salesforce.stackexchange.com/questions/20763/creating-a-custom-object-using-rest-api
- [adinserter block='9']
-
Use this code in your Apex class to create custom Object :-
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 }); -
Hi Piyush,
Try this:
HTTP h = new HTTP();
HTTPRequest req = new HTTPRequest();
req.setMethod(‘POST’);
req.setHeader(‘Content-Type’, ‘text/xml’);
req.setHeader(‘SOAPAction’, ‘create’);String b = ‘<?xml version=”1.0″ encoding=”UTF-8″?>’;
b += ‘<soapenv:Envelope xmlns:soapenv=”http://schemas.xmlsoap.org/soap/envelope/” xmlns:xsd=”http://www.w3.org/2001/XMLSchema” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”>’;
b += ‘<soapenv:Header>’;
b += ‘<ns1:SessionHeader soapenv:mustUnderstand=”0″ xmlns:ns1=”http://soap.sforce.com/2006/04/metadata”>’;
b += ‘<ns1:sessionId>’ + UserInfo.getSessionId() + ‘</ns1:sessionId>’;
b += ‘</ns1:SessionHeader>’;
b += ‘</soapenv:Header>’;
b += ‘<soapenv:Body>’;
b += ‘<create xmlns=”http://soap.sforce.com/2006/04/metadata”>’;
b += ‘<metadata xsi:type=”ns2:CustomObject” xmlns:ns2=”http://soap.sforce.com/2006/04/metadata”>’;
b += ‘<fullName>sample__c</fullName>’;
b += ‘<deploymentStatus>Deployed</deploymentStatus>’;
b += ‘<description>created by the Metadata API</description>’;
b += ‘<enableActivities>true</enableActivities>’;
b += ‘<label>sample Object</label>’;
b += ‘<nameField>’;
b += ‘<displayFormat>AN-{0000}</displayFormat>’;
b += ‘<label>sample__c Name</label>’;
b += ‘<type>AutoNumber</type>’;
b += ‘</nameField>’;
b += ‘<pluralLabel>sample Objects</pluralLabel>’;
b += ‘<sharingModel>ReadWrite</sharingModel>’;
b += ‘</metadata>’;
b += ‘</create>’;
b += ‘</soapenv:Body>’;
b += ‘</soapenv:Envelope>’;req.setBody(b);
req.setCompressed(false);
req.setEndpoint(‘https://na12-api.salesforce.com/services/Soap/m/25.0’);
HTTPResponse resp = h.send(req);
System.debug(resp.getBody()); -
you need to import all the classes from the metadata sample to get this workfing..
also your MetadataServiceExamples class should have all the methods to run.. your createObject method is calling createService method which is missing..check this for all the classes in metadata sample and add them to your org
https://github.com/financialforcedev/apex-mdapi/tree/master/apex-mdapi/src/classesand this is for the MetadataServiceExample class
https://github.com/financialforcedev/apex-mdapi/blob/master/apex-mdapi/src/classes/MetadataServiceExamples.cls
Log In to reply.