Composite API

Execute multiple operations in a single API call using Composite requests.

Composite Request

Go
resp, err := client.Composite().Execute(ctx, composite.Request{
    AllOrNone: true,  // Rollback all if any fails
    CompositeRequest: []composite.Subrequest{
        {
            Method:      "POST",
            URL:         "/services/data/v59.0/sobjects/Account",
            ReferenceId: "newAccount",
            Body:        map[string]interface{}{"Name": "New Account"},
        },
        {
            Method:      "POST",
            URL:         "/services/data/v59.0/sobjects/Contact",
            ReferenceId: "newContact",
            Body: map[string]interface{}{
                "LastName":  "Smith",
                "AccountId": "@{newAccount.id}",  // Reference previous result
            },
        },
    },
})

for _, result := range resp.CompositeResponse {
    fmt.Printf("%s: %d\n", result.ReferenceId, result.HTTPStatusCode)
}

SObject Tree (Create Related Records)

Go
resp, err := client.Composite().CreateTree(ctx, "Account", composite.TreeRequest{
    Records: []composite.TreeRecord{
        {
            Attributes:  composite.RecordAttributes{Type: "Account", ReferenceId: "acc1"},
            Name:        "Parent Account",
            Contacts: &composite.RelatedRecords{
                Records: []interface{}{
                    map[string]interface{}{
                        "attributes": map[string]string{"type": "Contact", "referenceId": "cont1"},
                        "LastName":   "Smith",
                    },
                },
            },
        },
    },
})

Composite Graph

Go
resp, err := client.Composite().ExecuteGraph(ctx, composite.GraphRequest{
    Graphs: []composite.Graph{
        {
            GraphId: "graph1",
            CompositeRequest: []composite.Subrequest{
                {Method: "POST", URL: "/sobjects/Account", ReferenceId: "newAcc",
                 Body: map[string]interface{}{"Name": "Graph Account"}},
            },
        },
    },
})

SObject Collections

Go
// Create multiple records at once
results, err := client.Composite().CreateCollection(ctx, composite.CollectionRequest{
    AllOrNone: false,
    Records: []map[string]interface{}{
        {"attributes": map[string]string{"type": "Account"}, "Name": "Account 1"},
        {"attributes": map[string]string{"type": "Account"}, "Name": "Account 2"},
    },
})

// Update multiple records
results, _ := client.Composite().UpdateCollection(ctx, req)

// Delete multiple records
results, _ := client.Composite().DeleteCollection(ctx, []string{id1, id2, id3})

Batch Request

Go
resp, err := client.Composite().ExecuteBatch(ctx, composite.BatchRequest{
    HaltOnError: false,
    BatchRequests: []composite.BatchSubrequest{
        {Method: "GET", URL: "v59.0/sobjects/Account/001xx000"},
        {Method: "GET", URL: "v59.0/sobjects/Contact/003xx000"},
    },
})

Available Methods

Method Description
Execute() Execute composite request
ExecuteBatch() Execute batch request
CreateTree() Create records with relationships
ExecuteGraph() Execute composite graph
CreateCollection() Create multiple records
UpdateCollection() Update multiple records
DeleteCollection() Delete multiple records
GetCollection() Get multiple records by ID