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:
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?
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.
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>';
}
}