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:
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 The name of the custom field is Field Name: contact_created_by_c
Then created the two files for the Logic Hook:
custom/modules/Notes/Logic_Hooks.php:
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
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.
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
}
}
}
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()
}
}