Triggering Advanced Workflow processes in PHP customizations

An Advanced Workflow process can only be triggered once per PHP process or HTTP request. This is intended to prevent Sugar Administrators from defining infinitely looping processes. (A real catastrophe!) But what does this mean for PHP customizations?

Assume that you have an Advanced Workflow process enabled for the Contacts module that performs an update on this Contact each time it is saved. If you have an Accounts after_save logic hook that performs an update on each related Contact SugarBean then the process will only run against the first related Contact. Any other related Contact that gets saved during your logic hook execution will not have a process run.

This affects not just logic hooks but any other class of PHP customization such as custom API endpoints or jobs.

Workaround

If you really need to run that process more than once in the same request, here is a workaround:

use Sugarcrm\Sugarcrm\ProcessManager\Registry;

...

Registry\Registry::getInstance()->drop('triggered_starts');

Calling this method will clear the internal Advanced Workflow registry that keeps track of the triggered process starts. After calling this method, the same process can then be triggered again inside the same PHP process or HTTP request.

Careful use of this method can make sure that PHP customizations play nicely with processes defined in Advanced Workflow.

Parents
  • You could also try to use aliases:

    use Sugarcrm\Sugarcrm\ProcessManager\Registry as RegistryA;
    ...
    use Sugarcrm\Sugarcrm\ProcessManager\Registry as RegistryB;

    Etc.

    This would allow you to define multiple local usages of this class within the same file (scheduledtasks.ext.php) without causing name conflicts. You would just need to make sure your alias names are unique.

    PHP: Using namespaces: Aliasing/Importing - Manual 

  • Hi Matt Marum, Thanks for your help,

    In this case, using the alias I would use in file 1:

    use Sugarcrm\Sugarcrm\ProcessManager\Registry as RegistryA;

    and in File 2:

    use Sugarcrm\Sugarcrm\ProcessManager\Registry as RegistryB;

    right?

    Then how would I use the second line? 

    Registry\Registry::getInstance()->drop('triggered_starts');

    Would it stay the same or should I change it to:

    RegistryA\Registry::getInstance()->drop('triggered_starts');

    OR

    Registry\RegistryA::getInstance()->drop('triggered_starts');

    OR

    RegistryA\RegistryA::getInstance()->drop('triggered_starts');

    Can you help me with this issue?

    Regards,

  • The following works without causing any errors.

    use Sugarcrm\Sugarcrm\ProcessManager\Registry as RegistryA;
    use Sugarcrm\Sugarcrm\ProcessManager\Registry as RegistryB;

    function test()
    {
        RegistryA\Registry::getInstance()->drop('triggered_starts');
        RegistryB\Registry::getInstance()->drop('triggered_starts');
    }

    The alias from my previous example is for the namespace and not the class name. But conceptually, it all just comes down to avoiding name conflicts within your PHP files.

    For example, you could just as easily use the following which uses an alias for the class name instead of the namespace.

    use Sugarcrm\Sugarcrm\ProcessManager\Registry\Registry as RegistryA;
    use Sugarcrm\Sugarcrm\ProcessManager\Registry\Registry as RegistryB;

    function test()
    {
        RegistryA::getInstance()->drop('triggered_starts');
        RegistryB::getInstance()->drop('triggered_starts');
    }
  • Hi Matt Marum, thank you for the clarification, helped a lot.

    Regards.

Comment Children
No Data