In the end I decided to use bean (with logic_hook before_save_method) but I can’t get out.
In my custom mdule, I created from Studio the fields to populate which are:
description_c and price_c of AOS_products
categoria_c of AOS_produts_categories.
Now, how do I get the id of my relate field “codice_prodotto”?
And second question, how can I retrieve the name of its category?
I hope someone can help me.
I saw many examples around, I saw the guide but I can’t understand the usage
Then, I saw that when you have a relate field, in vardes there is both a “name” field containing the value seen by the user and an “id_c” field which is what I have to retrieve to find other values associated with the products.
So I think my logic_hooks_class.php can be something like that:
(Note: my relate field is with the standard aos_products module)
<?php
if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
class logic_hooks_class
{
function before_save_method($bean, $event, $arguments)
{
$idproductBeans = $this->bean->'aos_products_id_c'->getBeans();
......
If I have taken well the id of the product selected in the related field, how can I also take the description and copy it to the mydescription_c field of my custom module?
And the second question is: how can I also pull out the name of the category found in aos_produts_categorues? (and copy it to mycategory_c in my custom module)
Look online for examples of using “load_relationship”, that’s what you need to go get data from related records.
You need to know the exact internal name of your relationship. I am not sure how to find that out, but it should be somewhere in the files under “custom7modules/yourmodule”, probably in the vardefs.
I was suspecting it.
But I don’t really know where to find the name of the relationship with a relate field … I looked at the files and also on db but where does it write?
I believe that the way to call the relationship for the relate field (= name of the related module) is correct but but I’m not taking the values correctly and I’m not populating the field correctly…
<?php
if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
class logic_hooks_class
{
function before_save_method($bean, $event, $arguments)
{
if ($bean->load_relationship('aos_products')) {
$condizione -> $bean->load_relationship('aos_products');
$product = $condizione->aos_products->getBeans();
$product->name = $product ->descrizione_c;
$bean->save();
}
}
}
?>
First I would start by using a constant value just to test if the hook is running, and if it does its job:
$product->name = "hello world!";
Then I would be very careful about calling bean->save inside a before_save logic hook! Did you find examples online doing that? I am not sure, but I fear that It could generate an endless loop…
Finally - getbeans returns an array, you need to index the first item before getting the fields:
$condizione -> $bean->load_relationship('aos_products');
$product = reset($condizione->aos_products->getBeans()); // get only the first item
$product->name = $product->descrizione_c;
Or you could use a “for each” loop to check the various items, of course.
You want take data to the first module (???) from the second module (AOS_products).
You should have the keys of array ‘field_to_name_array’ , which are the names of fields of the second module and value of array ‘field_to_name_array’ which are the names of fields of the first module in file editviewsdefs.php . It will work only for fields of these 2 modules on Editview form.
If you want to use data from the third module, you should use logic hook ‘before_save’ (not after_save). Example:
function before_save_method($bean, $event, $arguments){
$second_module = new <name_of_second_module>();
$second_module->retrieve($bean->aos_products_id_c); // ‘aos_products_id_c’ fill in Editview form
$third_module = new <name_of_third_module>();
$third_module->retrieve($second_module-><name_of_id_field>);
$bean->codice_prodotto = $third_module->name;
}
I don’t know all structure of your modules and fields. Check all the names. If you write configuration modules and fields I can write the correct hook.
I read documentation too. Method ‘before_save’ should be used when you want to change data of current object. When you use ‘before_save’ you don’t call save() because save() will be called automatically. Method ‘after_save’ should be used when you want to work with data of other objects related to current object. When you use ‘after_save’ you should call save() obligatory and switch on control of endless cycle as you planned to do.
In your situation ‘before_save’ will be called only one time, but ‘after_save’ will be called two times. You should remember that you use ‘ignore_update_c’ as temporary variable and never use it as really field in this object.
As far as I remember, method ‘after_save’ was implemented earlier in SugarCRM then ‘before_save’.