Toggle Side Panel

  • Home
  • Articles
    • All Articles
    • Blogs
    • Videos
    • Infographics
  • Consultants
    • Salesforce Product Expertise
      • Top Salesforce ConsultantsTop Salesforce Consultants
      • Marketing Cloud ConsultantsMarketing Cloud Consultants
      • Service Cloud ConsultantsService Cloud Consultants
      • Experience Cloud ConsultantsExperience Cloud Consultants
      • Analytics Cloud ConsultantsAnalytics Cloud Consultants
    • Salesforce Industry Expertise
      • Non-Profit Cloud ConsultantsNon-Profit Cloud Consultants
      • Financial Service Cloud ConsultantsFinancial Service Cloud Consultants
      • Health Cloud ConsultantsHealth Cloud Consultants
      • Commerce Cloud ConsultantsCommerce Cloud Consultants
      • Manufacturing Cloud ConsultantsManufacturing Cloud Consultants
    • Salesforce Experts by Location
      • USATop Salesforce Consultants in USA
      • IndiaTop Salesforce Consultants in India
      • AustraliaTop Salesforce Consultants in Australia
      • United KingdomTop Salesforce Consultants in UK
      • CanadaTop Salesforce Consultants in Canada
  • Webinars
  • Marketplace
  • Advertise With Us
  • Contact Us
  • Discussions
More options
    Sign in Sign up
    • Home
    • Articles
      • All Articles
      • Blogs
      • Videos
      • Infographics
    • Consultants
      • Salesforce Product Expertise
        • Top Salesforce ConsultantsTop Salesforce Consultants
        • Marketing Cloud ConsultantsMarketing Cloud Consultants
        • Service Cloud ConsultantsService Cloud Consultants
        • Experience Cloud ConsultantsExperience Cloud Consultants
        • Analytics Cloud ConsultantsAnalytics Cloud Consultants
      • Salesforce Industry Expertise
        • Non-Profit Cloud ConsultantsNon-Profit Cloud Consultants
        • Financial Service Cloud ConsultantsFinancial Service Cloud Consultants
        • Health Cloud ConsultantsHealth Cloud Consultants
        • Commerce Cloud ConsultantsCommerce Cloud Consultants
        • Manufacturing Cloud ConsultantsManufacturing Cloud Consultants
      • Salesforce Experts by Location
        • USATop Salesforce Consultants in USA
        • IndiaTop Salesforce Consultants in India
        • AustraliaTop Salesforce Consultants in Australia
        • United KingdomTop Salesforce Consultants in UK
        • CanadaTop Salesforce Consultants in Canada
    • Webinars
    • Marketplace
    • Advertise With Us
    • Contact Us
    • Discussions
    Close search

    Activity › Forums › Salesforce® Discussions › How can I create custom object using the Metadata API from Apex?

    Tagged: Metadata API, MetadataService, Salesforce Apex Class, Salesforce Custom Object

    • Salesforce® Discussions

      How can I create custom object using the Metadata API from Apex?

      Posted by Piyush on April 29, 2016 at 7:54 AM

      How can I create custom object using the Metadata API from Apex class?

       

      Gourav replied 9 years, 8 months ago 4 Members · 6 Replies
      • Metadata API
      • MetadataService
      • Salesforce Apex Class
      • Salesforce Custom Object
    • 6 Replies
    • Naman

      Member
      April 29, 2016 at 7:58 AM

      Go through http://salesforce.stackexchange.com/questions/20763/creating-a-custom-object-using-rest-api

    • Gourav

      Member
      April 29, 2016 at 7:59 AM

      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 });

    • shafali

      Member
      April 29, 2016 at 8:06 AM

      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());

    • Piyush

      Member
      April 29, 2016 at 8:10 AM

      @Gourav when i execute this sample of the code I am getting error Invalid type: MetadataService.CustomObject

    • Piyush

      Member
      April 29, 2016 at 8:15 AM

      Thanks all it really helpful for me.

    • Gourav

      Member
      April 29, 2016 at 9:10 AM

      @Piyush

      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/classes

      and this is for the MetadataServiceExample class
      https://github.com/financialforcedev/apex-mdapi/blob/master/apex-mdapi/src/classes/MetadataServiceExamples.cls

    Log In to reply.

    • Public
    • All Members
    • My Connections
    • Only Me
    • Public
    • All Members
    • My Connections
    • Only Me
    • Public
    • All Members
    • My Connections
    • Only Me

    application solution

    Popular Salesforce Blogs

    Going Beyond Traditional Implementation Methodologies for Salesforce Service Cloud: A Fireside Chat

    Blog in Salesforce Cloud Platform, Salesforce Implementation

    Salesforce Service Cloud has myriads of capabilities that enable you to ace your customer service game. But optimizing Service Cloud the right way calls for…

    AppExchange, Application Services, Assessment Tool, Best Practices, big data analytics
    Aastha Thakur Jun 17, 2021
    2,788  Views

    Why Salesforce is the #1 Choice for Customer Service Management

    Blog in Salesforce Implementation

    Customer service is at the heart of every thriving business, and Salesforce has positioned itself as the go-to choice for companies looking to transform their…

    Certified Salesforce Consulting Company, Custom Salesforce Development Services, Customer Service, Salesforce Consulting Companies in the USA, Salesforce CRM
    AwsQuality Aug 1, 2025
    156  Views
    salesforce service cloud

    What is Salesforce Service Cloud and What are its Features?

    Blog in Salesforce Cloud Platform

    This area of the Salesforce platform is dedicated to giving clients assistance and support. Customers are more satisfied and loyal as a result, which aids…

    Automated Workflows, Business Objectives, Call Centre Teams, Chat Service, Client Retention
    Romil Jul 20, 2022
    3,628  Views
    Footer Forcetalks logo

    support@forcetalks.com

    • twitterx

    Quick Links

    Advertise with Us

    Salesforce® Articles

    Dreamforce 2023

    Top Salesforce® Bloggers 2023

    Top Salesforce Consultants

    Get Listed

    Company

    Contact Us

    About Us

    Privacy Policy

    Terms & Conditions

    InsightHub

    Salesforce Blogs

    Salesforce Videos

    Salesforce Groups

    Salesforce Jobs

    © 2026 - Forcetalks ● All Rights Reserved

    Salesforce® is a trademark of Salesforce® Inc. No claim is made to the exclusive right to use “Salesforce”. Any services offered within the Forcetalks website/app are not sponsored or endorsed by Salesforce®.

    Try AuditMyCRM - It is a Salesforce CRM Audit tool which comprehensively scans your Salesforce org and gives you the list of errors or warnings you need to take care of.
    We use cookies to enhance your browsing experience. Please see our privacy policy if you'd like more information on our use of cookies.