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)
donāt use apostrophes surrounding the field name.
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ā.