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
  • Hi Marcel Lyyra,

    When using that workaround in the Extension framework you need to make sure that it the following only loaded once per file:  

    use Sugarcrm\Sugarcrm\ProcessManager\Registry; 

    Let's say you have two schedulers where you want to use it:

    ./custom/Extension/modules/Schedulers/Ext/ScheduledTasks/jobA.php
    ./custom/Extension/modules/Schedulers/Ext/ScheduledTasks/jobB.php

    If you add use Sugarcrm\Sugarcrm\ProcessManager\Registry; both of those files would be merged into the following single file and thus cause a PHP error as you described since the same namespace is used twice: 

    custom/modules/Schedulers/Ext/ScheduledTasks/scheduledtasks.ext.php

    So adding this only to jobA.php should also allow to the namespace in jobB.php since both will be merged into one file custom/modules/Schedulers/Ext/ScheduledTasks/scheduledtasks.ext.php where the namespace would be loaded once then. This is the file that is used in the cache/ to actually run both schedulers. 

    Can you confirm if loading this through the Extension framework and making sure the namespace is only loaded in the first file resolves this issue? 

     

  • Hi Dennis Wangerin, thanks for your answer.

    Just to be sure, the line:

    use Sugarcrm\Sugarcrm\ProcessManager\Registry;

    would be used in only one file, but the line:

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

    would still be written before every $bean->save() even in the file that doesn't have the first line on it.

    Is that right?

    Regards

  • Correct, so if you add 

    use Sugarcrm\Sugarcrm\ProcessManager\Registry;

    only in the first file ( didn't test when using it in the second ) you should be able use the following several times in each of the files  

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

    This is what the merged file would look like after Quick Repair for two separate schedulers: 

    ./custom/modules/Schedulers/Ext/ScheduledTasks/scheduledtasks.ext.php

    <?php
    // WARNING: The contents of this file are auto-generated.
    ?>

    <?php
    // Merged from custom/Extension/modules/Schedulers/Ext/ScheduledTasks/custom_job.php

    use Sugarcrm\Sugarcrm\ProcessManager\Registry;
    array_push($job_strings, 'custom_job');
    function custom_job()
    {
        //logic here
        Registry\Registry::getInstance()->drop('triggered_starts');

        return true;
    }

    ?>

    <?php
    // Merged from custom/Extension/modules/Schedulers/Ext/ScheduledTasks/custom_job2.php
    array_push($job_strings, 'custom_job2');
    function custom_job2()
    {
        //logic here
        Registry\Registry::getInstance()->drop('triggered_starts');

        return true;
    }

    ?>
Comment
  • Correct, so if you add 

    use Sugarcrm\Sugarcrm\ProcessManager\Registry;

    only in the first file ( didn't test when using it in the second ) you should be able use the following several times in each of the files  

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

    This is what the merged file would look like after Quick Repair for two separate schedulers: 

    ./custom/modules/Schedulers/Ext/ScheduledTasks/scheduledtasks.ext.php

    <?php
    // WARNING: The contents of this file are auto-generated.
    ?>

    <?php
    // Merged from custom/Extension/modules/Schedulers/Ext/ScheduledTasks/custom_job.php

    use Sugarcrm\Sugarcrm\ProcessManager\Registry;
    array_push($job_strings, 'custom_job');
    function custom_job()
    {
        //logic here
        Registry\Registry::getInstance()->drop('triggered_starts');

        return true;
    }

    ?>

    <?php
    // Merged from custom/Extension/modules/Schedulers/Ext/ScheduledTasks/custom_job2.php
    array_push($job_strings, 'custom_job2');
    function custom_job2()
    {
        //logic here
        Registry\Registry::getInstance()->drop('triggered_starts');

        return true;
    }

    ?>
Children