Loading a relationship (one to many) with Bean Factory Possible?

So I’m in the project bean, and I can load an Opportunity relationship no problem with:

$projectBean->load_relationship('opportunities_project_1');

However when I’m in the opportunity bean, I have a one to many relationship with quotes. Where many Quotes can be related to one Opportunity. In this arrangement there is no opportunites_aos_quotes table to load and I can’t:

$QuoteBean->load_relationship('opportunities_aos_quotes');

I ended up doing a custom SQL query, to get the related quote. However, just asking is there a way to do this with the Bean Factory from the Opportunity bean?

I suggest looking in the DB, a table called fields_metadata (if I remember correctly) and seeing the internal relationship names there.

1 Like

Thanks @pgr Im pretty sure I got the name right. I dumped the bean and its there. I might try another approach today either just loading the quote record based on the opp id which I have or. get_linked_beans snd see if that gets me there. I found a bunch of posts on this subject but eveyone eventually just uses a sql query. If its possible I’d like to use the Bean Factory as best practice. I do have a sql query that works.

Sometimes especially in the default relationships
we have to do like

$QuoteBean->load_relationship('opportunities');
$QuoteBean->opportunities->getBeans();

or

$QuoteBean->load_relationship('opportunity');

one thing that I do when this is still an issue is I try

$QuoteBean->load_relationships();

this loads all the relationships and then I vardump or inspect the bean using debugger to find the relationship

3 Likes

Ok I solved it! For anyone that comes across this in the future:

If you have a one to many relationship for example quotes (Many) to one (Opportunity), so the quote side has a field and the opportunity has a subpanel. You can’t use load_relationship from the Opportunity Bean. There is no third table opportuntity_aos_quotes to pull from. It won’t work.

However, if you are in the opportunity bean, you have the opportunity ID. There is a field on the quotes side opportunity_id. So you can just load the bean directly with getBean.

Here’s my use case. I also have an additional condition that the quote is a “Primary” quote.

if ($oppBean instanceof Opportunity) {
    // Get the Opportunity ID
    $opportunityId = $oppBean->id;

    // Load the Quote bean based on the Opportunity ID and quote_pipeline_c = 'Primary'
    $quoteBean = BeanFactory::getBean('AOS_Quotes')->retrieve_by_string_fields(array(
        'opportunity_id' => $opportunityId,
        'quote_pipeline_c' => 'Primary'
    ));

    // Check if the Quote bean was found
    if ($quoteBean instanceof AOS_Quotes) {
        // Get the quote name and ID
        $quoteName = $quoteBean->name;
        $quoteId = $quoteBean->id;

        // Set the link value in the bean
        $bean->quote_link_c = '<a href="index.php?module=AOS_Quotes&action=DetailView&record=' . $quoteId . '">' . $quoteName . '</a>';
    }
}

1 Like