After Relationship add triggers BEFORE the Database is updated?

I am on-site, v25.1.2 Ent

I have an After Relationship Add logic hook on Leads that calls an external function to do some work.

When the After Relationship Add event triggers, the Leads bean has the campaignID, the Arguments of the Logic Hook have the Related ID set to that campaignID, but the database does not yet have the campaign_id in the lead.

So the external API that is being called, which receives the LeadID as parameter and queries the leads table directly from the DB, does not see a campaign_id on the Lead record.

I checked in my logic hook by adding log statements and a DB query at the start of the after_relationship_add hook:

$GLOBALS['log']->fatal('related campaign from bean is' . $bean->campaign_id);
$GLOBALS['log']->fatal('related campaign from logic_hook args is' . $arguments['related_id']);
$GLOBALS['log']->fatal('related module ' . $arguments['related_module']);
$query = 'SELECT campaign_id FROM leads WHERE id = ?';
$params = array($bean->id);
$conn = $GLOBALS['db']->getConnection();
$stmt = $conn->executeQuery($query, $params);
$results = $stmt->fetchAll();
$GLOBALS['log']->fatal('related campaign from db query is' . $results['campaign_id']);

and the Log confirms that the campaign_id is NOT on the lead when the after relationship add on Campaigns runs

Wed Oct  1 14:57:37 2025 [766][1][FATAL] event = after_relationship_add

Wed Oct  1 14:57:37 2025 [766][1][FATAL] related campaign from bean is1fc5ca18-4f09-11e8-b022-001a4a160206

Wed Oct  1 14:57:37 2025 [766][1][FATAL] related campaign from logic_hook args is1fc5ca18-4f09-11e8-b022-001a4a160206

Wed Oct  1 14:57:37 2025 [766][1][FATAL] related module Campaigns

Wed Oct  1 14:57:37 2025 [766][1][FATAL] rrelated campaign from db query is

I would expect the DB to be updated BEFORE the logic hook triggers...

Thoughts?

Thanks,

FrancescaS

Parents
  • Hi  ,

    If I understood the use case correct:

    The value you are looking wouldn't be there because the value would be set back after the relationship (right after the spot you are on).
     
    I'm not sure about your constraints but I'd like to ask why don't you throw the value you have into the function rather than pulling it? 

    If that is a requirement, I would move the logic to into after_save Lead?

    $stateChanges = $arguments['stateChanges'] ?? $bean->getStateChanges();
    if(isset($stateChanges['campaign_id']) {
    //code here
    }



    Hope it helps.

  • The API does not "require" a campaignID so using that state change on campaign_id is not appropriate in this case.

    Our workaround was to change the external API and pass the $bean->campaign_id together with the lead_id to the external API, but it did not feel right to me that the hook runs before the record is actually updated.
    Had it been a Before Relationship Add, sure, but After did not sound right to me.

    If your engineering thinks it's correct to say a relationship exists before it is recorded, then I'll just have to remember that.

    FrancescaS

Reply
  • The API does not "require" a campaignID so using that state change on campaign_id is not appropriate in this case.

    Our workaround was to change the external API and pass the $bean->campaign_id together with the lead_id to the external API, but it did not feel right to me that the hook runs before the record is actually updated.
    Had it been a Before Relationship Add, sure, but After did not sound right to me.

    If your engineering thinks it's correct to say a relationship exists before it is recorded, then I'll just have to remember that.

    FrancescaS

Children
No Data