Newer Version Available
createMetadata()
Syntax
1SaveResult[] = metadatabinding.createMetadata(
2 Metadata[] metadata);Usage
Use the createMetadata() call to create any component that extends Metadata. All components must be of the same type in the same call. For more details, see Metadata Components and Types.
This call executes synchronously, which means that the call returns only when the operation completes.
Starting in API version 34.0, this call supports the AllOrNoneHeader header. By default, if AllOrNoneHeader isn’t used in API version 34.0 and later, this call can save a partial set of records for records with no errors (equivalent to AllOrNoneHeader=false). In API version 33.0 and earlier, the default behavior is to only save all records when there are no failures in any record in the call (equivalent to AllOrNoneHeader=true).
Version
Available in API version 30.0 and later.
Permissions
Your client application must be logged in with the Modify Metadata Through Metadata API Functions or Modify All Data permission.
Required Fields
The metadata components being created determine required fields. For more information about specific component types, see Metadata Components and Types.
Valid Data Values
You must supply values that are valid for the field’s data type, such as integers for integer fields (not alphabetic characters). In your client application, follow the data formatting rules specified for your programming language and development tool. (Your development tool handles the appropriate mapping of data types in SOAP messages.)
String Values
When storing values in string fields, the API trims any leading and trailing whitespace. For example, if the value of a label field is entered as "MyObject ", the value is stored in the database as "MyObject".
Basic Steps for Creating Metadata Components
Follow this process to create metadata components.
- Design an array, and populate it with the components that you want to create. All components must be of the same type.
- Call createMetadata() with the component array passed in as an argument.
- A SaveResult object is returned for each component you tried to create. It contains information about whether the operation was successful, the name of the component created, and any errors returned if the operation wasn’t successful.
Sample Code—Java
1public void createCustomObjectSync() {
2 try {
3 CustomObject co = new CustomObject();
4 String name = "MyCustomObject1";
5 co.setFullName(name + "__c");
6 co.setDeploymentStatus(DeploymentStatus.Deployed);
7 co.setDescription("Created by the Metadata API");
8 co.setEnableActivities(true);
9 co.setLabel(name + " Object");
10 co.setPluralLabel(co.getLabel() + "s");
11 co.setSharingModel(SharingModel.ReadWrite);
12
13 CustomField nf = new CustomField();
14 nf.setType(FieldType.Text);
15 nf.setLabel(co.getFullName() + " Name");
16 co.setNameField(nf);
17
18 SaveResult[] results = metadataConnection
19 .createMetadata(new Metadata[] { co });
20
21 for (SaveResult r : results) {
22 if (r.isSuccess()) {
23 System.out.println("Created component: " + r.getFullName());
24 } else {
25 System.out
26 .println("Errors were encountered while creating "
27 + r.getFullName());
28 for (Error e : r.getErrors()) {
29 System.out.println("Error message: " + e.getMessage());
30 System.out.println("Status code: " + e.getStatusCode());
31 }
32 }
33 }
34 } catch (ConnectionException ce) {
35 ce.printStackTrace();
36 }
37}Arguments
| Name | Type | Description |
|---|---|---|
| metadata | Metadata[] | Array of one or more metadata components. Limit: 10. (For CustomMetadata and CustomApplication only, the limit is 200.) You must submit arrays of only one type of component. For example, you can submit an array of 10 custom objects or 10 profiles, but not a mix of both types. |