AnsweredAssumed Answered

After_save logic hook not executed when a Process Definition creates a new record

Question asked by Artis Plocins on Jul 20, 2017
Latest reply on Jul 24, 2017 by Artis Plocins

This logic hook checks whether a record is new and a checkbox is checked, then links an account to the record based on the related contact.

 

It works perfectly when a user creates a new record, but not when created by a process definition. The process definition checks the checkbox so that this logic hook would proceed with the code.

 

$hook_array['after_save'][] = array(
    1,
    'When creating a Supplier Revenue record, if the Contact field is set, but Account field is empty, fill the Account field with the account related to the contact.',
    'custom/modules/SR_Supplier_Revenue/AccountLogicHook.php',
    'Record',
    'setAccount'
);
class Record
{
    function setAccount($bean, $event, $arguments)
    {
        // Only new records
        if ($arguments['isUpdate']) return;
        // If the checkbox says the account is not added to the Supplier Revenue
        if (!$bean->no_account_c) return;

        // Relationship names
        $rel_contacts = 'contacts_sr_supplier_revenue_1';
        $rel_accounts = 'accounts_sr_supplier_revenue_1';

        // If relationships loaded successfully, get related contacts as beans
        if ($bean->load_relationship($rel_contacts) && $bean->load_relationship($rel_accounts)) {
            $relatedContacts = $bean->$rel_contacts->getBeans();
            // If related contacts found, reorder the array
            // and select the first element, which is the parent
            if (!empty($relatedContacts)) {
                reset($relatedContacts);
                $parentContact = current($relatedContacts);
                // Load parent contact accounts relationship and get accounts as beans
                if ($parentContact->load_relationship('accounts')) {
                    $relatedAccounts = $parentContact->accounts->getBeans();
                    $parentAccount = false;
                    // Get the parent account of the parent contact account
                    // and add the relationship to that account in the
                    // supplier revenue record using the ID of the account
                    if (!empty($relatedAccounts)) {
                        reset($relatedAccounts);
                        $parentAccount = current($relatedAccounts);
                        $bean->$rel_accounts->add($parentAccount->id);
                        $bean->no_account_c = false; // Account added
                        $bean->save();
                    }
                }
            }
        }

    }
}

Why does it not work when a record is created by a process definition? Does process definition create records technically in a different way? How can I make this logic hook work for process definitions as well?

 

It would make sense for it to not work if process definitions create records like:

  1. Create a new, blank record (not ticking the checkbox)
  2. Edit the new record with the values the new record is supposed to have as defined in the process definition (ticks the checkbox now, but the record is no longer a new record for the logic hook)

 

Is there a technical explanation somewhere on how exactly process definitions work under the hood?

Outcomes