Announcement

Collapse
No announcement yet.

Workflows After record deleted

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

  • Workflows After record deleted


    I want the child data to be automatically deleted when the parent data is deleted. Since there is no "After record deleted" option in Workflows, how should I proceed?
    Click image for larger version

Name:	image.png
Views:	66
Size:	38.7 KB
ID:	107942

  • #2
    Use signal (@delete) and then use action (Run script formula) to loop through the related records and delete them using record\delete() function.

    Comment


    • #3
      @rabii
      Thank you for your guidance. I used the following formula, and it works correctly in the Formula Sandbox, but it doesn't work in Workflows.
      Could it be because the actions are being executed after the deletion?

      HTML Code:
      //获取请求明细ID
      $relatedRecordIds = requestlistsIds;
      output\printLine($relatedRecordIds);
      
      //循环删除请求明细记录
      $i = 0;
      while ($i < array\length($relatedRecordIds)) {
      output\printLine(array\at($relatedRecordIds, $i));
      $relatedId = array\at($relatedRecordIds, $i);
      if ($relatedId) {
      record\delete('Requestlist', $relatedId);
      }
      $i = $i + 1;
      }​
      Click image for larger version  Name:	image.png Views:	0 Size:	42.2 KB ID:	107978
      Last edited by lj4353; 07-01-2024, 02:39 PM.

      Comment


      • #4
        Yes
        You are right since the record is deleted the system can't find an related records therefore it doesn't execute. For the moment you can use afterRemove Hook which will be best for your use case. below is an example code:

        PHP Code:
        <?php

        namespace Espo\Custom\Hooks\YourEntity// Replace YourEntity with the correct entity name

        use Espo\Core\Hook\Hook\AfterRemove;
        use 
        Espo\Custom\Entities\RequestList// Replace with correct name
        use Espo\Modules\Crm\Entities\YourEntity// Replace with correct name Assuming the entity is a crm entity if it is a custom entity then adjust the path Espo\Custom\Entities\YourEntity
        use Espo\ORM\Entity;
        use 
        Espo\ORM\EntityManager;
        use 
        Espo\ORM\Repository\Option\RemoveOptions;

        /**
         * @implements AfterRemove<YourEntity> // Replace YourEntity with the correct entity name
         */
        class DeleteRequestList implements AfterRemove
        {
            public function 
        __construct(private EntityManager $entityManager) {}

            public function 
        afterRemove(Entity $entityRemoveOptions $options): void
            
        {
                
        $deleteQuery $this->entityManager
                    
        ->getQueryBuilder()
                    ->
        delete()
                    ->
        from(RequestList::ENTITY_TYPE)
                    ->
        where(['yourLinkId' => $entity->getId()]) // replace yourLinkId with the foreign link Id name
                    
        ->build();

                
        $this->entityManager->getQueryExecutor()->execute($deleteQuery);
            }
        }

        Comment


        • #5
          rabii


          Following your instructions, I created the code, but it throws a 500 error:

          use Espo\Custom\Entities\Requestlist;
          use Espo\Custom\Entities\RequestManagement;​
          According to the log, these two files cannot be found. How should I create them?

          HTML Code:
          <?php
          
          namespace Espo\Custom\Hooks\RequestManagement;
          
          use Espo\Core\Hook\Hook\AfterRemove;
          use Espo\Custom\Entities\Requestlist;
          use Espo\Custom\Entities\RequestManagement;
          use Espo\ORM\Entity;
          use Espo\ORM\EntityManager;
          use Espo\ORM\Repository\Option\RemoveOptions;
          
          /**
          * @implements AfterRemove<RequestManagement>
          */
          class DeleteRequestList implements AfterRemove
          {
          private EntityManager $entityManager;
          
          public function __construct(EntityManager $entityManager)
          {
          $this->entityManager = $entityManager;
          }
          
          public function afterRemove(Entity $entity, RemoveOptions $options): void
          {
          $deleteQuery = $this->entityManager
          ->getQueryBuilder()
          ->delete()
          ->from(Requestlist::ENTITY_TYPE)
          ->where(['requestManagementId' => $entity->getId()]) // 替换为 Requestlist 实体中关联的外键字段名
          ->build();
          
          $this->entityManager->getQueryExecutor()->execute($deleteQuery);
          }
          }

          Additionally, if I use Workflows to handle this, it throws the following error:
          ​------------------------------------------------------------------------------
          [2024-07-03 03:38:38] DEBUG: WorkflowManager: Start workflow [$@delete] for [RequestManagement, 6684c7a283d179cf3].
          [2024-07-03 03:38:38] DEBUG: Start workflow rule [6682b90ab18c6e25c].
          [2024-07-03 03:38:38] DEBUG: Condition result [1] for workflow rule [6682b90ab18c6e25c].
          [2024-07-03 03:38:38] DEBUG: Start actions for workflow rule [6682b90ab18c6e25c].
          [2024-07-03 03:38:38] DEBUG: Start workflow rule [6682b90ab18c6e25c].
          [2024-07-03 03:38:38] DEBUG: Workflow\Actions: Start [executeFormula] with cid [0] for entity [RequestManagement, 6684c7a283d179cf3].
          [2024-07-03 03:38:38] ERROR: Workflow [6682b90ab18c6e25c]: Action failed, [executeFormula] [0], Formula: Entity required but not passed..
          [2024-07-03 03:38:38] DEBUG: End workflow rule [6682b90ab18c6e25c].
          [2024-07-03 03:38:38] DEBUG: End running actions for workflow rule [6682b90ab18c6e25c].
          [2024-07-03 03:38:38] DEBUG: End workflow rule [6682b90ab18c6e25c].
          [2024-07-03 03:38:38] DEBUG: End workflow [$@delete] for [RequestManagement, 6684c7a283d179cf3].​

          Comment


          • #6
            Hey,

            You need to create a folder called RequestManagement under custom\Espo\Custom\Hooks and inside that folder create a file called DeleteRequestList.php and then paste the code into the php file.

            I am sure you just need to get the correct name of the two entities. Find the correct name under administration > entity manager >

            Comment


            • #7
              As mentioned before with the signal @delete it won't work as the record has been deleted, a hook is much suitable and easy to implement, you just need to find the correct name of the entities and also make sure that you have the two php files for both (Requestlist and RequestManagement) under custom\Espo\Custom\Entities

              Comment


              • #8
                @rabii
                I cannot find the Requestlist and RequestManagement PHP files under custom\Espo\Custom\Entities. Should I create them manually? If so, how should I create them?​

                Comment


                • #9
                  Did you create these two entities using the UI from administration create entity ?

                  Comment


                  • #10
                    @rabii
                    Yes, I created these two entities through the UI in the entity creation process:
                    • Request Management: RequestManagement
                    • Request Details: Requestlist

                    I found the RequestManagement.php and Requestlist.php files in the following path: custom/Espo/Custom/Controllers

                    However, I cannot find the RequestManagement.php and Requestlist.php files in this path, custom\Espo\Custom\Entities

                    I searched my entire root directory and could only find the RequestManagement.php and Requestlist.php files under the Controllers folder.

                    Could this be related to the type of entity I selected during creation? I selected the base type.
                    Click image for larger version  Name:	image.png Views:	0 Size:	31.5 KB ID:	108076

                    Click image for larger version  Name:	image.png Views:	0 Size:	34.3 KB ID:	108075

                    Comment


                    • #11
                      So just create these two files under custom\Espo\Custom\Entities

                      RequestManagement.php
                      PHP Code:
                      <?php

                      namespace Espo\Custom\Entities;

                      class 
                      RequestManagement extends \Espo\Core\Templates\Entities\Base
                      {
                          public const 
                      ENTITY_TYPE 'RequestManagement';

                          protected 
                      $entityType 'RequestManagement';
                      }

                      Requestlist.php
                      PHP Code:
                      <?php

                      namespace Espo\Custom\Entities;

                      class 
                      Requestlist extends \Espo\Core\Templates\Entities\Base
                      {
                          public const 
                      ENTITY_TYPE 'Requestlist';

                          protected 
                      $entityType 'Requestlist';
                      }

                      Comment


                      • lj4353
                        lj4353 commented
                        Editing a comment
                        @rabii
                        Thank you for your tips, I succeeded.

                      • rabii
                        rabii commented
                        Editing a comment
                        you are welcome.
                        Is the hook working as you want now ?

                      • lj4353
                        lj4353 commented
                        Editing a comment
                        Yes, everything is working perfectly now. Thank you for your help!
                    Working...
                    X