Search code examples
suitecrm

How to check relationship field value is changing


i'm working for History module in Suite crm. My module is tracking history of changing relationship between account and contact. I'm using Notes module to make history.

My expect is when add relationship contract to account then add a Note record.

function addRelationshipHook($bean, $event, $arguments){
        if($arguments['related_module']=='Accounts'){
            if($bean->account_id != $bean->rel_fields_before_value['account_id'] ){
                $noteBean7 = BeanFactory::newBean('Notes');
                $noteBean7->name = "Premise created for Customer";
                $noteBean7->parent_type = "Contacts";
                $noteBean7->parent_id = $bean->id;
                $noteBean7->contact_id = $bean->id;
                $noteBean7->assigned_user_id = $bean->assigned_user_id;
                $noteBean7->save();
            }

        }

    }

$hook_array['before_relationship_add'][] = Array(78, 'addRelationshipHook', 'custom/modules/Contacts/ContactsLogicHook.php','ContactsLogicHook', 'addRelationshipHook'); 

When I change relationship in contact edit page, it works. But when I add relationship contact in account, it does work, function is called but $bean->account_id is same $bean->rel_fields_before_value['account_id'].


Solution

  • I finger out my problem. Check $bean->account_id and $arguments['related_id']. It works

        function addRelationshipHook($bean, $event, $arguments){
            if($arguments['related_module']=='Accounts'){
                if($bean->account_id != $bean->rel_fields_before_value['account_id'] ){
    // when you update account on contact edit page
                    $noteBean7 = BeanFactory::newBean('Notes');
                    $noteBean7->name = "Premise created for Customer";
                    $noteBean7->parent_type = "Contacts";
                    $noteBean7->parent_id = $bean->id;
                    $noteBean7->contact_id = $bean->id;
                    $noteBean7->assigned_user_id = $bean->assigned_user_id;
                    $noteBean7->save();
                }else{
    
                    if($bean->account_id != $arguments['related_id'] ){
    //when you add relationship contact to account
                        $noteBean7 = BeanFactory::newBean('Notes');
                        $noteBean7->name = "Premise created for Customer";
                        $noteBean7->parent_type = "Contacts";
                        $noteBean7->parent_id = $bean->id;
                        $noteBean7->contact_id = $bean->id;
                        $noteBean7->assigned_user_id = $bean->assigned_user_id;
                        $noteBean7->save();
                    }
                }
            }
    
        }