Fetched Row Not Working on Relate Field Logic Hook

Hi Everyone,

Is it possible to use the fetched_row function on relate field (not relationship)?  

Currently we have the code below written in a before save logic hook, but the current account field is not pulling back a value using fetched_row.

$currentAccount = $bean->fetched_row['account_user_c'];
$GLOBALS['log']->fatal('Current Account: ' . $currentAccount);
$newAccount = $bean->account_user_c;
$GLOBALS['log']->fatal('New Account:: ' . $newAccount);

Any suggestions? Thanks!

  • Hello Dan,

    By design, the text value for the related field may fetch incorrectly for before_save.
    However, there is a solution.

    Below is the Logic Builder flowchart design for the before_save logic hook that logs "account_user_c" Relate type record data

    Please note, that by logging not only the account_user_c" changes but also an ID for that relate field, you will find both ID values, previous and new, are fetched correctly.

    Therefore in the flowchart, the previous and new IDs are used to retrieve FullNames of the users to solve your task.

    here are the results in the Log:

    As you can see, the previous user ID is "seed_sally_id" and FullName is "Sally Bronsen", while the new user id is "seed_will_id" and FullName is Will Westin

    In the same time, the previous and new "account_name_c" values are incorrect, so please do not rely on them

    I hope this helps

    Best Regards,
    Dmytro Chupylka

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

  • Dan,

    Although I don't recall using it in the fetched_row array I believe the field should be available - I see no reason why not.

    However, it doesn't look as if you have the correct field name and usage in your code. When you create a Relate type field in Studio it will actually create a pair of fields, a pseudo one to hold the text of (generally) the "name" field of the related record and a real database one to hold the id value. Studio will normally auto-create the id one using the name you choose. The plain "name" field may not hold a bean value though and I don't think you can create a separate instance using it, you need to collect a bean using the id field.

    In your case, it looks to me like you might have created a field in Studio and given the field a name of "account_user" and Studio has auto-added the "_c" suffix. If that is the case then there will be a second field in both the fields_meta_data database table and in the Vardefs called "account_user_id_c" and you then need to use that field to grab the related bean using a BeanFactory call.

    So, second guessing your field names, I'd say you need to put this:

    $currentAccount = BeanFactory::retrieveBean('Accounts', $bean->fetched_row['account_user_id_c']);

    (assuming here of course that account_user_c holds a related Account record - if it is a related User then you need to adjust the BeanFactory call accordingly)

    Hopefully, that should pull in the bean record of the fetched row value for your related Account.

    [EDIT] In your original code I would say that your line: "$newAccount = $bean->account_user_c;" is not actually pulling back the related Account record but is simply storing the name of the related Account. It is likely that this field is not filled in on the fetched_row array either as the relate will likely be made as part of the Save event. So you might be seeing that the $currentAccount and $newAccount variables hold the same value during the before_save hook or that the $currentAccount is empty as it has not been filled yet. However, the $bean->fetched_row['account_user_id_c'] should hold the previous value for that field when changed (I hope).

    Thanks,

    JH.

  • I forgot to try the user_id_c field. This is perfect and works great with the logic we have in place. Thank you!