Share via


Scripts.CreateTriggerAsync Method

Definition

Creates a trigger as an asynchronous operation in the Azure Cosmos DB service.

public abstract System.Threading.Tasks.Task<Microsoft.Azure.Cosmos.Scripts.TriggerResponse> CreateTriggerAsync (Microsoft.Azure.Cosmos.Scripts.TriggerProperties triggerProperties, Microsoft.Azure.Cosmos.RequestOptions requestOptions = default, System.Threading.CancellationToken cancellationToken = default);
abstract member CreateTriggerAsync : Microsoft.Azure.Cosmos.Scripts.TriggerProperties * Microsoft.Azure.Cosmos.RequestOptions * System.Threading.CancellationToken -> System.Threading.Tasks.Task<Microsoft.Azure.Cosmos.Scripts.TriggerResponse>
Public MustOverride Function CreateTriggerAsync (triggerProperties As TriggerProperties, Optional requestOptions As RequestOptions = Nothing, Optional cancellationToken As CancellationToken = Nothing) As Task(Of TriggerResponse)

Parameters

triggerProperties
TriggerProperties

The TriggerProperties object.

requestOptions
RequestOptions

(Optional) The options for the stored procedure request.

cancellationToken
CancellationToken

(Optional) CancellationToken representing request cancellation.

Returns

A task object representing the service response for the asynchronous operation.

Exceptions

If triggerProperties is not set.

Represents a consolidation of failures that occurred during async processing. Look within InnerExceptions to find the actual exception(s)

This exception can encapsulate many different types of errors. To determine the specific error always look at the StatusCode property. Some common codes you may get when creating a Document are:

StatusCodeReason for exception
400BadRequest - This means something was wrong with the request supplied. It is likely that an Id was not supplied for the new trigger or that the Body was malformed.
403Forbidden - You have reached your quota of triggers for the collection supplied. Contact support to have this quota increased.
409Conflict - This means a TriggerProperties with an id matching the id you supplied already existed.
413RequestEntityTooLarge - This means the body of the TriggerProperties you tried to create was too large.

Examples

This creates a trigger then uses the trigger in a create item.

Scripts scripts = this.container.Scripts;
TriggerResponse triggerResponse = await scripts.CreateTriggerAsync(
    new TriggerProperties
    {
        Id = "addTax",
        Body = @"function AddTax() {
            var item = getContext().getRequest().getBody();

            // calculate the tax.
            item.tax = item.cost * .15;

            // Update the request -- this is what is going to be inserted.
            getContext().getRequest().setBody(item);
        }",
        TriggerOperation = TriggerOperation.All,
        TriggerType = TriggerType.Pre
    });

ItemRequestOptions options = new ItemRequestOptions()
{
    PreTriggers = new List<string>() { triggerResponse.Id },
};

// Create a new item with trigger set in the request options
ItemResponse<dynamic> createdItem = await this.container.Items.CreateItemAsync<dynamic>(item.status, item, options);
double itemTax = createdItem.Resource.tax;

Applies to