Share via


Scripts.CreateUserDefinedFunctionAsync Method

Definition

Creates a user defined function as an asynchronous operation in the Azure Cosmos DB service.

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

Parameters

userDefinedFunctionProperties
UserDefinedFunctionProperties

The UserDefinedFunctionProperties object.

requestOptions
RequestOptions

(Optional) The options for the user defined function request.

cancellationToken
CancellationToken

(Optional) CancellationToken representing request cancellation.

Returns

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

Exceptions

If userDefinedFunctionProperties 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 user defined function 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 user defined function or that the Body was malformed.
403Forbidden - You have reached your quota of user defined functions for the collection supplied. Contact support to have this quota increased.
409Conflict - This means a UserDefinedFunctionProperties with an id matching the id you supplied already existed.
413RequestEntityTooLarge - This means the body of the UserDefinedFunctionProperties you tried to create was too large.

Examples

This creates a user defined function then uses the function in an item query.

Scripts scripts = this.container.Scripts;
await scripts.UserDefinedFunctions.CreateUserDefinedFunctionAsync(
    new UserDefinedFunctionProperties 
    { 
        Id = "calculateTax", 
        Body = @"function(amt) { return amt * 0.05; }" 
    });

QueryDefinition sqlQuery = new QueryDefinition(
    "SELECT VALUE udf.calculateTax(t.cost) FROM toDoActivity t where t.cost > @expensive and t.status = @status")
    .WithParameter("@expensive", 9000)
    .WithParameter("@status", "Done");

using (FeedIterator<double> setIterator = this.container.Items.GetItemsQueryIterator<double>(
    sqlQueryDefinition: sqlQuery,
    partitionKey: "Done")
{
    while (setIterator.HasMoreResults)
    {
        foreach (var tax in await setIterator.ReadNextAsync())
        {
            Console.WriteLine(tax);
        }
    }
}

Applies to