# Creating triggers

**URL:** <https://community.neo4j.com/t/creating-triggers/65715>\
**Category:** Neo4j Graph Platform\
**Tags:** apoc, cypher\
**Created:** [January 10, 2024, 5:31pm UTC](https://community.neo4j.com/t/creating-triggers/65715 "2024-01-10T17:31:17Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![jacob3](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/jacob3/32/29487_2.png) [@jacob3](https://community.neo4j.com/u/jacob3)\
**Post date:** [January 10, 2024, 5:31pm UTC](https://community.neo4j.com/t/creating-triggers/65715/1 "2024-01-10T17:31:17Z")

</div>

Hello,

I am attempting to use an `apoc.trigger` in order to create a set of nodes after a node - with a specific label - is created. However, it doesn't seem to be working.

I updated/added the `apoc.conf` with the appropriate statements to enable `apoc.trigger`. I'm able to use `apoc.trigger` functions including installing a trigger.

BUT, it's still not working for some reason. When I create new nodes, the trigger is not activated.

Any thoughts on what I may be missing?

---

<div class="post-metadata">

**Author:** ![dana\_canzano](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/dana_canzano/32/27392_2.png) [@dana\_canzano](https://community.neo4j.com/u/dana_canzano)\
**Post date:** [January 10, 2024, 6:43pm UTC](https://community.neo4j.com/t/creating-triggers/65715/2 "2024-01-10T18:43:53Z")

</div>

@jacob3

Neo4j version?  
APOC version?

Can you share the cypher used to create the trigger?

---

<div class="post-metadata">

**Author:** ![jacob3](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/jacob3/32/29487_2.png) [@jacob3](https://community.neo4j.com/u/jacob3)\
**Post date:** [January 10, 2024, 7:29pm UTC](https://community.neo4j.com/t/creating-triggers/65715/3 "2024-01-10T19:29:06Z")

</div>

Neo4j Version: 5.11 enterprise  
APOC Version: 5.11 core

---

<div class="post-metadata">

**Author:** ![dana\_canzano](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/dana_canzano/32/27392_2.png) [@dana\_canzano](https://community.neo4j.com/u/dana_canzano)\
**Post date:** [January 10, 2024, 7:57pm UTC](https://community.neo4j.com/t/creating-triggers/65715/4 "2024-01-10T19:57:31Z")

</div>

@jacob3

> [@dana\_canzano](#):
>
> Can you share the cypher used to create the trigger?

or is that not possible?

---

<div class="post-metadata">

**Author:** ![jacob3](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/jacob3/32/29487_2.png) [@jacob3](https://community.neo4j.com/u/jacob3)\
**Post date:** [January 10, 2024, 8:36pm UTC](https://community.neo4j.com/t/creating-triggers/65715/5 "2024-01-10T20:36:43Z")

</div>

Here is the cypher that creates the trigger (in the `system` database. I changed the node labels to remove info that I couldn't share. 🙂

```auto
CALL apoc.trigger.install(
    'dev',
    'create-class-new-user',
    "UNWIND $createdNodes AS n
    WHERE n:User AND NOT (n)-[:PREFERS]->(:School)
    CREATE (p:School)
    SET
        p.id = randomUUID(),
        p.isPrimary = true,
        p.createdAt = datetime(),
        p.modifiedAt = datetime()
    WITH p
    CREATE (d1:Department {name: 'Math'})
    SET d1.id = randomUUID(), d1.createdAt = datetime(), d1.modifiedAt = datetime(), d1.isPrimary = false
    CREATE (p)-[:HAS_DEPARTMENT]->(d1)
    CREATE (d2:Department {name: 'Science'})
    SET d2.id = randomUUID(), d2.createdAt = datetime(), d2.modifiedAt = datetime(), d2.isPrimary = false
    CREATE (p)-[:HAS_DEPARTMENT]->(d2)
    WITH d1, d2
    CREATE (t:Category {name:'Monday'})
    SET 
        t.id = randomUUID(),
        t.createdAt = datetime(),
        t.modifiedAt = datetime()
    CREATE (d1)-[:PREFERS]->(t)
    CREATE (d2)-[:PREFERS]->(t)
    WITH d1, d2
    CREATE (t:Category {name:'Tuesday'})
    SET 
        t.id = randomUUID(),
        t.createdAt = datetime(),
        t.modifiedAt = datetime()
    CREATE (d1)-[:PREFERS]->(t)
    CREATE (d2)-[:PREFERS]->(t)
    WITH d1, d2
    CREATE (t:Category {name:'Wednesday'})
    SET 
        t.id = randomUUID(),
        t.createdAt = datetime(),
        t.modifiedAt = datetime()
    CREATE (d1)-[:PREFERS]->(t)
    CREATE (d2)-[:PREFERS]->(t)
    WITH d1, d2
    CREATE (t:Category {name:'Thursday'})
    SET 
        t.id = randomUUID(),
        t.createdAt = datetime(),
        t.modifiedAt = datetime()
    CREATE (d2)-[:PREFERS]->(t)
    WITH d1, d2
    CREATE (t:Category {name:'Friday'})
    SET 
        t.id = randomUUID(),
        t.createdAt = datetime(),
        t.modifiedAt = datetime()
    CREATE (d1)-[:PREFERS]->(t)", {phase:'afterAsync'})

```

---

<div class="post-metadata">

**Author:** ![dana\_canzano](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/dana_canzano/32/27392_2.png) [@dana\_canzano](https://community.neo4j.com/u/dana_canzano)\
**Post date:** [January 10, 2024, 9:43pm UTC](https://community.neo4j.com/t/creating-triggers/65715/6 "2024-01-10T21:43:52Z")

</div>

@jacob3

have u tried with a simpler trigger definition as a means to debug.  
Given

```auto
UNWIND $createdNodes AS n
    WHERE n:User AND NOT (n)-[:PREFERS]->(:School)
    CREATE (p:School)

```

are any :School nodes created?  
If you remove the `WHERE` clause are any :School nodes created?

---

<div class="post-metadata">

**Author:** ![jacob3](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/jacob3/32/29487_2.png) [@jacob3](https://community.neo4j.com/u/jacob3)\
**Post date:** [January 11, 2024, 1:42am UTC](https://community.neo4j.com/t/creating-triggers/65715/7 "2024-01-11T01:42:58Z")

</div>

Removing the where clause doesn't seem to work either.

Am I not allowed to create a new node in a trigger?

---

<div class="post-metadata">

**Author:** ![dana\_canzano](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/dana_canzano/32/27392_2.png) [@dana\_canzano](https://community.neo4j.com/u/dana_canzano)\
**Post date:** [January 11, 2024, 10:51pm UTC](https://community.neo4j.com/t/creating-triggers/65715/8 "2024-01-11T22:51:36Z")

</div>

@jacob3  
given the current issue and rather than building out a trigger with a lot of complexity, I decided to build out a very small test case. And with Neo4j 5.11.0 and

```auto
ls -al plugins/
drwxr-xr-x 2 neo4j neo4j 4096 Jan 11 22:24 .
drwxr-xr-x 15 neo4j neo4j 4096 Dec 5 11:22 ..
-rw-r--r-- 1 neo4j neo4j 14495768 Jan 11 22:24 apoc-5.11.0-core.jar
-rw-rw-r-- 1 neo4j neo4j 11137885 Dec 5 11:20 apoc-5.11.0-extended.jar
-rw-r--r-- 1 neo4j neo4j 2217 Aug 8 07:47 README.txt

```

and apoc.conf defined as

```auto
cat ../conf/apoc.conf
apoc.trigger.enabled=true
apoc.trigger.refresh=60000

```

and then i defined trigger as follows

```auto
@system> CALL apoc.trigger.install(
             'neo4j',
             'create-class-new-user',
             "UNWIND $createdNodes AS cN create(n:Result {nodes:id(cN)})",{});

```

which does nothing more than for the nodes creates define a new node `:Result` and set its `nodes:` property to be the id of the node created.

And thus

```auto
@neo4j> match (n:Test) return n;
+---+
| n |
+---+
+---+

0 rows
ready to start consuming query after 96 ms, results consumed after another 3 ms
@neo4j> match (n:Result) return n;
+---+
| n |
+---+
+---+

0 rows
ready to start consuming query after 3 ms, results consumed after another 3 ms
@neo4j> unwind range(1,5) as x create (n:Test {id:x});
0 rows
ready to start consuming query after 8 ms, results consumed after another 0 ms
Added 5 nodes, Set 5 properties, Added 5 labels
@neo4j> match (n:Test) return n.id, id(n);
+--------------+
| n.id | id(n) |
+--------------+
| 1 | 10101 |
| 2 | 10102 |
| 3 | 10103 |
| 4 | 10104 |
| 5 | 10105 |
+--------------+

5 rows
ready to start consuming query after 4 ms, results consumed after another 4 ms

@neo4j> match (n:Result) return n order by n.nodes asc;;
+--------------------------+
| n |
+--------------------------+
| (:Result {nodes: 10101}) |
| (:Result {nodes: 10102}) |
| (:Result {nodes: 10103}) |
| (:Result {nodes: 10104}) |
| (:Result {nodes: 10105}) |
+--------------------------+

5 rows
ready to start consuming query after 73 ms, results consumed after another 7 ms

```

so would seem to work for me.

---

<div class="post-metadata">

**Author:** ![jacob3](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/jacob3/32/29487_2.png) [@jacob3](https://community.neo4j.com/u/jacob3)\
**Post date:** [January 11, 2024, 11:32pm UTC](https://community.neo4j.com/t/creating-triggers/65715/10 "2024-01-11T23:32:33Z")

</div>

Thanks @dana_canzano for looking into this.

What worked for me was keeping the `phase` parameter undefined, i.e. `"CALL apoc.trigger.install("", "", {})"`.

Now it's working!

---

<div class="post-metadata">

**Author:** ![StuMoore](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/stumoore/32/29270_2.png) [@StuMoore](https://community.neo4j.com/u/StuMoore)\
**Post date:** [January 12, 2024, 4:54pm UTC](https://community.neo4j.com/t/creating-triggers/65715/11 "2024-01-12T16:54:38Z")

</div>

There is another option besides APOC, we have introduced Change Data Capture (CDC) in Neo4j, it is currently available as a beta in versions from Neo4j 5.14 Enterprise. You can create selectors which watch for specific labels, when a node is created a change event will be created and you can then use that to perform the desired action [Introduction - Change Data Capture](https://neo4j.com/docs/cdc/current/)
