Set a maximum record limit per user

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ciucaadrian11
    Member
    • Oct 2019
    • 37

    Set a maximum record limit per user

    Hello,
    I hope to find support and solution for my need.
    I would like to set a a maximum limit for records in entity per user. Exemple: I created a custom entity "Cars" and I would like to set for users to create maximum 100 records and when this limit is reached, users will no longer be able to create records.
    Thank you!
  • victor
    Active Community Member
    • Aug 2022
    • 844

    #2
    Requires coding skills: https://forum.espocrm.com/forum/deve...ndler-and-ajax.

    Comment

    • yuri
      Member
      • Mar 2014
      • 8795

      #3
      HI,

      You can utilize an API before-save script.

      Whenever a user creates a record, check the count of records created by the user. If it's greater than the limit, throw an exception with a message. I think it will be a few lines of code (right in UI, no writing in source files needed).

      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

    • lazovic
      Super Moderator
      • Jan 2022
      • 917

      #4
      Hi ciucaadrian11,

      You can use the following formula script in the Administration > Entity Manager > Car > Formula > API Before Save Script:
      Code:
      $currentUserId = env\userAttribute('id');
      
      if (entity\isNew() && record\count('Car', 'createdById=', $currentUserId) >= 100) {
          recordService\throwForbidden("You have exceeded the limit for creating Cars records.");
      }

      Comment


      • yuri
        yuri commented
        Editing a comment
        Note that the custom entity is likely named 'CCar' (with the 'C' prefix).

      • ciucaadrian11
        ciucaadrian11 commented
        Editing a comment
        Hi,
        Thank you very much for your support. It's working.
    Working...