# Chain Mutations by Using Field References

| AVAILABLE API VERSION               |
| ----------------------------------- |
| Beta v59.0–65.0                     |
| Generally available v66.0 and later |

Create a mutation request with an operation that references another operation by using field references. You can create a record and a parent or child record in the same transaction. You can also chain operations such as creating a user and then creating an account whose owner is the newly created user.

When working with multiple operations in a single mutation request, consider setting the [`allOrNone` boolean](/docs/platform/graphql/guide/mutations-use.md) to `false` so that some operations successfully execute even if there are independent unsuccessful operations.

## Chain Operations with Multiple Field Reference Patterns

There are multiple ways to reference fields in chained mutations. Assuming `ref` is the name or alias of a previous mutation operation from the same request, supported field reference patterns are:

- `@{ref.Record.Id}` - Explicit record ID reference. Available in API v67.0 and later.
- `@{ref.Record.FieldName.value}` - GraphQL field reference. For example, `Name { value }` in a GraphQL query is `@{ref.Record.Name.value}`. Available in API v67.0 and later.
- `@{ref}` - `IdOrRef` scalar reference that accepts a record ID value or the name or alias of another operation. Available in API v59.0 and later.

:::note
When chaining operations, place an operation after the operation it references. If you order an operation that references another operation first, the request fails because the operation can’t find the referenced operation.
:::

## Example: Chain Account, Contact, and Opportunity Creation

This example chains the creation of an account, a contact, and an opportunity in a single request and uses multiple field reference patterns. The child contact creation requires the `AccountId` field value from the account creation operation. The contact create operation passes in `@{AccountCreate}` to the `Contact.AccountId` field to reference the `Id` field on the newly created account. The contact creation also requires the `LastName` field value from the account creation operation. The contact create operation passes in `@{AccountCreate.Record.Name.value}` to the `Contact.LastName` field. The opportunity creation also requires the `AccountId` field value from the account creation operation. In this example, the opportunity create operation passes in `@{AccountCreate.Record.Id}` to reference the `Id` of the record explicitly.

```sfdocs-code {"lang":"graphql", "title": "Chained Account, Contact, and Opportunity Creation", "src": "" }
mutation CreateAccountAndContact {
  uiapi(input: { allOrNone: false }) {
    AccountCreate(input: {
      Account: {
        Name: "Mutation Fun"
      }
    }) {
      Record {
        Id                              # referenced in 'OpportunityCreate'
        Name { value }                  # referenced in 'ContactCreate'
      }
    }
    ContactCreate(input: {
      Contact: {
        MailingCity: "Paris"
        LastName: "@{AccountCreate.Record.Name.value}"
        AccountId: "@{AccountCreate}"   # equivalent to AccountCreate.Record.Id
      }
    }) {
      Record {
        Id
        LastName { value }              # referenced in 'OpportunityCreate'
        MailingCity { value }           # referenced in 'OpportunityCreate'
      }
    }
    OpportunityCreate(input: {
      Opportunity: {
        Name: "@{ContactCreate.Record.LastName.value}"            # 'ContactCreate' reference
        Description: "@{ContactCreate.Record.MailingCity.value}"  # 'ContactCreate' reference
        AccountId: "@{AccountCreate.Record.Id}"                   # 'AccountCreate' reference
        StageName: "Value Proposition"
        CloseDate: "2026-12-31"
      }
    }) {
      Record {
        Id
        AccountId { value }
        Name { value }
        Description { value }
      }
    }
  }
}
```

The response looks like this.

```sfdocs-code {"lang":"graphql", "title": "Chained Account, Contact, and Opportunity Creation Response", "src": "" }
{
  "data": {
    "uiapi": {
      "AccountCreate": {
        "Record": {
          "Id": "001LT000011naPRYAY",
          "Name": {
            "value": "Mutation Fun"
          }
        }
      },
      "ContactCreate": {
        "Record": {
          "Id": "003LT00000ArlizYAB",
          "LastName": {
            "value": "Mutation Fun"
          },
          "MailingCity": {
            "value": "Paris"
          }
        }
      },
      "OpportunityCreate": {
        "Record": {
          "Id": "006LT000005R4HZYA0",
          "AccountId": {
            "value": "001LT000011naPRYAY"
          },
          "Name": {
            "value": "Mutation Fun"
          },
          "Description": {
            "value": "Paris"
          }
        }
      }
    }
  },
  "errors": [],
  "extensions": {}
}
```

## Example: Chain Contact Update and Opportunity Creation

This example updates a contact and then creates an opportunity for the related account with the contact’s name.

```sfdocs-code {"lang":"graphql", "title": "Chained Contact Update and Opportunity Creation", "src": "" }
mutation UpdateContactAndCreateOpportunity{
  uiapi {
    contact: ContactUpdate(input: {
      Contact: {
        MailingCity: "Paris"
      },
      Id: "003xx000004WjJEAA0"
    }) {
      Record {
        Id
        Name { value }                  # referenced in 'oppty'
        AccountId { value }             # referenced in 'oppty'
      }
    }
    oppty: OpportunityCreate(input: {
      Opportunity: {
        AccountId: "@{contact.Record.AccountId.value}"  # 'contact' field reference
        ContactId: "@{contact.Record.Id}"               # 'contact' field reference
        Name: "@{contact.Record.Name.value}"            # 'contact' field reference
        StageName: "Value Proposition"
        CloseDate: "2026-12-31"
      }
    }) {
      Record {
        Id
        AccountId { value }
        ContactId { value }
        Name { value }
        Description { value }
      }
    }
  }
}
```

The response looks like this.

```sfdocs-code {"lang":"graphql", "title": "Chained Contact Update and Opportunity Creation Response", "src": "" }
{
  "data": {
    "uiapi": {
      "contact": {
        "Record": {
          "Id": "003LT00000ArlizYAB",
          "Name": {
            "value": "Mutation Fun"
          },
          "AccountId": {
            "value": "001LT000011naPRYAY"
          }
        }
      },
      "oppty": {
        "Record": {
          "Id": "006LT000005R5FFYA0",
          "AccountId": {
            "value": "001LT000011naPRYAY"
          },
          "ContactId": {
            "value": "003LT00000ArlizYAB"
          },
          "Name": {
            "value": "Mutation Fun"
          },
          "Description": null
        }
      }
    }
  },
  "errors": [],
  "extensions": {}
}
```
