# The syntax for creating a trigger on relationship property assignments

**URL:** <https://community.neo4j.com/t/the-syntax-for-creating-a-trigger-on-relationship-property-assignments/7616>\
**Category:** Cypher\
**Tags:** apoc, cypher\
**Created:** [May 24, 2019, 9:37pm UTC](https://community.neo4j.com/t/the-syntax-for-creating-a-trigger-on-relationship-property-assignments/7616 "2019-05-24T21:37:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![neo4joe](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/neo4joe/32/2169_2.png) [@neo4joe](https://community.neo4j.com/u/neo4joe)\
**Post date:** [May 24, 2019, 9:37pm UTC](https://community.neo4j.com/t/the-syntax-for-creating-a-trigger-on-relationship-property-assignments/7616/1 "2019-05-24T21:37:33Z")

</div>

I really just want a 'hello world' example for how to make something work. I want to create a trigger on a relationship when new property is assigned to it that matches a specified key.

So this is a bit contrived, but let's say that whenever a property is added to a label with the property key of 'firstname', our trigger adds another property, lasname: 'Smith'.

I have tried some variations on this, but even when Neo4j accepts the trigger, it doesn't actually invoke the trigger's action. Here's what I have tried last:

```auto
CALL apoc.trigger.add('testRelProp','UNWIND apoc.trigger.propertiesByKey({assignedRelationshipProperties},'firstname') as prop  
WITH prop.relationship as r
SET r.lastname = "Smith"', 
{phase:'before'});

```

I have to admit that I am guessing on prop.relationship because I haven't seen it defined anywhere for relationships.

I would be happy to see this run, or any example of the relationship-property-trigger.

---

<div class="post-metadata">

**Author:** ![Jiropole](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/jiropole/32/223_2.png) [@Jiropole](https://community.neo4j.com/u/Jiropole)\
**Post date:** [May 25, 2019, 3:46am UTC](https://community.neo4j.com/t/the-syntax-for-creating-a-trigger-on-relationship-property-assignments/7616/2 "2019-05-25T03:46:48Z")

</div>

Can't comment on using `apoc.trigger`. I don't think the writers of that project recommend it for mission-critical work. However, if you need a bulletproof solution, you can either:  
A) Implement a plugin, aka "unmanaged extension". This is a JVM solution you can deploy in the `/plugins` folder to implement serious app work on the database machine, in the native/fast Neo4j verbatim. If you go this deep, you can also register a "transaction hook" whereby you can be guaranteed to make your changes in response to other changes.  
B) Run a scheduled task executing Cypher against the DB, using your own infrastructure  
outside of Neo4j.

---

<div class="post-metadata">

**Author:** ![neo4joe](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/neo4joe/32/2169_2.png) [@neo4joe](https://community.neo4j.com/u/neo4joe)\
**Post date:** [May 26, 2019, 5:29pm UTC](https://community.neo4j.com/t/the-syntax-for-creating-a-trigger-on-relationship-property-assignments/7616/3 "2019-05-26T17:29:59Z")

</div>

The ideal scenario would be to work with continually improving community solutions (APOC).  
At the moment, the documentation seems to be my issue. I find no example anywhere of a relationship firing a trigger upon property assignment to that relationship.

I’ll keep in mind the option of building my own unmanaged extension for these. I have coded in java in the past, but to be completely honest I would trust the community code over my own for something that runs inside Neo4j. If you’re offering to write a custom trigger, I can keep that in mind too, as my needs evolve. 🙂

---

<div class="post-metadata">

**Author:** ![Jiropole](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/jiropole/32/223_2.png) [@Jiropole](https://community.neo4j.com/u/Jiropole)\
**Post date:** [June 1, 2019, 5:58am UTC](https://community.neo4j.com/t/the-syntax-for-creating-a-trigger-on-relationship-property-assignments/7616/4 "2019-06-01T05:58:39Z")

</div>

You're right, ideally APOC can do everything you need, and that's true especially if you only work with a single instance via the native browser. My comments are definitely biased toward my use of it for a backing store for an API truth state, where we may want to run scheduled tasks anyway, and we already support a plugin.

APOC can get you 98% of the way there – maybe others can chime in?
