0

I have tried to implement a delete pre-trigger in CosmosDB by using Script Explorer in Azure portal (Trigger type is "Pre" and trigger operation is "Delete" ). What I want to do is creating a new document by using the deleted one and changing its "pastDue" property as true. After deleting a document from my collection, I can't see any changes.

Could someone tell me what I am doing wrong?And how can I see if the trigger is executed successfully or not?

    function markReminderAsPastDue() {
      var collection = getContext().getCollection();
      var request = getContext().getRequest();
      var docToCreate = request.getBody();
      docToCreate["pastDue"] = true;
      collection.createDocument(collection.getSelfLink(),
            docToCreate,
            function (err, documentCreated) {
                if (err) throw new Error('Error' + err.message);       

        });
    }

Here is the function that I use to delete any document from any collection in my database handler class:

removeItem (item, partitionKey, callback)
{
    var options = {
        partitionKey: [ partitionKey ? partitionKey: item.email]
    };
    this.client.deleteDocument(item._self, options,
        (err, doc) => {
            if (err)
            {
                LogUtils.error("DBHandler.removeItem "+err.body);
                callback(err, null);
            }
            else
            {
                callback(null, "success remove Item");
            }
        });
} 
Nil
  • 328
  • 2
  • 16
  • Do you specify preTriggersInclude in request options when you delete the document? The triggers are not auto-invoked and need to be specified explicitly in each request that needs triggers. Also make sure to check return value of collection.createDocument, something like this: var isAccepted = collection.createDocument(...); if (!isAccepted) throw new Error("not accepted"); – Michael Koltachev Nov 28 '17 at 17:56

1 Answers1

0

According to your description , you need to create Pre-Trigger first on portal or by using Document DB SDK.

enter image description here

Trigger Type should be Pre and Trigger Operation should be Delete.

Per my experience, 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.

You need to specify the triggers to be invoked before the operation in the RequestOptions arguments. I don't know which language you're using , here please refer to the snippet of java code as below:

RequestOptions options = new RequestOptions();
//enable script logging  true
options.setScriptLoggingEnabled(true);
List<String> list = new ArrayList<String>();
list.add("test");
options.setPreTriggerInclude(list);

documentClient.deleteDocument("dbs/db/colls/coll/docs/400", options);

System.out.println("delete success");

In addition, you could refer to the thread : Trigger in DocumentDb not fired?

Hope it helps you.


Update Answer :

I found you did not specify the triggers to be invoked in the RequestOptions arguments. Please modify the options in your code as below:

var options = {
        partitionKey: [ partitionKey ? partitionKey: item.email],
        preTriggerInclude: "<your pre-trigger's id >"
    };

And trigger function as below :

function trigger(){
    var context = getContext();
        var request = context.getRequest();
        var collection = getContext().getCollection();

        var documentToCreate = request.getBody();
        documentToCreate["pastDue"] = true;
        documentToCreate["id"] = "";
        collection.createDocument(collection.getSelfLink(),documentToCreate);

}
Jay Gong
  • 23,163
  • 2
  • 27
  • 32
  • @user3127584 Did you delete documents by SDK or on the portal? Currently, It seems that it is not supported from Azure portal. – Jay Gong Nov 29 '17 at 06:27
  • Thanks for your reply Jay. The code snippet that I have shared above is written in JavaScript. I have used Script Explorer in Azure portal to create this trigger. Its trigger type is "Pre" and its trigger operation is "Delete". After deleting a document from the associated collection, I can't see a new document as a result of the trigger. So it seems that it is not fired. – Nil Nov 29 '17 at 06:29
  • I have deleted documents by using Node.js SDK from a Web App which is also on Azure cloud. – Nil Nov 29 '17 at 06:30
  • @user3127584 Are you convenient share you delete the code snippet of document to let me help you solve this issue? – Jay Gong Nov 29 '17 at 06:32
  • I have shared the delete function above in the description area. I couldn't write it here because of the character limitations for the comment field. – Nil Nov 29 '17 at 06:41
  • Thank you so much for your help. It works and the trigger creates new document. However I have another little question. It isn't fired by deletion with TTL. Do you know is this possible? Or do you recommend me to raise another question for this? – Nil Nov 29 '17 at 08:18
  • @user3127584 Yes.You could raise another question and I will try to follow up. – Jay Gong Nov 29 '17 at 08:54