Forum Discussion

mmcossu's avatar
mmcossu
Copper Contributor
Mar 14, 2024

Azure Cosmos Db Triggers not working

I have a cosmos db for NoSql instance I'm working on.
I have a collection, let's coll it "my-coll".
I want to add a property in any document being added to the collection.
The https://learn.microsoft.com/en-us/training/modules/expand-query-transaction-functionality-azure-cosmos-db-sql-api/4-add-triggers-operation for the 'pre-trigger' has an example i want to reproduce. It is also inserted in https://learn.microsoft.com/en-us/azure/cosmos-db/nosql/how-to-write-stored-procedures-triggers-udfs?tabs=javascript#pre-triggers.

 

function addLabel() {
    var context = getContext();
    var request = context.getRequest();
    var pendingItem = request.getBody();
    if (!('label' in pendingItem))
        pendingItem['label'] = 'new';
    request.setBody(pendingItem);
}

 

I created my new trigger from the Azure Portal CosmosDB explorer:

  • Trigger Id: createPreTrigger
  • Trigger Type: Pre
  • Trigger Operation: Create

and then pasted the code.

 

I added a new Item from the Explorer. Nothing happened.

I setup a postman collection and invoked the database api with a POST call and sending a payload.
The document is created. The label is not there, meaning the trigger didn't work.

Why is that?
I'm exploring a real world production scenario, i need to save states in the document depending on creation or replacement, since the change feed with the operationType is not yet made public available.
All documentation available is in those 2 links above, sadly and embarrassingly.
I need the triggers to work.

Please do not tell me that Azure Functions are a solution: as stated, they are not, due to the fact that change feed do not distinguish between insertion and replacement.

Thanks

1 Reply

  • Seems the problem is that Cosmos DB triggers are not automatically invoked when using the Azure Portal or REST API, please considering this:

     

    1. Specify the Trigger in Request Options: When you create or update a document, you need to include the trigger in the request options. Here's an example using the .NET SDK:

    dynamic newItem = new { id = "1", name = "Sample Item" };
    Uri collectionUri = UriFactory.CreateDocumentCollectionUri("myDatabase", "my-coll");
    RequestOptions requestOptions = new RequestOptions { PreTriggerInclude = new List<string> { "createPreTrigger" } };
    await client.CreateDocumentAsync(collectionUri, newItem, requestOptions);
    

    2. Using Postman: If you're using Postman to test, you need to include the trigger in the request headers. Add a header named x-ms-documentdb-pre-trigger-include with the value set to your trigger ID (createPreTrigger).

    3. JavaScript Example: If you're using JavaScript, you can also specify like this:

    const newItem = { id: "1", name: "Sample Item" };
    const requestOptions = { preTriggerInclude: ["createPreTrigger"] };
    await client.createDocument(collectionUri, newItem, requestOptions);
    

     

Resources