Newer Version Available

This content describes an older version of this product. View Latest

AllOrNoneHeader

Indicates whether to roll back all metadata changes when some of the records in a call result in failures.

Version

This header is available in API version 34.0 and later.

Usage

If this header isn’t used in API version 34.0 and later, by default a call can save a partial set of records (equivalent to AllOrNoneHeader=false)—the records that are processed successfully are saved and records that have failures aren’t saved.

Fields

Field Name Type Description
allOrNone boolean Set to true to cause all metadata changes to be rolled back if any records in the call cause failures. Set to false to enable saving only the records that are processed successfully when other records in the call cause failures.

Sample Code—Java

Add the AllOrNoneHeader to the metadata connection before you perform a call as follows:

1metadataConnection.setAllOrNoneHeader(true);

This next example shows how to use the AllOrNoneHeader when creating two custom objects. Because the second custom object doesn’t have the required Name field, the create() call can’t create this custom object and rolls back the first custom object. The output is shown after this code sample.

1import com.sforce.soap.metadata.*;
2import com.sforce.soap.metadata.Error;
3import com.sforce.ws.ConnectionException;
4
5public class CallWithHeader {
6	
7    MetadataConnection metadataConnection = null;
8    
9    public static void main(String[] args) throws ConnectionException {
10    	CallWithHeader samples = new CallWithHeader();
11        samples.createWithHeader();
12    }
13
14    public CallWithHeader() throws ConnectionException {
15        metadataConnection = MetadataLoginUtil.login();
16    }
17    
18    public void createWithHeader() throws ConnectionException {
19        // Define two custom objects to be inserted.
20        CustomObject co1 = new CustomObject();
21        String name1 = "MyCustomObject1";
22        co1.setFullName(name1 + "__c");
23        co1.setDeploymentStatus(DeploymentStatus.Deployed);
24        co1.setDescription("Created by the Metadata API");
25        co1.setEnableActivities(true);
26        co1.setLabel(name1 + " Object");
27        co1.setPluralLabel(co1.getLabel() + "s");
28        co1.setSharingModel(SharingModel.ReadWrite);
29
30        CustomField nf = new CustomField();
31        nf.setType(FieldType.Text);
32        nf.setLabel(co1.getFullName() + " Name");
33        co1.setNameField(nf);
34        
35        // The second custom object doesn't have a Name field
36        CustomObject co2 = new CustomObject();
37        String name2 = "MyCustomObject2";
38        co2.setFullName(name2 + "__c");
39        co2.setDeploymentStatus(DeploymentStatus.Deployed);
40        co2.setDescription("Created by the Metadata API");
41        co2.setEnableActivities(true);
42        co2.setLabel(name2 + " Object");
43        co2.setPluralLabel(co2.getLabel() + "s");
44        co2.setSharingModel(SharingModel.ReadWrite);
45
46        
47        // Setting the allOrNone header to true to cause
48        //  the call to not commit any record if one or more 
49        //  records in this call have failures.
50        metadataConnection.setAllOrNoneHeader(true);
51
52        // Now that the header has been set, make the create call.
53        SaveResult[] results = metadataConnection
54                .createMetadata(new Metadata[] { co1, co2 });
55
56        // Iterate through the call results
57        for (SaveResult r : results) {
58            if (r.isSuccess()) {
59                System.out.println("Created component: " + r.getFullName());
60            } else {
61                System.out
62                        .println("Errors were encountered while creating "
63                                + r.getFullName());
64                for (Error e : r.getErrors()) {
65                    System.out.println("Error message: " + e.getMessage());
66                    System.out.println("Status code: " + e.getStatusCode());
67                }
68            }
69        }
70    }
71
72}

This is the output that the sample returns. The first record is rolled back and the second has a failure.

1Errors were encountered while creating MyCustomObject1__c
2Error message: Record rolled back because not all records were valid and the request was using AllOrNone header
3Status code: ALL_OR_NONE_OPERATION_ROLLED_BACK
4Errors were encountered while creating MyCustomObject2__c
5Error message: Must specify a nameField of type Text or AutoNumber
6Status code: FIELD_INTEGRITY_EXCEPTION