How to create and retrieve metadata using SOAP API

Nov 07, 2023


 

 

After completing this unit, you will able to:

 

Introduction

Salesforce is a powerful CRM platform that allows organizations to manage their customer relationships, sales, and other business processes efficiently. While Salesforce provides a wide range of standard objects and fields to work with, you may need to create custom objects and custom fields to meet your specific business requirements. In this blog post, we will explore how to create custom objects and custom fields in Salesforce using the SOAP API and XML.

 

Retrieve All Fields From the Object

 

Endpoint: Salesforce_Base_URL/services/Soap/m/58.0

Method: POST

Header:

Content-Type text/XML
SOAPAction ""

 

Request Body:

<?xml version="1.0" encoding="UTF-8"?>

<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">

    <soapenv:Header

        xmlns="http://soap.sforce.com/2006/04/metadata">

        <SessionHeader>

<sessionId>Access Token/Session Id</sessionId>

        </SessionHeader>

    </soapenv:Header>

    <soapenv:Body

        xmlns="http://soap.sforce.com/2006/04/metadata">

        <readMetadata>

            <type>CustomObject</type>

            <fullNames>Object Name</fullNames>

        </readMetadata>

    </soapenv:Body>

</soapenv:Envelope>

 

 

Response Body:

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope
    xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns="http://soap.sforce.com/2006/04/metadata"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <soapenv:Body>
        <readMetadataResponse>
            <result>
                <records xsi:type="CustomObject">
                    <fullName>Contact</fullName>
                    <fields>
                        <fullName>AccountId</fullName>
                        <trackFeedHistory>true</trackFeedHistory>
                        <type>Lookup</type>
                    </fields>
                    <fields>
                        <fullName>AssistantName</fullName>
                        <trackFeedHistory>false</trackFeedHistory>
                    </fields>
                    <fields>
                        <fullName>AssistantPhone</fullName>
                        <trackFeedHistory>false</trackFeedHistory>
                    </fields>
                    <fields>
                        <fullName>Birthdate</fullName>
                        <trackFeedHistory>false</trackFeedHistory>
                    </fields>
                    <fields>
                        <fullName>CleanStatus</fullName>
                        <trackFeedHistory>false</trackFeedHistory>
                    </fields>
                    <fields>
                        .......                    
                    </fields>
                </records>
            </result>
        </readMetadataResponse>
    </soapenv:Body>
</soapenv:Envelope>

 

 

Retrieve the Specific Field Details of an object

 

Endpoint: Salesforce_Base_URL/services/Soap/m/58.0

Method: POST

Header:

Content-Type

text/XML

SOAPAction

“”

 

Request Body:

<?xml version="1.0" encoding="UTF-8"?>

<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">

    <soapenv:Header

        xmlns="http://soap.sforce.com/2006/04/metadata">

        <SessionHeader>

            <sessionId></sessionId>

        </SessionHeader>

    </soapenv:Header>

    <soapenv:Body

        xmlns="http://soap.sforce.com/2006/04/metadata">

        <readMetadata>

            <type>CustomField</type>

            <fullNames>Student__c.Roll_Number__c</fullNames>

        </readMetadata>

    </soapenv:Body>

</soapenv:Envelope>

 

 

 

Response Body:

<?xml version="1.0" encoding="UTF-8"?>

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns="http://soap.sforce.com/2006/04/metadata" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <soapenv:Body>

        <readMetadataResponse>

            <result>

                <records xsi:type="CustomField">

                    <fullName>Student__c.Roll_Number__c</fullName>

                    <externalId>false</externalId>

                    <label>Roll Number</label>

                    <precision>18</precision>

                    <required>false</required>

                    <scale>0</scale>

                    <trackTrending>false</trackTrending>

                    <type>Number</type>

                    <unique>false</unique>

                </records>

            </result>

        </readMetadataResponse>

    </soapenv:Body>

</soapenv:Envelope>

 

 

 

Retrieve All or Multiple Object at a time

 

Endpoint: Salesforce_Base_URL/services/Soap/m/58.0

Method: POST

Header:

 Content-Type  text/XML
 SOAPAction  “”

 

Request Body:

<?xml version="1.0" encoding="UTF-8"?>

<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">

    <soapenv:Header

        xmlns="http://soap.sforce.com/2006/04/metadata">

        <SessionHeader>

            <sessionId>Access Token/SessionId</sessionId>

        </SessionHeader>

    </soapenv:Header>

    <soapenv:Body

        xmlns="http://soap.sforce.com/2006/04/metadata">

        <readMetadata>

            <type>CustomObject</type>

            <fullNames>Contact</fullNames>

            <fullNames>Account</fullNames>

            <fullNames>Opportunity</fullNames>

        </readMetadata>

    </soapenv:Body>

</soapenv:Envelope>

 

 

 

Response body:

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns="http://soap.sforce.com/2006/04/metadata" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <soapenv:Body>
        <readMetadataResponse>
            <result>
                <records xsi:type="CustomObject">
                    <fullName>Contact</fullName>                    
                    <compactLayoutAssignment>SYSTEM</compactLayoutAssignment>
                    <enableFeeds>true</enableFeeds>
                    <enableHistory>false</enableHistory>
                    <externalSharingModel>ControlledByParent</externalSharingModel>
                    <fields>
                        <fullName>AccountId</fullName>
                        <trackFeedHistory>true</trackFeedHistory>
                        <type>Lookup</type>
                    </fields>
                    <fields>
                        <fullName>AssistantName</fullName>
                        <trackFeedHistory>false</trackFeedHistory>
                    </fields>                    
                    <label>Contact</label>
                    <listViews>
                        <fullName>AllContacts</fullName>
                        <filterScope>Everything</filterScope>
                        <label>All Contacts</label>
                    </listViews>                    
                    <searchLayouts>
                        <customTabListAdditionalFields>FULL_NAME</customTabListAdditionalFields>
                        <customTabListAdditionalFields>ACCOUNT.NAME</customTabListAdditionalFields>
                    </searchLayouts>
                    <sharingModel>ControlledByParent</sharingModel>
                </records>
                <records xsi:type="CustomObject">
                    <fullName>Account</fullName>                    
                    <compactLayoutAssignment>SYSTEM</compactLayoutAssignment>
                    <enableFeeds>true</enableFeeds>
                    <enableHistory>false</enableHistory>
                    <externalSharingModel>Private</externalSharingModel>
                    <fields>
                        <fullName>AccountNumber</fullName>
                        <trackFeedHistory>false</trackFeedHistory>
                    </fields>
                    <fields>
                        <fullName>AccountSource</fullName>
                        <trackFeedHistory>false</trackFeedHistory>
                        <type>Picklist</type>
                    </fields>
                    <label>Account</label>
                    <listViews>
                        <fullName>AllAccounts</fullName>
                        <filterScope>Everything</filterScope>
                        <label>All Accounts</label>
                    </listViews>
                    <listViews>
                        <fullName>MyAccounts</fullName>
                        <filterScope>Mine</filterScope>
                        <label>My Accounts</label>
                    </listViews>
                    <listViews>
                        <fullName>NewLastWeek</fullName>
                        <filterScope>Everything</filterScope>
                        <filters>
                            <field>ACCOUNT.CREATED_DATE</field>
                            <operation>equals</operation>
                            <value>LAST_WEEK</value>
                        </filters>
                        <label>New Last Week</label>
                    </listViews>
                    <searchLayouts>
                        <customTabListAdditionalFields>ACCOUNT.NAME</customTabListAdditionalFields>
                        <customTabListAdditionalFields>ACCOUNT.ADDRESS1_CITY</customTabListAdditionalFields>
                    </searchLayouts>
                    <sharingModel>ReadWrite</sharingModel>
                    <webLinks>
                        <fullName>Billing</fullName>
                        <availability>online</availability>
                        <url>http://www.genwatt.com/genwatt/billing.htm?actname={!Account_Name}</url>
                    </webLinks>
                </records>
                <records xsi:type="CustomObject">
                    <fullName>Opportunity</fullName>
                    <compactLayoutAssignment>SYSTEM</compactLayoutAssignment>
                    <enableFeeds>true</enableFeeds>
                    <enableHistory>false</enableHistory>
                    <externalSharingModel>Private</externalSharingModel>
                    <fields>
                        <fullName>AccountId</fullName>
                        <trackFeedHistory>false</trackFeedHistory>
                        <trackTrending>false</trackTrending>
                        <type>Lookup</type>
                    </fields>
                    <fields>
                        <fullName>Amount</fullName>
                        <trackFeedHistory>true</trackFeedHistory>
                        <trackTrending>false</trackTrending>
                    </fields>                    
                    <label>Opportunity</label>
                    <listViews>
                        <fullName>AllOpportunities</fullName>
                        <filterScope>Everything</filterScope>
                        <label>All Opportunities</label>
                    </listViews>                
                    <searchLayouts>
                        <customTabListAdditionalFields>OPPORTUNITY.NAME</customTabListAdditionalFields>
                        <customTabListAdditionalFields>ACCOUNT.NAME</customTabListAdditionalFields>                        
                    </searchLayouts>
                    <sharingModel>ReadWrite</sharingModel>
                    <webLinks>
                        <fullName>DeliveryStatus</fullName>
                        <availability>online</availability>
                        <url>http://www.fedex.com/cgi-bin/tracking?tracknumbers={!Opportunity_Tracking_Number}&amp;action=track&amp;language=english&amp;cntry_code=us</url>
                    </webLinks>
                </records>
            </result>
        </readMetadataResponse>
    </soapenv:Body>
</soapenv:Envelope>

 

 

 

Create Custom Object in Salesforce Using SOAP API

 

Endpoint: Salesforce_Base_URL/services/Soap/m/58.0

Method: POST

Header:

Content-Type text/XML
SOAPAction “”

 

Request Body:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Header>
    <h:SessionHeaderxmlns:h="http://soap.sforce.com/2006/04/metadata"
      xmlns="http://soap.sforce.com/2006/04/metadata"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <sessionId></sessionId>
    </h:SessionHeader>
  </s:Header>
  <s:Bodyxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <createMetadata xmlns="http://soap.sforce.com/2006/04/metadata">
      <metadata xsi:type="CustomObject">
        <fullName>Student__c</fullName>
        <label>Student</label>
        <pluralLabel>Student</pluralLabel>
        <deploymentStatus>Deployed</deploymentStatus>        
        <nameField>
            <label>Student Name</label>
            <type>Text</type>
        </nameField>
        <sharingModel>ReadWrite</sharingModel>
      </metadata>
    </createMetadata>
  </s:Body>
</s:Envelope>

 

Response Body:

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns="http://soap.sforce.com/2006/04/metadata">
    <soapenv:Body>
        <createMetadataResponse>
            <result>
                <fullName>Student__c</fullName>
                <success>true</success>
            </result>
        </createMetadataResponse>
    </soapenv:Body>
</soapenv:Envelope>

 

 

Create Custom Field in Salesforce Using SOAP API

 

Endpoint: Salesforce_Base_URL/services/Soap/m/58.0

Method: POST

Header:

Content-Type

text/XML

SOAPAction

“”

 

Request Body:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Header>
    <h:SessionHeaderxmlns:h="http://soap.sforce.com/2006/04/metadata"
      xmlns="http://soap.sforce.com/2006/04/metadata"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <sessionId></sessionId>
    </h:SessionHeader>
  </s:Header>
  <s:Bodyxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <createMetadata xmlns="http://soap.sforce.com/2006/04/metadata">
      <metadata xsi:type="CustomField">
        <fullName>Student__c.Roll_Number__c</fullName>
        <label>Roll Number</label>
        <type>Text</type>
        <length>60</length>
      </metadata>
    </createMetadata>
  </s:Body>
</s:Envelope>
 
Response Body:
 
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns="http://soap.sforce.com/2006/04/metadata">
    <soapenv:Body>
        <createMetadataResponse>
            <result>
                <fullName>Student__c.Roll_Number__c</fullName>
                <success>true</success>
            </result>
        </createMetadataResponse>
    </soapenv:Body>
</soapenv:Envelope>

 

I hope this blog helped you!