afterRemove hook to reclaculate

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zerosix
    Member
    • Jan 2024
    • 45

    afterRemove hook to reclaculate

    can i have a sample of code for afterRemove hook to reclaculate fields?
    A has many Bs, if one B is deleted and I need to recalculae A fields
  • item
    Active Community Member
    • Mar 2017
    • 1476

    #2
    Hi,

    see hook on documentation, you just need this in B entity as hook

    PHP Code:
        public function afterRemove(Entity $entity, array $options): void
        {
            if ($entity->get('aId'))
            {
                $a = $this->em->getEntityById('A', $entity->get('aId') );
                
                if ($a){
                    $this->em->saveEntity($a, [
                        'modifiedById' => $this->user->getId(),
                    ]);
                }
            }
        }
    If you could give the project a star on GitHub. EspoCrm believe our work truly deserves more recognition. Thanks.​

    Comment

    Working...