# Two Plugins Listening for Changes on a Contact

**URL:** https://forum.mautic.org/t/two-plugins-listening-for-changes-on-a-contact/24901
**Category:** Development
**Tags:** development, integrations-plugins
**Created:** [July 27, 2022, 4:38pm UTC](https://forum.mautic.org/t/two-plugins-listening-for-changes-on-a-contact/24901 "2022-07-27T16:38:27Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![steverobinson](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/steverobinson/32/174_2.png) [@steverobinson](https://forum.mautic.org/u/steverobinson)
#### Post date: [July 27, 2022, 4:38pm UTC](https://forum.mautic.org/t/two-plugins-listening-for-changes-on-a-contact/24901/1 "2022-07-27T16:38:27Z")

</div>

I have two custom plugins, both doing different things but following the same pattern:

1. listen for a LEAD\_POST\_SAVE event
2. Check to see what fields have been updated
3. If certain fields are updated, then do some stuff and update the contact

The problem I have is that if Plugin A and Plugin B both get triggered for the same contact update, Plugin A will write to the record and when Plugin B runs it only sees the changes that Plugin A made, not the changes that triggered the subscribed event in the first place.

Is this intended behavior or do you think it warrants a GitHub issue?

Is there a way to get the changes from the LEAD\_POST\_SAVE event itself instead of calling $lead-\>getChanges() on the Lead object passed from the event so they can’t be messed with by other plugins?

---

<div class="post-metadata">

### Author: ![mzagmajster](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/mzagmajster/32/687_2.png) [@mzagmajster](https://forum.mautic.org/u/mzagmajster)
#### Post date: [July 28, 2022, 11:05am UTC](https://forum.mautic.org/t/two-plugins-listening-for-changes-on-a-contact/24901/2 "2022-07-28T11:05:39Z")

</div>

Inside subsriber for LEAD\_POST\_SAVE you probably save entity again. Thats why the second plugin only sees the changes that the first made.

Try listening to LEAD\_PRE\_SAVE and avoid saving the value inside subscriber.

---

<div class="post-metadata">

### Author: ![steverobinson](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/steverobinson/32/174_2.png) [@steverobinson](https://forum.mautic.org/u/steverobinson)
#### Post date: [July 28, 2022, 4:06pm UTC](https://forum.mautic.org/t/two-plugins-listening-for-changes-on-a-contact/24901/3 "2022-07-28T16:06:39Z")

</div>

@mzagmajster Thank you for the reply. Yes, you’ve nailed the problem.

How are you thinking to save the value outside the subscriber?

---

<div class="post-metadata">

### Author: ![mzagmajster](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/mzagmajster/32/687_2.png) [@mzagmajster](https://forum.mautic.org/u/mzagmajster)
#### Post date: [July 28, 2022, 5:33pm UTC](https://forum.mautic.org/t/two-plugins-listening-for-changes-on-a-contact/24901/4 "2022-07-28T17:33:33Z")

</div>

Looks like you can also set the lead in event:

> <https://github.com/mautic/mautic/blob/63491f45303310660aae5a30bc05569fc3a536cc/app/bundles/LeadBundle/Event/LeadEvent.php>

Listen to LEAD\_PRE\_SAVE modify the lead based on values and then call setLead on event.

When LEAD\_POST\_SAVE fires your changes should be saved in database.

Option 2: I think this complicates things and I am not sure its really necessary so I would try the above first.

You can create a custom filed and store result of getChanges there and then the second plugin can look into that instead of $lead-\>getChanges().

---

<div class="post-metadata">

### Author: ![steverobinson](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/steverobinson/32/174_2.png) [@steverobinson](https://forum.mautic.org/u/steverobinson)
#### Post date: [July 28, 2022, 9:47pm UTC](https://forum.mautic.org/t/two-plugins-listening-for-changes-on-a-contact/24901/5 "2022-07-28T21:47:35Z")

</div>

@mzagmajster I owe you a beer! Thank you so much.

This was exactly what was needed to make this work!

For anyone else’s benefit, I had modeled my plugins from the Clearbit plugin as a starting point, and it uses the LEAD\_POST\_SAVE event then fires saveEntity, so I assumed that was a good way to do it, but that actually messes up any other plugin looking to detect the changes to the lead after that. By moving both my plugins to LEAD\_PRE\_SAVE and not persisting/saving the changes in the subscriber, they both just work!

I may submit a PR moving the Clearbit plugin to LEAD\_PRE\_SAVE too. We don’t actually use the Clearbit plugin, but if we did, it would be part of the problem.
