Documentation

Action

Class description


Presently, there are five types of [link=14005980]operations[/link] with individual specifics.

To avoid expanding class hierarchy, an individual class hierarchy was created with additional action for each entity type to be managed when launching operations.

Class Bitrix\Crm\Service\Operation\Action is abstract with a single method:

abstract public function process(Item $item): Result;

This method can perform checks; in case of an error it must be added to executed method result.

You can modify status of [link=13985532]object[/link] $item, if this action is pre-saving.

When action is performed after saving, you can call $item->save() once again.

Adding an action is performed via the method Service\Operation::addAction().

When adding actions, full-fledged descendant is optional, an anonymous class is sufficient.

Presently, additional actions are implemented as follows:

  • Action\CheckRestrictions - limits checks.
  • Action\ClearCache - cache reset.
  • Action\MoveToBin - moving to recycle bin.
  • Action\SendEventCompatible - sending and processing events. This class has descendants for various types of events.

Example


use Bitrix\Crm\Service;

$entityTypeId = 128;

$factory = Service\Container::getInstance()->getFactory($entityTypeId);

$item = $factory->getItem(100);

$operation = $factory->getUpdateOperation($item);

$operation
    ->addAction(
        Operation ::ACTION_BEFORE_SAVE,
        new class extends Operation \Action {
            public function process(Item $item): Result
            {
                $result = new Result();
                
                if ($item->getId() > 100)
                {
                    $result->addError (new Error('id is too big!' ));
                }
                
                return $result;
            }
        }
    )
    ->addAction(
        Operation ::ACTION_AFTER_SAVE,
        new class extends Operation \Action {
            public function process(Item $item): Result
            {
                // send notification, log changes etc. 
                return new Result();
            }
        }
    )
;
© «Bitrix24», 2001-2024
Up