Problem with Logic Hook trying to copy User object from related module field

Warm Greetings to everyone. My first post here as I am starting to Hesitantly write code for the system.

I am trying to create a logic hook but does not seem to work.

What I am trying to do is copy a User object from a parent module (Contacts) to the current module (Notes) in a custom field.
What I have done:

  1. I have created a custom field of RELATE type (to USER module) in NOTES (in order to hold the USER object) - I am not sure if such logic is correct :blush: The name of the custom field is Field Name: contact_created_by_c

  2. Then created the two files for the Logic Hook:
    custom/modules/Notes/Logic_Hooks.php:


<?php
$hook_version=1;
$hook_array=Array();
$hook_array['before_save'] = Array();
$hook_array['before_save'][] = Array(1, 'copyContactCreatedBy', 'custom/modules/Notes/notes_hook.php', 'logic_hooks_class', 'before_save_method');
?>

custom/modules/Notes/notes_hook.php:


<?php
if (!defined('sugarEntry') || !sugerEntry) die('Not A Valid Entry Point');

class logic_hooks_class {

        function before_save_method($bean, $event, $arguments) {

                if ($bean->load_relationship('contacts'))
                {
                        $relatedBeans = $bean->contacts->getBeans();
                        $parentBean = false;
                        if (!empty($relatedBeans))
                        {
                                reset($relatedBeans);
                                $parentBean = current($relatedBeans);

                                $bean->contact_created_by_c = $parentBean->contacts_created_by;

                        }

                }


        }

}

?>

Any Ideas ?
Many thank to everyone in advance

Kind Regards

you need to set the id field of your relate filed with the id field of the relate in contact in this case would be ā€œcreated_byā€

ā€œcontacts_created_byā€ is the relationship name

1 Like

Thank you Matt for your prompt reply.

I apologize - I am new to coding with SuiteCRM and there are some things I donā€™t quite understand yet.
So please bear with me :blush:

I did some tests and there are a couple of things I would like to ask in the code above. They may enlighten other users as well.

1. First Question. Please see comment below


class logic_hooks_class {

        function before_save_method($bean, $event, $arguments) {

                if ($bean->load_relationship('contacts'))
                {

[b]//FOR SOME STRANGE REASON THE ABOVE IF IS FALSE AND THE SYSTEM NEVER REACHES THIS. ANY IDEAS WHY ???[/b]


2. Second Question. Please see comments below
Since I could not make the above work, I tried a different route:


class logic_hooks_class {

        function before_save_method($bean, $event, $arguments) {

$contactbean = BeanFactory::getBean('Contacts',$bean->contact_id);
$bean->contact_created_by_c = $contactbean->created_by;     [b]// THIS DOES NOT WORK[/b]
$bean->created_by = $contactbean->created_by;    [b] // THIS DOES WORK[/b]

        }
}
?>

What I fail to understand is the kind of values a custom relate field is expected to have. Even though it links a USER object like the created_by field does, it does not seem to accept the same values.

Additioanlly what TYPE of custom field should I create in order to keep the created_by values ?

Any help would be greatly appreciated. Any suggestions for further reading on this would also be greatly welcomed.

So many thanks for your time

Hey guys,

Im actually stuck at the same step. Would appreciate some help.
I have a before save logic hook and the code I have written is fairly simple, but doesnt work. No errors so no idea how to debug.

What I am trying to do:
I have a Relate field in my Contacts module which allows me to pick an acccount (facility_assigned_c). On save, I would like to look up the address of the account (Accounts module field: facility_address_c) and copy it over to the Contact (Contacts module: facility_address_c)

I created a before save logic hook in the contacts module.
The code is below:
class Prepaymentt2
{
function AdvancePayment2(&$focus, $event)
{
$facility = BeanFactory::getBean(ā€˜Accountsā€™, $focus->facility_assigned_c); //getBean does not return an account
//get Address from facility
if ($facility != null) //returns null
{
$focus->facility_address_c = $facility->facility_address_c; //This statement is never executed as $facility returns null
}
}
}

Hello thereā€¦

The way you are trying to achieve this is not correct. Also I did not really understood your setup.

First of all I donā€™t know if you are using a true RELATE field. If you doā€¦DONā€™T. I was not able to debug it but it does not seem to work like the relationships do.
Instead of using a relate field, create a true custom relationship (if you were using a relate field the relationship will be 1-1

Then Try the following:


class Prepaymentt2
{
function AdvancePayment2($bean, $event, $arguments) //All variables are passed by reference now so no need to do &$focus. Also use $bean
{
$accountbean = BeanFactory::getBean('Accounts',$bean->account_id); // This the account bean related to this one

//...then check what you want in its fields...like  $accountbean->billingaddress == "whatever"

To copy the relationship (WHY do you want to do that ? Isn't the two objects already related ???? try

$bean->load_relationship
and then
$bean->RELATIONSHIP_TABLE->add()

}
}

Hope I helpedā€¦