Trigger a BPM-process to start by date field

Hi, 

We have the need to create a BPM-process that should be triggered to start after the end date of a contract is passed. Do any of you have any examples on how this could be configured?

Best regards, 

Frida Yngve

  • Hello

    To have a solution that scales the best approach is to implement a custom Scheduler that will run daily to identify the records that reached the contract end date. 
    That scheduler should then update a field in the target module that you can use as a trigger for your BPM.

    Let me know if this helps. 

    André 

  • Hi Frida,

    I don';t have any custom coding in our installation.  I've taken a simple approach on this using a Business Rule within existing processes on a parallel path at the start, OR with a separate BPM Processes that runs until the date is reached, or some other event triggers closure. 

    I added a filed in the module called "Date Today"

    and I created a Business Rule with two conditions for the "Status Check Date" field:

    The Rule is in a loop that checks the rule and either move on to the next step or waits one day and checks the rule again.

    In some modules, I created a separate process to trigger the alert or action when the date was coming, arrived, or past by some amount.  I needed to add a parallel wait event to close the process for other situations (like the closure of the record for some other reason).

    Bud Hartley | Cape Foulwind, NZ (and Oregon, USA)

  • Hello

    While I understand and support the goal of avoiding custom code whenever possible, I believe that, in this exact scenario, the benefits outweigh the disadvantages. 

    Creating a custom scheduler will still provide some control to the admin for tweaking the job start time and frequency, and enabling/disabling it if needed. 

    And we can outsource to our custom scheduler one thing that BPM doesn't do very well which is checking what day it is. 

    The major benefit is to prevent the creation of too many in-flight BPM processes, which depending on the instance activity may lead to performance issues and storage concerns in the long run. A good rule of thumb is to keep the inflight processes below 10 000 for a given instance. 


    As asked for an example you can check a very simplistic approach to have this working. 

    1 - Create a custom field in the contracts module called expired_trigger_flag_c

    2 - Create a new custom scheduler  file custom/Extension/modules/Schedulers/Ext/ScheduledTasks/contract_expire_monitor.php and run a QRR

    <?php
    
    array_push($job_strings, 'contract_expire_monitor');
    function contract_expire_monitor()
    {
    
        $contractsBean = \BeanFactory::newBean('Contracts');
        $query = new \SugarQuery();
        $query->from($contractsBean);
        $query->where()->queryAnd()
        ->dateRange('end_date', 'yesterday')
        ->notEquals('expired_trigger_flag_c', 1);
    
    
        $query->select(array('id'));
    
        $ExpiredContractIds = $query->execute();
    
        $count = count($ExpiredContractIds);
    
        if ($count == 0) {
            $GLOBALS['log']->fatal('No contracts expired today');
        } elseif ($count == 1) {
            //one contract was expired
            $bean = BeanFactory::getBean('Contracts', $ExpiredContractIds[0]['id']);
            $bean->expired_trigger_flag_c = true;
            $bean->save();
            $GLOBALS['log']->fatal('One contract expired today. Contract ID: ' . $bean->id);
        } else {
            // more than one contract was expired
            foreach ($ExpiredContractIds as $contract) {
                $bean = BeanFactory::getBean('Contracts', $contract['id']);
                $bean->expired_trigger_flag_c = true;
                $bean->save();
                $GLOBALS['log']->fatal('Multiple contracts expired today. Contract ID: ' . $bean->id);
            }
        }
    
    
    
    
            return true;
    }


    3 - Create a Scheduler in Admin > Scheduler pointing to the new function: 



    4 -  Use the created field as a Trigger to start the BPM when the Contract is Expired. 





    I hope this helps 


    André


  • Hi

    regarding this:

    "keep the inflight processes below 10 000 for a given instance. "

    how can we check please?

  • Hi

    Great question, thanks!

    A possible way to do it is by creating an Advance Report with the following query: 

    SELECT pmse_bpmn_process.name, COUNT(*) AS num_processes FROM pmse_bpm_flow LEFT JOIN pmse_bpmn_process 
    ON pmse_bpm_flow.pro_id=pmse_bpmn_process.id 
    WHERE pmse_bpm_flow.cas_flow_status="WAITING" 
    GROUP BY pmse_bpmn_process.name 
    ORDER BY pmse_bpm_flow.date_entered DESC;


    This will list the "Waiting" Processes by Process Definition. 

    I hope this helps.