Announcement

Collapse
No announcement yet.

How to implement a custom Job

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to implement a custom Job

    Hi Folks,

    I am trying to implement a job with a simple logic to increment an integer value on a custom entity when its status is changed. i have looked into documentation but couldn't figure it out. Anyone has done this before that can share steps on how to create custom job and schedule the job to run every minute please share with us.

    Many thanks

  • #2
    Hi,
    job is not adequate for what you will..
    workflow afterSave
    hook afterSave
    https://docs.espocrm.com/development...hed-attributes

    all solution is avaible.

    Comment


    • #3
      Hi,

      The problem with workflow is that it depends on a report to do the job and there is a risk that some other admin could change that report or even delete it. i have build a custom job as below and it seems to work but not yet incrementing the integer variable, but it is available through the crob job scheduler and i can set up this custom job to run for spefici time, below my code, i am almost there:

      Code:
      <?php
      namespace Espo\Custom\Jobs;
      use Espo\Core\ORM\EntityManager;
      use Espo\Core\Job\JobDataLess;
      
      class UpdateDwellTime implements JobDataLess
      {
      protected $entityManager;
      protected $dwellTime = 0;
      
      public function __construct(EntityManager $entityManager)
      {
      $this->entityManager = $entityManager;
      }
      
      public function run(): void
      {
      $list = $this->entityManager
      ->getRDBRepository('Claim')
      ->where([
      'stage!=' => ['Invoice Settled', 'Failed Processing']
      ])
      ->find();
      
      foreach ($list as $e) {
      $e->set('dwellTime', $this->increment());
      $this->entityManager->saveEntity($e);
      }
      }
      
      public function increment() {
      return $dwellTime = $dwellTime + 1;
      }
      }
      Not sure why it is not incrementing yet.

      Comment


      • #4
        dwellTime is a class property, you need to reference it as $this->dwellTime instead of $dwellTime

        Comment


        • #5
          Hello telecastg ,
          i will not kill the post but, i retry : job is not good for what he will
          a job who loop about all record with status waiting... so every minute, you will increment ... it's a clock

          he must go admin->entityManager->entity-> Formula

          and use :
          ifThen


          ifThen(CONDITION, CONSEQUENT)

          If CONDITION is met, then do CONSEQUENT. If not - do nothing.

          CONSEQUENT can consist of mutliple commands separated by the semicolon ;.

          Example:

          ifThen( $someVariable == 'someValue', // if condition is true $anotherVariable = 1, // do this )

          entity\isAttributeChanged


          entity\isAttributeChanged(ATTRIBUTE)

          Returns TRUE if ATTRIBUTE of the record was changed.

          Example:

          entity\isAttributeChanged('status')
          Last edited by item; 12-07-2021, 06:13 PM.

          Comment


          • #6
            I agree with you, a job will require the server to be constantly running waiting for a specific condition to be met and then execute some logic, instead of not doing anything until the condition is met.

            Formula or even better a hook would be more appropriate in my opinion

            Comment


            • #7
              Hi Both,

              thanks for your reply, much appreciated.

              formula is not good for my use case, i had a prior solution with scheduled workflow that increment (dwellTime) field, it was scheduled to do this just at 1:10 am everyday. But there was a risk that the report could be deleted by another admin so i though a job will be the best way to handle this.

              i have managed go get it to work and now the job is available through / Job scheduler in admin and just configured the specific job to run everyday at 1:10 am .
              all is working like a charm

              thanks for your thoughts and reply

              Comment


              • telecastg
                telecastg commented
                Editing a comment
                You're welcome, I'm glad that it worked out well for your use case.

                If you only run the job once a day, then using formula or a hook would not have any impact in terms of server usage.

                Ultimately the best solution is the one that works for each one of us
                Last edited by telecastg; 12-08-2021, 03:32 AM.
            Working...
            X