AnsweredAssumed Answered

sugarcrm scheduled jobs not triggering workflows

Question asked by Daniel Alexander Montoya Herrera on Mar 22, 2019

Hello to everyone.

 

 

I have a custom scheduler task that get some accounts from a query, then it change a value on a field and then save it with php code.

 

At first only the first records that the query returns trigger a WF then the other won't do it.

 

Then I added a line to force the WF to trigger after the record is saved.

 

 

We try to force the task to load over 200 accounts and then only 60 did trigger the WF the others didn't.

 

If I try to edit one of the acocunts that didn't trigger the WF and then save it within the UI on crome it triggers the WF manually.

 

 

Whats the proper way to periodically save and edit some accounts to trigger a WF?

 

 

Also, the WF is waiting for "any update" with that field on "1" on the WF design start and then it change the value of the field to "2", so if a user edit the record again, the WF will not trigger endless again.

 

 

 

 

 

 

<?php
/*
Se requiere para Nutresa que a las 5:30am
el sistema envíe un correo a todos los usuarios que tengan una tarea
en estado "No iniciada" o una llamada "Planificada", cuya fecha de vencimiento sea el día presente.

Igualmente se requiere que envíe un correo al usuario registrado en el "Informa a"
de los usuarios asignados a tareas en estado diferente a "Completada" o llamadas en
estado diferente a "Realizada", cuya fecha de vencimiento sea el día anterior.
*/
use Sugarcrm\Sugarcrm\ProcessManager\Registry;
$job_strings[] = 'tarea_accounts_retraso_ctrl_wf';
function tarea_accounts_retraso_ctrl_wf(){
$GLOBALS['log']->security("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
$GLOBALS['log']->security("******************************************************");
$GLOBALS['log']->security("Inicio-tarea_accounts_retraso_ctrl_wf. ".date("Y-m-d h:i:s"));
$GLOBALS['log']->security("******************************************************");
$return = true;
try{
$query = "
SELECT
accounts_cstm.id_c AS idAccounts
FROM
accounts
LEFT JOIN accounts_cstm ON accounts.id = accounts_cstm.id_c
WHERE
accounts.deleted = 0 AND
accounts.account_type = 'Customer' AND
(
accounts_cstm.sasa_retrasoctrlworkflow_c = '' OR
accounts_cstm.sasa_retrasoctrlworkflow_c IS NULL
)
;
";

global $db;
$result = $db->query($query);
while ($row = $db->fetchByAssoc($result)){
$idAccount = $row['idAccounts'];
if(!empty($idAccount))
{
$Account = BeanFactory::getBean('Accounts', $idAccount,array('disable_row_level_security' => true));
if(!empty($Account->id)){
$Account->sasa_retrasoctrlworkflow_c = '1';
$Account->save();
Registry\Registry::getInstance()->drop('triggered_starts');
$GLOBALS['log']->security("\n{$Account->id} -> {$Account->name} = Update RetrasoCtrlWorkFlow");
}
}
}
}
catch (Exception $e) {
$GLOBALS['log']->security("ERROR: ".$e->getMessage());
$return=false;
}
$GLOBALS['log']->security("******************************************************");
$GLOBALS['log']->security("Fin-tarea_accounts_retraso_ctrl_wf. ".date("Y-m-d h:i:s"));
$GLOBALS['log']->security("******************************************************\n\n\n\n");
return $return;
}

 

 

The first bold line will set the empty field to "1" and then save() the current record on the while loop returned by the query, then the second bold line will force the WF definition to trigger at any update if that field is set to "1", then the  WF will set the field to "2", do some logic and then finish.

 

 

Please, let me know If should fill a case to sugar support instead.

Outcomes