+ Start a Discussion
sfdcsushilsfdcsushil 

Can we use Metadata API within Salesforce?

Is there a option to use Metadata API within Salesforce? If yes, Please give some documentation or relevant code.

 

Thanks.

Best Answer chosen by Admin (Salesforce Developers) 
soofsoof

In apex code?  No!

All Answers

soofsoof

In apex code?  No!

This was selected as the best answer
sfdcsushilsfdcsushil

ok. thanks.

Apex LearnerApex Learner

Yes , 

we can creat soap envelop within  apex code , 

and call it to do changes .

 

I am adding picklist values via visualforce pages 

sfdcsushilsfdcsushil
Hi, Thanks for reply. Can you please give a code snippet. It will help.
Apex LearnerApex Learner
public void update_picklist (string session_id, string client_id, string current_object , string current_field, string new_value, string 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 (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';

 }

 

Pass all valid values in this function  and it will insert new picklist in metadata

 

e.g : do let me know if u need , any help in this