Salesforce Metadata API - Objects, Record Types & Metadata API Tips

By Laura Wyse in GET/technical Posted Apr 29, 2015

Salesforce allows users to do a lot of customization with custom objects, custom fields, validation rules, multiple record types per object etc.

Salesforce Metadata API

You can make changes to a lot of these things through the API by using the Salesforce Metadata API. The metadata API allows you to create, update or delete metadata. For example, you could update an existing object in Salesforce by adding new fields, updating the values in a picklist or updating or creating a new Record Type.

HERE ARE SOME TRICKS FOR USING THE METADATA API:

1. The metadata API is a SOAP API, and unlike the standard Salesforce API, there is no REST version.

2. The object structure can be a bit complex, depending on what type of metadata you are working with.

For example, if you are updating or creating metadata on a custom object you need to build that object using the CustomObject class. This object can contain an array of CustomField objects which has a FieldType object for the type and if that type is a pick-list, it then contains a Picklist object, which in turn contains an array of PicklistValue objects. A CustomObject can also contain RecordType objects, which in turn contain a list of RecordTypePicklistValue objects… (Okay, you get the point.) Many of these fields and settings on the object are their own classes, instead of just standard Java objects or primitive types.

3. Record Types can be a bit tricky.

“Record types allow you to offer different business processes, picklist values, and page layouts to different users based on their profiles.” [1] They allow you to have customized settings on an object on different profiles or based on certain settings. For example you could have two record types – one which you allow updates on and then when the user checks a “submitted” flag you could change it to a second record type which does not allow updates and essentially locks the record. You could also use a record type to display a different set of picklist values for one field depending on what profile that user belongs to. When updating metadata you need to specify what picklist values you want to be present in a record type, otherwise they could end up being empty for that specific record type.

4. Beware of default value changes when you update metadata.

If you are updating metadata of an object or record type and build your custom object with the name, label and updated fields you are probably not setting every field possible on that object. When you update the metadata Salesforce resets other fields to the default value – so if you need “enableReports” to be active on your object, you better make sure you set it when updating the metadata so it is not overridden by the default value in Salesforce. An important field to note is that if you are updating record types and you want them to be “active” you should make sure you set the active field to true. By default Salesforce sets the first record type created to true and any thereafter to inactive.

For more tips and tricks for integrating the Salesforce API, download our API Cheat Sheet! 

Salesforce  Cheat Sheet