2

I am trying to put a trigger on a Collection, and it appears to not be tripping. The Trigger is prepared with the Azure Portal. I was trying specific logic - but in the end just placed an example on the Azure Site. The Trigger logic is the following:

var context = getContext();
var request = context.getRequest();

// document to be created in the current operation
var documentToCreate = request.getBody();

// validate properties
if (!("timestamp" in documentToCreate)) 
{
  var ts = new Date();
  documentToCreate["my timestamp"] = ts.getTime();
}

// update the document that will be created
request.setBody(documentToCreate);

The trigger is not working. No errors appear to be generated and I am unsure what is going on.

Note: the trigger is a pre crate trigger. And for the trigger logic I listed I am not creating a document with a "my timestamp" property. I have also tested other scenarios along the same concept where I want to add an attribute to the document being created.

Peter
  • 173
  • 12

1 Answers1

6

Triggers are not automatically triggered. Rather, you have to explicitly specify the trigger in the operation that you want to activate the trigger. This is done for performance reasons but it makes triggers less useful.

Larry Maccherone
  • 9,393
  • 3
  • 27
  • 43
  • Exactly how are the tripped from an operation? And can they only be triggered from code? Can they be tripped from the Azure Portal? – Peter Jun 20 '17 at 17:55
  • For the node.js SDK, you specify it in the request options of any operation (say `upsertDocument()` for example): http://azure.github.io/azure-documentdb-node/global.html#RequestOptions. I suspect something similar for the other SDKs. – Larry Maccherone Jun 20 '17 at 20:45
  • As far as I know, as Larry Maccherone mentioned, we need to explicitly specify the trigger in the operation to activate the trigger. Currently, It seems that it is not supported from Azure portal. I also find another smilar [SO thread](https://stackoverflow.com/questions/32647843/trigger-in-documentdb-not-fired). – Tom Sun - MSFT Jun 28 '17 at 02:27
  • @LarryMaccherone, if triggers are not automatically triggered, what is the reason that the portal has "Trigger Type" and "Trigger Operation", if the developer choose which trigger to run in each request? – nrofis Oct 09 '20 at 21:16