Scripts.CreateTriggerStreamAsync Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Creates a trigger as an asynchronous operation in the Azure Cosmos DB service.
public abstract System.Threading.Tasks.Task<Microsoft.Azure.Cosmos.ResponseMessage> CreateTriggerStreamAsync(Microsoft.Azure.Cosmos.Scripts.TriggerProperties triggerProperties, Microsoft.Azure.Cosmos.RequestOptions requestOptions = default, System.Threading.CancellationToken cancellationToken = default);
abstract member CreateTriggerStreamAsync : Microsoft.Azure.Cosmos.Scripts.TriggerProperties * Microsoft.Azure.Cosmos.RequestOptions * System.Threading.CancellationToken -> System.Threading.Tasks.Task<Microsoft.Azure.Cosmos.ResponseMessage>
Public MustOverride Function CreateTriggerStreamAsync (triggerProperties As TriggerProperties, Optional requestOptions As RequestOptions = Nothing, Optional cancellationToken As CancellationToken = Nothing) As Task(Of ResponseMessage)
Parameters
- triggerProperties
- TriggerProperties
The TriggerProperties object.
- requestOptions
- RequestOptions
- cancellationToken
- CancellationToken
Returns
A task object representing the service response for the asynchronous operation.
Examples
This creates a trigger then uses the trigger in a create item.
Scripts scripts = this.container.Scripts;
ResponseMessage triggerResponse = await scripts.CreateTriggerStreamAsync(
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
});
if (!triggerResponse.IsSuccessStatusCode)
{
//Handle and log exception
return;
}
ItemRequestOptions options = new ItemRequestOptions()
{
PreTriggers = new List<string>() { "addTax" },
};
// 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;