Preventing Relate/Unrelate Action

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • alohatech
    Junior Member
    • May 2024
    • 26

    Preventing Relate/Unrelate Action

    Hello,
    I Have two entities, A and B. Entity A has a status field, and it has multiple related B records.

    I'd like to implement a restriction where, if the status of entity A is set to "Closed", users should not be able to relate or unrelate any B records to A.

    The hook for relate and unrelate are just after not before
  • alohatech
    Junior Member
    • May 2024
    • 26

    #2
    When afterRelate runs, Espo has already established the relationship in the DB

    Comment

    • alohatech
      Junior Member
      • May 2024
      • 26

      #3


      I’m trying to enforce a rule in EspoCRM where users should not be allowed to link related records if a Project is still in "Closed" status.
      Context:
      • Entity A (Project)
      • Entity B (Resource)
      • Relationship: many-to-many → Project ⇄ Resources
      • Control field: Project.status
      • Block condition: when status === 'Closed'

      Goal:


      If the Project.status === 'Closed':
      • Users should not be allowed to link new Resources to the Project
      • Existing linked Resources should remain untouched
      • The Project itself should still save — I just want the new links rolled back silently

      What I’ve Tried:
      • I added a beforeSave hook on the Project entity.
      • In that hook, I compare resourcesIds:
        php
        CopyEdit
        $oldIds = $entity->getFetched('resourcesIds') ?? []; $newIds = $entity->get('resourcesIds') ?? []; $added = array_diff($newIds, $oldIds);
      • For each added ID, I run:
        php
        CopyEdit
        $repository->unrelate($entity, 'resources', $id);
      • Then I reset the field:
        php
        CopyEdit
        $entity->set('resourcesIds', $oldIds);

      I also made sure not to call saveEntity() inside beforeSave to avoid recursion.
      The Problem:


      Even though I run unrelate() and reset the IDs in beforeSave, the newly linked Resources still appear after saving.

      I suspect this is because EspoCRM creates the many-to-many link before the entity's beforeSave hook is executed, or that the relationship is committed separately and not affected by my rollback.
      Questions:
      • How can I cleanly prevent or undo the linking of new Resources to a Project while its status is "Closed"?
      • Should this logic go in afterRelate, afterSave, or in a custom controller?
      • Is there a recommended EspoCRM way to enforce restrictions on relationship actions?


      Any suggestions, examples, or guidance would be greatly appreciated.
      Thank you.

      Comment

      • rabii
        Active Community Member
        • Jun 2016
        • 1286

        #4
        do you have the rekationship as fields ? or are you using panels ? can you share a screenshot. i guess you can use front end to disable unlink & select on the resource panel on project detail view and then use backend to validate your logic.
        Rabii
        Here to help :)

        Comment

        • alohatech
          Junior Member
          • May 2024
          • 26

          #5
          Thank you Rabii for your reply, i am having rekationship as panel at the bottom

          Comment

          Working...