$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?

  • 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

  • You can maybe utilitize the before relationshp add and before_Relationship delete hooks? 

  • 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

  • Hello John,

    As I can see, this logic hook is easily configurable with the Logic Builder co-code tool and the subscriber's Sugar Admin with no coding skills could make it.

    Here is the example - on S_SerialNumber record is assigned to another Account, do read previous and new Account names, then concatenate them and put into Description:



    The drawing for this behavior is fairly simple (follow the white line to read):



    And zip to install with ModuleLoader is got in a click:
    BothAccPreviousAndAccNew.zip

    Let me know if any questions

    Best Regards,
    Dmytro Chupylka

    integroscrm.com
    We make work in Sugar CRM system faster, more convenient and efficient

  • I understand the power of your software but  its not in our budget and wont be, 

  • Thanks for the high evaluation of the Logic Builder no-code tool

    Do you see any other reasons for the company decisions but economical ones?

    If Sugar subscriber expects that simple configurable tasks might take more than 11 days of experienced developer's services within a single year and even if the developer's real rate is £40 per hour, then this amount already exceeds 365 days of access for Sugar no-code configuring with Logic Builder.
    And this estimation does not include spends on custom code maintenance versus simple logic refresh in the no-code tool.

    So why not employ developers' experience for the Sugar development tasks and use no-code tools for simple Sugar configuring?

    The cost of ownership is the reason why vendors provide their subscribers with no-code tools like Sugar Studio and Logic Builder - according to Gartner, by 2024 low-code application development might be responsible for more than 65% of application development activity

    Therefore, each company decides itself what to wait for and when to start benefit from the no-code approach.

    Here is the feedback of one that started with no-code:
    sugarclub.sugarcrm.com/.../22348

    Best Regards,
    Dmytro Chupylka

    integroscrm.com
    We make work in Sugar CRM system faster, more convenient and efficient

  • Hi John,
    Is the objective to track changes to this field? If so, does the standard Audit log not provide a desirable solution?
    https://support.sugarcrm.com/Knowledge_Base/User_Interface/Historical_Summary_vs._Activity_Stream_vs._Change_Log/ 

    There is also a method using calculated fields with example by Sugar knowledgebase here:
    https://support.sugarcrm.com/Knowledge_Base/Studio_and_Module_Builder/Sugar_Logic/Sugar_Logic_Walkthrough_Tracking_a_Fields_History/ 

    CRM Business Consultant

  • Hi Vincent, the solution I used was to access the arguments array

    $args['dataChanges']

    which contained the array of any updated fields and then searched that for any keys which matched the fields I wanted

                if ($args['isUpdate'] == 1) {
                    // Update Cache
                    $updateCache = false;
                    $cacheFields = array(
                        'warrantystart_c',
                        'warrantystatus_c',
                        'accounts_s_serialnumber_1accounts_ida',
                        'status',
                        'dealer_id_c',
                        'p_product_s_serialnumber_1p_product_ida',
                    );
    
                    foreach ($cacheFields as $field) {
                        if (array_key_exists($field, $args['dataChanges'])) {
                            $updateCache = true;
                        }
                    }
                }

  • thanks  I ended up using the $args['dataChanges'] as i could loop through this and see if any of my required fields had changed as well as the relationship and if any of them had fire off an update to our caching server to update the printer data