I have a recent post that I will keep up where I had this same problem but there were more elements involved and I didnāt know how to specify this issue. But Iām seeing if there is another approach to it since I was unable/didnāt know exactly what I was looking for.
I have two modules, one being Accounts and the other a custom module. Both are linked by an one-many relationship (Customers->Moves) but the biggest thing Iām stuck with is finding a way to retrieve the Record ID from the custom module using beans. Iām not worrying about what comes after the bean is retrieved in this post. I just donāt know how to find the Record ID of an account after itās selected by a relate field.
This is the flow Iām interested in:
Moves Module -> select account record (already have this covered) -> get the bean ID of the Account Record selected -> retrieve the bean fields
This has been a massive headache to me and I feel like itās probably a lot easier than Iām making it out to be
Hi,
are you talking about e.g. hooks? So you save a record and want to get the ids of related objects in your php method?
E:
$accountBean = BeanFactory::getBean('Accounts', $id); //get an account bean by id
$accountBean->load_relationship("contacts"); //initialize relationship.
$contacts = $accountBean->contacts->getBeans(); //get all contacts related to the current account bean
foreach($contacts as $contact){
//do something with a contact.
$someId = $contact->id;
}
it is sometimes tricky to figure out the correct relationship name (in this example, it is ācontactsā. usually, it is something less readable) -> the studio should display these releationship names too.
I have a relate field where I can select a Customer, but I donāt know how to find the $bean->id of that selected bean. And yes Iām assuming I will be using logic hooks, Iāll modify the code and try it out and let you know
Iām getting an error on line 3 of your snippet, on the getBeans() call. This is my modified version up to the error, I isolated the code to rule out the foreach statement that Iām leaving out for now. I donāt see any error messages in my logs it just white screens me:
Iām running this off my ubuntu server, sshād in to make changes. Iām going to look around the log some more to see what happens when loading relationships
PHP Fatal error: Uncaught Error: Call to a member function load_relationship() on bool in /var/www/html/crm-alpha/custom/modules/Moves_Moves/FieldMap.php:7
this is from $accountBean->load_relationship(āmoves_moves_accountsā);
Ah then you need to step back one further line to get to your problem - it looks like BeanFactory::getBeans('Accounts', $id); isnāt returning anythingā¦
Yeah I was looking into that, would $id have a value in this case? Because I donāt have the id of the Accounts record selected, in my code I just initialized the variable.
This is a logic hook running from a module (Moves_Moves) which is related to Accounts, in the edit view layout there is a relate field that Iāve made where you can select an Account, but my intention is to then access the record of the selected account and pull the beanās fields (from the Moves_Moves module).
So it sounds like you just need to traverse the relationship in the opposite direction. You start from the Moves bean (hook parameter), load_relationship to get to the accounts (you might have to loop if is many-to-many), then you can write stuff in the Account bean and save.
Itās One to Many (One Account to Many Moves), how would I be able to call load_relationship without having an Account bean initiated, which needs an $id from what I see in the previous code?
// $bean is a Moves bean that comes as a parameter to the logic hook
$bean->load_relationship('moves_moves_accounts');
$account = $bean->moves_moves_accounts->getBeans();
now you have both your beans, write to whichever you need, and save at the end.
Avoid saving bean in loops (an after save hook that saves the same bean)