$bean->stored_fetched_row_c, logic hooks and relate fields

Im trying to detect a change in a relationship through a logic hook but I cant seem to get the old value to compare it to the new.

I have a serial number module which has a relationship to accounts

Accounts -> one-to-many -> S_SerialNumber

this is visible in my module view and i can get the relationship data if i query the S_SerialNumber

    "accounts_s_serialnumber_1_name": "ID Print",
    "accounts_s_serialnumber_1": {
        "name": "ID Print",
        "id": "cac71e64-7c2c-11eb-9bf6-066c2def7d44",
        "_acl": {
            "fields": {
                "business_center_name": {
                    "create": "no",
                    "write": "no",
                    "license": "no"
                },
                "business_center_id": {
                    "create": "no",
                    "write": "no",
                    "license": "no"
                }
            },
            "_hash": "e376f7492abb281a71d6a0860441877a"
        }
    },
    "accounts_s_serialnumber_1accounts_ida": "cac71e64-7c2c-11eb-9bf6-066c2def7d44",

the problem I'm having is I want to detect if this relationship is changed on an after_save logic hook

My before_save hook captures the data with the 

        public function get_record($bean, $event, $arguments)
        {
            $bean->stored_fetched_row_c = $bean->fetched_row;
        }

However $bean->fetched_row does not include the relationship data for me to then compare

If I use the $bean->load_relationship() it only gets the updated data

Anybody know how you compare a relationship between a before and after save logic hook?

Parents
  • Hello john, exist a var for relationship fields into SugarBean

    I'm been using into before_save LH the Related Module, for example:

    if (
        $args['isUpdate'] == 1
        && !empty($bean->rel_fields_before_value[$idrel])
        && $bean->$idrel != $bean->rel_fields_before_value[$idrel]
    ) {

    In this code $idrel is the value the "join_key_lhs", this value can be found in the php file of the relationship between the Accounts and your custom module.

    The directory where you find the metadata is: "custom/metadata/*.php"

    The file has this structure:

    <?php
    // created: 2020-12-03 15:33:40
    $dictionary["accounts_opportunities_1"] = array (
      'true_relationship_type' => 'one-to-many',
      'from_studio' => true,
      'relationships' => 
      array (
        'accounts_opportunities_1' => 
        array (
          'lhs_module' => 'Accounts',
          'lhs_table' => 'accounts',
          'lhs_key' => 'id',
          'rhs_module' => 'Opportunities',
          'rhs_table' => 'opportunities',
          'rhs_key' => 'id',
          'relationship_type' => 'many-to-many',
          'join_table' => 'accounts_opportunities_1_c',
          'join_key_lhs' => 'accounts_opportunities_1accounts_ida',
          'join_key_rhs' => 'accounts_opportunities_1opportunities_idb',
        ),
      ),
      'table' => 'accounts_opportunities_1_c',
      'fields' => 
      array (
        'id' => 
        array (
          'name' => 'id',
          'type' => 'id',
        ),
        'date_modified' => 
        array (
          'name' => 'date_modified',
          'type' => 'datetime',
        ),
        'deleted' => 
        array (
          'name' => 'deleted',
          'type' => 'bool',
          'default' => 0,
        ),
        'accounts_opportunities_1accounts_ida' => 
        array (
          'name' => 'accounts_opportunities_1accounts_ida',
          'type' => 'id',
        ),
        'accounts_opportunities_1opportunities_idb' => 
        array (
          'name' => 'accounts_opportunities_1opportunities_idb',
          'type' => 'id',
        ),
      ),
      'indices' => 
      array (
        0 => 
        array (
          'name' => 'idx_accounts_opportunities_1_pk',
          'type' => 'primary',
          'fields' => 
          array (
            0 => 'id',
          ),
        ),
        1 => 
        array (
          'name' => 'idx_accounts_opportunities_1_ida1_deleted',
          'type' => 'index',
          'fields' => 
          array (
            0 => 'accounts_opportunities_1accounts_ida',
            1 => 'deleted',
          ),
        ),
        2 => 
        array (
          'name' => 'idx_accounts_opportunities_1_idb2_deleted',
          'type' => 'index',
          'fields' => 
          array (
            0 => 'accounts_opportunities_1opportunities_idb',
            1 => 'deleted',
          ),
        ),
        3 => 
        array (
          'name' => 'accounts_opportunities_1_alt',
          'type' => 'alternate_key',
          'fields' => 
          array (
            0 => 'accounts_opportunities_1opportunities_idb',
          ),
        ),
      ),
    );

    Kind Regards

  • Thank you that has worked great for me and thanks for point to the meta files saves having to get the info from studio. Ill post my working code shortly to close this question

Reply Children