function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ted.TsungTed.Tsung 

Metadata API custom field create example

Is there a example of how to add a custom field to an existing custom object? In the sample script, it only shows how to create a custom object.
Fiaz AhmadFiaz Ahmad

I am also facing the same problem. If anyone have code please help me.

Apex LearnerApex Learner
public class soap_envelope 
{

public void update_picklist (string session_id, string client_id, string current_object , string current_field, string new_value){
string SoapBody = '<?xml version="1.0" encoding="UTF-8"?>';
SoapBody += '<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">';
SoapBody += '<S:Header>';
SoapBody += '<SessionHeader xmlns="http://soap.sforce.com/2006/04/metadata"> ';
SoapBody += '<sessionId>'+session_id+'</sessionId>';

SoapBody += '</SessionHeader>';
SoapBody += '<CallOptions xmlns="http://soap.sforce.com/2006/04/metadata">';
SoapBody += '<client>'+client_id+'</client>';
SoapBody += '</CallOptions>';
SoapBody += '</S:Header>';
SoapBody += '<S:Body>';

SoapBody += '<update xmlns="http://soap.sforce.com/2006/04/metadata">';
SoapBody += '<!--Zero or more repetitions:-->';
SoapBody += '<UpdateMetadata xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Customfield">';
SoapBody += '<currentName>'+current_object+'.'+current_field+'</currentName>';

SoapBody += '<metadata xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="CustomField">';
SoapBody += '<!--Optional:-->';
SoapBody += '<fullName>'+current_object+'.'+current_field+'</fullName>';
if (custom =='true') {SoapBody += '<label>number</label>';}


SoapBody += '<picklist>';


SoapBody += '<picklistValues>';
SoapBody += '<fullName>'+new_value+'</fullName>';

SoapBody += '</picklistValues>';
SoapBody += '<sorted>false</sorted>';
SoapBody += '</picklist>';
SoapBody += '<type>Picklist</type>';


SoapBody += '</metadata>';
SoapBody += '</UpdateMetadata>';
SoapBody += '</update>';
SoapBody += '</S:Body>';
SoapBody += '</S:Envelope>';


system.debug('soap envelope============================================='+SoapBody );


HttpRequest req= new HttpRequest(); 
req.setEndpoint('https://ap1-api.salesforce.com/services/Soap/m/25.0'); 
req.setMethod('POST'); 
req.setHeader('content-type', 'text/xml; charset=utf-8'); 
req.setHeader('SOAPAction', 'https://ap1-api.salesforce.com/services/Soap/m/25.0' );
req.setBody(SoapBody);
Http ht = new Http(); 
Httpresponse response = new Httpresponse();
response = ht.send(req);

system.debug('-----------------------------'+response);
// return 'success';
}









}

 Call this class and method eith parameters , you will add new values in picklist field of current object 

Apex LearnerApex Learner

Update version with controlloing field 

 

 

public with sharing class soap_envelope1 {
	
	public void update_picklist (string session_id, string client_id, string current_object , string current_field, string new_value, string is_custom, string label, string controlling_field, string controlling_field_value){
    string SoapBody = '<?xml version="1.0" encoding="UTF-8"?>';
    SoapBody += '<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">';
    SoapBody += '<S:Header>';
     SoapBody += '<SessionHeader xmlns="http://soap.sforce.com/2006/04/metadata"> ';
     SoapBody += '<sessionId>'+session_id+'</sessionId>';
    
     SoapBody += '</SessionHeader>';
     SoapBody += '<CallOptions xmlns="http://soap.sforce.com/2006/04/metadata">';
     SoapBody += '<client>'+client_id+'</client>';
     SoapBody += '</CallOptions>';
     SoapBody += '</S:Header>';
     SoapBody += '<S:Body>';
      
      SoapBody += '<update xmlns="http://soap.sforce.com/2006/04/metadata">';
      SoapBody += '<!--Zero or more repetitions:-->';
      SoapBody += '<UpdateMetadata xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Customfield">';
      SoapBody += '<currentName>'+current_object+'.'+current_field+'</currentName>';          
      SoapBody += '<metadata xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="CustomField">';
      SoapBody += '<!--Optional:-->';
      SoapBody += '<fullName>'+current_object+'.'+current_field+'</fullName>';
   if (is_custom =='true') 
   {
      SoapBody += '<label>'+label+'</label>';
   }
      SoapBody += '<picklist>';
                
  if(controlling_field != '-none-')
     {
       soapbody += '<controllingField>'+controlling_field+'</controllingField>';  
     }
       
         SoapBody += '<picklistValues>';
         SoapBody += '<fullName>'+new_value+'</fullName>';
            if (controlling_field_value != '-none-')
            {
              soapbody+= '<controllingFieldValues>'+controlling_field_value+'</controllingFieldValues>';
            }        
          SoapBody += '</picklistValues>';
          SoapBody += '<sorted>false</sorted>';
        SoapBody += '</picklist>';
        SoapBody += '<type>Picklist</type>';
    
                
           SoapBody += '</metadata>';
          SoapBody += '</UpdateMetadata>';
       SoapBody += '</update>';
      SoapBody += '</S:Body>';
    SoapBody += '</S:Envelope>';
    
    
    system.debug('soap envelope============================================='+SoapBody );
    
    
       HttpRequest req= new HttpRequest(); 
            req.setEndpoint('https://ap1-api.salesforce.com/services/Soap/m/25.0'); 
            req.setMethod('POST'); 
            req.setHeader('content-type', 'text/xml; charset=utf-8');  
            req.setHeader('SOAPAction', 'https://ap1-api.salesforce.com/services/Soap/m/25.0' );
            req.setBody(SoapBody);
            Http ht = new Http(); 
            Httpresponse response = new Httpresponse();
            response = ht.send(req);
            
            system.debug('-----------------------------'+response);
           // return 'success';

 }

}