共用方式為


CosmosScripts.CreateTriggerAsync Method

Definition

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

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

Parameters

triggerProperties
TriggerProperties

The TriggerProperties object.

requestOptions
RequestOptions

(Optional) The options for the stored procedure request RequestOptions

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.

CosmosScripts scripts = this.container.Scripts;
Response<TriggerProperties> 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