Announcement

Collapse
No announcement yet.

Which Hook to Use for Updating Entity A After Unlinking a B?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Which Hook to Use for Updating Entity A After Unlinking a B?

    I have two entities: A and B. Entity A has many Bs. I want to update a value in A after unlinking one B from the collection of Bs. Which hook should I use to accomplish this? Thank you

  • #2
    Hello alohatech
    Here you have a list of hooks: Hooks - EspoCRM Documentation

    Based on the description afterUnrelate​ seems the best.

    Comment


    • alohatech
      alohatech commented
      Editing a comment
      in the documentation afterUnrelate is used when two records are unrelated through a many-to-many relationship; I have one-to-many in my case

  • #3
    Like this?
    PHP Code:
    <?php
    namespace Espo\Custom\Hooks\A;
    use 
    Espo\ORM\Entity;
    use 
    Espo\Core\Hooks\Base;
    class 
    AfterUnrelate extends \Espo\Core\Hooks\Base
    {
    public static 
    $order 10;
    public function 
    afterUnrelate(Entity $entity, array $options)
    {

    // Fetch all B records related to this A
    $BList $entity->get('Bs');
    $total 0// Initialize as 0
    // Sum the price values from all related Bs records
    foreach ($BList as $B) {
    $total += (float) $B->get('price');
    }
    // Update the total field in the A entity
    $entity->set('total'$total);
    }
    }

    Comment


    • #4
      You can check this hook:
      espocrm/application/Espo/Modules/Crm/Hooks/Account/Contacts.php

      Comment

      Working...
      X