Calling custom code with custom mass action button

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bandtank
    Active Community Member
    • Mar 2017
    • 379

    Calling custom code with custom mass action button

    I'm following this guide. I thought about trying this as well, but the official way is probably better.

    Here are my files:

    custom/Espo/Custom/Resources/i18n/en_US/EntityName.json:
    Code:
      ...
      },
        "massActions": {
        "processVisits": "Process Visits"
      }
    }
    custom/Espo/Custom/Resources/metadata/clientDefs/EntityName.json:
    Code:
    {
      "controller": "controllers/record",
      "boolFilterList": [
        "onlyMy"
      ],
      "massActionList": [
        "__APPEND__",
        "processVisits"
      ],
      "checkAllResultMassActionList": [
        "__APPEND__",
        "processVisits"
      ],
      "massActionDefs": {
        "processVisits": {
          "handler": "custom:process-visits-handler",
          "initFunction": "initProcessVisits"
        }
      }
    }
    client/custom/src/process-visits-handler.js
    Code:
    define('custom:process-visits-handler', [], function () {
    
      var ProcessVisitsHandler = function (view) {
        this.view = view;
      };
    
      _.extend(ProcessVisitsHandler.prototype, {
    
        initProcessVisits: function() {
          console.log("got here");
        },
    
        actionProcessVisits: function (data) {
          console.log("got here2");
        },
      });
    
      return ProcessVisitsHandler;
    });
    I see the button in the mass action menu, but I don't know what to do from here. I am currently running a job on a schedule to perform an action, but I would like it to be called on demand using the mass action button. How do I call a PHP function using the button? I saw this, but I can't figure out how to use it or what it means:
    You can use Mass Action framework to handle mass actions in the back-end. Available since v6.2.0. Send a POST request to MassAction URL with data passed into the action method.
    A few other things - I could not get the button to work until I named everything exactly the same way: processVisits. The functions in the js file also have to be named the same way: initProcessVisits and actionProcessVisits. This detail is not mentioned in the guide, which is confusing.
    Last edited by bandtank; 05-08-2022, 07:22 PM.
  • rabii
    Active Community Member
    • Jun 2016
    • 1250

    #2
    cron jobs are executed based on given time. i think you can implement your cron job code in JS so that it could be called every time you click the mass action. if you share your cron job code someone could help you.
    Rabii
    Web Dev

    Comment

    • bandtank
      Active Community Member
      • Mar 2017
      • 379

      #3
      There is still a lot I don't know, but here is a working example that executes a PHP function in the controller.

      client/custom/src/process-visits-handler.js:
      Code:
      actionProcessVisits: function (data) {
        Espo.Ajax.postRequest('EntityName/action/ProcessVisits', {
          ids: data.params.ids,
        });
      },
      custom/Espo/Custom/Controllers/EntityName.php:
      Code:
      <?php
      
      namespace Espo\Custom\Controllers;
      use Espo\Core\Exceptions\BadRequest;
      use Espo\Core\Api\Request;
      
      class EntityName extends \Espo\Core\Templates\Controllers\Base
      {
        public function postActionProcessVisits(Request $request) : bool {
          $data = $request->getParsedBody();
          if(empty($data->ids)) {
            throw new BadRequest('No rows are selected');
          }
      
          $GLOBALS['log']->debug('ids', [$data]);
          return true;
        }
      }

      Comment

      • rabii
        Active Community Member
        • Jun 2016
        • 1250

        #4
        yes you can still send ajax request to a controller but not a job.
        Rabii
        Web Dev

        Comment

        Working...