Implementing Time-based Record Creation Restriction in Espo CRM

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sapyantuk
    Senior Member
    • Oct 2020
    • 265

    Implementing Time-based Record Creation Restriction in Espo CRM

    Greetings,
    I have a query for the developers regarding a specific requirement. I intend to implement a functionality that prevents users from creating records prior to a designated time. To illustrate, within a given entity, I aim to establish a rule wherein users are only permitted to create records after 9:30 AM. Prior to this time, users should be restricted from creating any records.
    I am seeking guidance on how to accomplish this within Espo CRM. Your assistance in achieving this objective would be greatly appreciated.
    Thank you.



  • esforim
    Active Community Member
    • Jan 2020
    • 2204

    #2
    Sound very doable with the Before Save API and is(new) and time > 9:30am

    It will require a little bit of re-do but here is a quick concept I just did:

    Click image for larger version

Name:	image.png
Views:	367
Size:	19.3 KB
ID:	96510

    Comment

    • lazovic
      Super Moderator
      • Jan 2022
      • 809

      #3
      Hi sapyantuk,

      You can use API Before Save Script. Go to Administration > Entity Manager > [your_entity_name] > Formula > API Before Save Script and insert the following lines:
      Code:
      $time = datetime\format(datetime\now(), 'Europe/Kiev', 'hh:mm A');
      
      if (entity\isNew() && env\userAttribute('type') != 'Admin' && $time < '09:30 AM') {
         recordService\throwConflict("You can't create record right now.");
      }
      ​​
      In the first line of this formula script please insert your timezone instead of Europe/Kiev.

      Comment


      • esforim
        esforim commented
        Editing a comment
        The different in skill level is above! I think doing this will work for you.

        Personally but I find it weird to have this type of restriction, it lock data from being input but I can understand the reasoning for it in certain case. The issue with this it restrict permanently, rather than a "Confirmation"
    • sapyantuk
      Senior Member
      • Oct 2020
      • 265

      #4
      lazovic Thank you. Solved my issue.
      i have one more question

      I aim to prevent users from creating duplicate records. My objective is to implement a validation process that checks whether a user has already created a record on the same day. The intention behind this is to ensure that a user can only create a single record within a given day. How can I effectively accomplish this?




      Comment

      • sapyantuk
        Senior Member
        • Oct 2020
        • 265

        #5
        i am using this code


        $time = datetime\format(datetime\now(), 'Asia/Kathmandu', 'hh:mm A');
        $currentDate = date('Y-m-d');

        if (entity\isNew() && env\userAttribute('type') != 'Admin' && $time < '09:35 AM') {
        recordService\throwConflict("You can't check in before 09:35. Please try again after 09:35.");
        }

        // Check if the user has created a record on the current day
        $userRecords = recordService\findByAttributes([
        'user_id' => env\userId(),
        'created_at' => $currentDate,
        ]);

        if (count($userRecords) > 0) {
        recordService\throwConflict("You have already created a record today.");
        }



        but its not working

        Comment

        • lazovic
          Super Moderator
          • Jan 2022
          • 809

          #6
          sapyantuk,

          This is the incorrect formula script because of wrong syntax:
          Code:
          $userRecords = recordService\findByAttributes([
          'user_id' => env\userId(),
          'created_at' => $currentDate,
          ]);
          
          if (count($userRecords) > 0) {
          recordService\throwConflict("You have already created a record today.");
          }
          ​​​
          This is correct formula script to achieve your goal:
          Code:
          $userRecords = record\count('your-entity-type', 'createdById=', env\userAttribute('id'), 'createdAt>', datetime\today()); // change 'your-entity-type' to your entity name like Contact or Lead
          
          if ($userRecords > 0) {
          recordService\throwConflict("You have already created a record today.");
          }


          I recommend that you create formula scripts by following the relevant documentation: https://docs.espocrm.com/administration/formula​

          Comment

          • sapyantuk
            Senior Member
            • Oct 2020
            • 265

            #7
            lazovic it is blocking user from editing the record also. i want to check condition while creating record only.

            Comment


            • lazovic
              lazovic commented
              Editing a comment
              $userRecords = record\count('your-entity-type', 'createdById=', env\userAttribute('id'), 'createdAt>', datetime\today()); // change 'your-entity-type' to your entity name like Contact or Lead

              if ($userRecords > 0 && entity\isNew() && env\userAttribute('type') != 'Admin') {
              recordService\throwConflict("You have already created a record today.");
              }
          • yuri
            Member
            • Mar 2014
            • 8440

            #8
            $time < '09:30 AM' won't work reliably. It's better to represent time as a number (not string) and compare.

            E.g. get hours (0-24) * 60 and plus minutes. Do the same for both $time and '09:30'. Then compare two integer numbers.
            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

            • yuri
              Member
              • Mar 2014
              • 8440

              #9
              String comparison of time parts w/o AM/PM (0-24 format) should work too (as PHP that interprets and run formula supports string comparison in such way).
              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

              • sapyantuk
                Senior Member
                • Oct 2020
                • 265

                #10
                Dear lazovic , I wanted to bring to your attention an issue that Yuri mentioned regarding the time comparison "$time < '09:30 AM'." It appears that this comparison might not be functioning reliably.
                I would like to provide you with some context regarding my current location. I am currently in Nepal, specifically in the Asia/Kathmandu time zone. The local time here is 06:05 PM. However, I have encountered a problem when attempting to create an attendance record. This action triggers a server error with code 500, accompanied by the error message "throwConflict('You can't create a record right now.');"
                I believe that this issue requires some attention and investigation to ensure that the attendance functionality works smoothly regardless of the time zone differences. Your assistance in resolving this matter would be greatly appreciated.
                Thank you for your time and consideration.
                Best regards,


                Comment

                • lazovic
                  Super Moderator
                  • Jan 2022
                  • 809

                  #11
                  sapyantuk,

                  Please try to use this formula script:
                  Code:
                  $hourNow = datetime\hour(datetime\now(), 'Asia/Kathmandu');
                  $minutesNow = datetime\minute(datetime\now(), 'Asia/Kathmandu');
                  $timeNow = ($hourNow * 60) + $minutesNow;
                  
                  $time = (9 * 60) + 30; // 09:30 AM
                  
                  if (entity\isNew() && env\userAttribute('type') != 'Admin' && $timeNow < $time) {
                  recordService\throwConflict("You can't create record right now.");
                  }
                  
                  $userRecords = record\count('your-entity-type', 'createdById=', env\userAttribute('id'), 'createdAt>', datetime\today()); // change 'your-entity-type' to your entity name like Contact or Lead
                  
                  if ($userRecords > 0 && entity\isNew() && env\userAttribute('type') != 'Admin') {
                  recordService\throwConflict("You have already created a record today.");
                  }

                  Comment

                  • sapyantuk
                    Senior Member
                    • Oct 2020
                    • 265

                    #12
                    lazovic Thanks , Solved my issue .

                    Comment

                    Working...