Process Definition not Triggered from $bean->save()

Hi everyone.

I am struggling to figure out why, but a process definition that uses the "Changes" method is not firing when a logic hook is using $bean->save(). The changes is a simple "No to Yes" but when saving the record it doesn't go into the processes.

I tried doing:


$dataChanges = $bean->dataChanges;
$bean->save();
$bean->dataChanges = $dataChanges;

But seemingly had no luck. Has anyone seen this before?

Thanks

Dan

Parents
  • Hi Daniel,

    You are using which version of SugarCRM?

    As from version 10.2 $bean->dataChanges is no longer supported. You need to use $bean->stateChanges instead.

    Hope it will work fine. Have a good day!

  • Hi Maryam. 

    I am on 10.0.3 but I did try $statechanges but same issue I'm afraid. Any other suggestions would be really appreciated! 

    Thank you,

  • You are using which logic hook?

    Also, are you trying to call some function in your logic hook, and it is not calling? Is it the real problem? 

  • Hi Maryam, I am using before and after save. The issue is when doing $bean->save() in after save it doesn't seem to fire the process.

    I can see $bean->dataChanges is seeing the change which is the odd part.

  • Also slight note on this, if i comment out $bean->save() the process works just fine... So something with save function is causing the issue. 

  • Actually, process definition fires the logic hook. So for this, you need to re-check the design of your process definition that your implemented criteria are matching to trigger the process or not. 

    I believe there is a problem in the design of your PD, or maybe the criteria are not matching that is why the process is not triggering. When it will trigger then it will call your logic hook. 

  • Hi Maryam. The process definition simply checks if a field has changed from "No" to "Yes". It's seemingly that the $bean->save() is affecting the firing of the workflow. I'm not entirely sure why at the moment. But will keep looking!

  • Are you calling the save function of the same bean?

    If yes then it is wrong. Because by this it will be stuck in infinite loop. 

Reply Children
  • I am however I am stopping the infinite loop using the sugar recommend approach, unless I am still not doing something right? :) 

    Fullscreen
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    function contactsAfterSave($bean, $event, $arguments)
    {
    $GLOBALS['log']->error('### contactsAfterSave Started');
    if (!isset($bean->ignore_update_c) || $bean->ignore_update_c === false)
    {
    $GLOBALS['log']->error('### contactsAfterSave Update');
    if ($bean->contact_type_c == 'Child')
    {
    $this->checkForACPFieldChanges($bean, $event, $arguments);
    }
    if ($bean->contact_type_c == 'Contact')
    {
    $this->checkForProfDetailsChanges($bean,$event,$arguments);
    }
    $bean->ignore_update_c = true;
    $dataChanges = $bean->dataChanges;
    $bean->save();
    $bean->dataChanges = $dataChanges;
    }
    }
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

  • We should never call $bean->save in any before_save,after_save logic hook of the same bean. Because before save will call the save and due to this again before save logic hook will call and it will so on. 

  • Hi Maryam. 

    The code I provided is using the $bean->ignore_update_c so the code is not saving more than once. I don't think this is the issue for what I am experiencing.

    Thanks,

  • I am unable to understand that why you are added the following lines in your code:
    $bean->ignore_update_c = true;
    $dataChanges = $bean->dataChanges;
    $bean->save();
    $bean->dataChanges = $dataChanges;

    What is the purpose? You are not updating anything in bean apart from ignore_update_c and ignore_update_c need to use when calling save. So you should not use both. 

    It is like you have added both a problem and a solution by ownself. 

  • Hi Maryam.

    The function "checkForAcpFieldChanges" uses stored rows to check if data has changed:

    e.g.

    Fullscreen
    1
    2
    3
    4
    if ((isset($bean->stored_fetched_row_c) && $bean->stored_fetched_row_c["$crmField"] != $bean->$crmField) && $bean->acp_update_c != 1)
    {
    $bean->acp_update_c = 1;
    }
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    I think I understand what you are saying now, but how do I set the $bean to recognise a field change without calling $bean->save() in the after save?

  • Hi - I just want to say a massive thank you! I never even realised you could process the whole record in before_save, that solves so many other issues.

    For anyone else reading, "Fetched Row" gets the before value and the $bean in before_save actually has the new value!

  • For data changes why you are not getting this info from arguments?

    function contactsAfterSave($bean, $event, $arguments) {

           $arguments['dataChanges']['YourFieldName']['before']   //contains old value

           $arguments['dataChanges']['YourFieldName']['after']   //contains new value

    }

    By this, you will simply recognize :) 

  • Welcome :)

    Yes, For before_save logic hook it is the flow.
    For the after_save logic hook, we need to look into the $arguments for getting old and new values.