Taking a bean->id from one module, and retrieving it from another

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

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?


$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:

$accountBean = BeanFactory::getBeans('Accounts', $id); $accountBean->load_relationship('moves_moves_accounts'); $moves = $accountBean->moves_moves_accounts->getBeans();

This is my vardefs, it looks to me that moves_moves_accounts is the correct relationship to load but I may be wrong

Are you sure you’re checking both your logs?

PHP errors show on the PHP log, that’s defined in error_log entry in your php.ini

PHP Fatal error: Uncaught Error: Call to a member function getBeans() on null in /var/www/html/crm-alpha/custom/modules/Moves_Moves/FieldMap.php:8

Found it, i thought it was strange using


Should I be using a module name instead? I don’t think moves_moves_accounts is initialized

The class member would be there if your load_relationship had worked, but I guess it didn’t.

Are you using XDEBUG and a debugger? It makes sorting these things out so much easier…

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.

$id is a required input of the function. What the function does is precisely to get a module name and an id and return the corresponding bean.

This is the big issue I have, I don’t know how to find the record ID

What is your context, where is this code running?

If it’s a logic hook in the Accounts module, you get the bean already prepared for you as a parameter to the hook

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?

Something like

// $bean is a Moves bean that comes as a parameter to the logic hook
$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)

thank you much! I’m sure you’ll be hearing more from me :smiley: appreciate it man