Capture relate action API Before Save Script

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Maarten
    Member
    • Jun 2020
    • 56

    Capture relate action API Before Save Script

    I want to capture in the API Before Save Script if a user is being related to a specific Team. If so, I want to throw a recordService\throwForbidden error.

    When I relate a User to a Team, then this API call is created:

    api/v1/Team/65aec3d57181bc2f7/users

    The body of the call is:

    {
    "ids": [
    "6050a05e919a9f3de"
    ]
    }​


    However, when I create an API before save script on the Entity Team, that checks if the id of the team is a specific ID, then it's not being triggered.
    If I update for instance the description field of the Team, then it's triggered.

    Is the relate action not caught by the Team entity API before save script?
    I thought about using the API before save script of the Entity User, but then I don't know what Team the user being attempted to be added to. I can only retrieve all teamIds. That's not sufficient.

    Is there another way to catch this event and throw an error?
    Last edited by Maarten; 01-25-2024, 10:56 AM.
  • yuri
    Member
    • Mar 2014
    • 8453

    #2
    API Before Save won't work when you relate records via a relationship panel (dropdown > Select). You can disable this functionality to leave the User edit as the only way to add/remove teams.

    In the API-Before-Save script you can compare the previous teamsIds and new teamsIds. You can check whether a specific ID was set before, whether a specific ID is set now.
    If you find EspoCRM good, we would greatly appreciate if you could give the project a star on GitHub. We believe our work truly deserves more recognition. Thanks.

    Comment

    • rabii
      Active Community Member
      • Jun 2016
      • 1250

      #3
      Hi,

      The reason why it won't work is first, team and user are linked in a middle table (TeamUser) so you don't have access to the field users on the team to check if there is any change. In your situation i would reach out to save error handlers, they are very useful https://docs.espocrm.com/development...error-handlers

      Check out a tutorial i have written on how to use them below
      As per the documentation the Save Error Handlers are pretty powerful when it come to throwing an error / conflict to the front end when a record is being save. as quoted below from documentation: Today i am sharing with you how you can implement such feature, let suppose that you want to force a rule to not assign more than
      Rabii
      Web Dev

      Comment

      • Maarten
        Member
        • Jun 2020
        • 56

        #4
        Great, I'll give this a go!

        Comment

        Working...