Two Plugins Listening for Changes on a Contact

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?

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.

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

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

Looks like you can also set the lead in event:

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().

1 Like

@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.

2 Likes