How to get all cases linke to Opportunity?

I try to get all cases, linke to an opportunity (for example).

I need the information in a LogicHook because i want to link the same cases to another type (in this specific case to a document).

I´ve tried a lot, but always unable to fetch the linked Cases.

                $cases1 = $opportunity->get_linked_beans('opportunities_cases_1', 'aCase');

                $cases2 = $opportunity->get_linked_beans('opportunities_cases', 'aCase');

                if ($opportunity->load_relationship('cases')){
                   $caseBeans = $opportunity->cases->getBeans();
                }

In this example - $case1 and $case2 is "0" and $caseBeans never reached.

What´s the best or correct way to determine all attached/linke cases to a bean?

Appreciate any help. Thanks a lot.

Parents
  • Hi, 

    In your example you load the cases from the opportunities_cases relationship but I think, but don't know for sure, that the code on line 5 does the same thing. In any case: I think it is easiest to use the load_relationship methods to retrieve the cases from the 'opportunities_cases_1' and 'opportunities_cases' relationship. (I assume they are no relate fields) 

    I've created a little helper class that helps me with this: 

    <?php
    
    class BeanHelper
    {
        public static function getChildBeans(\SugarBean $bean, $relationship)
        {
            AcuityLog::debug(__CLASS__,sprintf('Start %s in %s at %s ',time(),__CLASS__, __FUNCTION__));
            $child_beans = null;
            if ($bean->load_relationship($relationship)) {
                $child_beans = $bean->{$relationship}->getBeans();
            }
    
            return $child_beans;
        }
    }
    
    

    with this in mind you can easily to the following: 

    <?php 
    
    $casesExtra = BeanHelper::getChildBeans($opportunity,'opportunities_cases_1');
    $cases  = BeanHelper::getChildBeans($opportunity,'opportunities_cases');
    
    // write code to merge the two arrays;
    

    The only thing you need to do is merge the two collections together ( based on case bean id?) and add them to another entity. 

Reply
  • Hi, 

    In your example you load the cases from the opportunities_cases relationship but I think, but don't know for sure, that the code on line 5 does the same thing. In any case: I think it is easiest to use the load_relationship methods to retrieve the cases from the 'opportunities_cases_1' and 'opportunities_cases' relationship. (I assume they are no relate fields) 

    I've created a little helper class that helps me with this: 

    <?php
    
    class BeanHelper
    {
        public static function getChildBeans(\SugarBean $bean, $relationship)
        {
            AcuityLog::debug(__CLASS__,sprintf('Start %s in %s at %s ',time(),__CLASS__, __FUNCTION__));
            $child_beans = null;
            if ($bean->load_relationship($relationship)) {
                $child_beans = $bean->{$relationship}->getBeans();
            }
    
            return $child_beans;
        }
    }
    
    

    with this in mind you can easily to the following: 

    <?php 
    
    $casesExtra = BeanHelper::getChildBeans($opportunity,'opportunities_cases_1');
    $cases  = BeanHelper::getChildBeans($opportunity,'opportunities_cases');
    
    // write code to merge the two arrays;
    

    The only thing you need to do is merge the two collections together ( based on case bean id?) and add them to another entity. 

Children
No Data