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.

  • 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. 

  • get_linked_beans is how we used to do it years ago in version 6, but that method has been obsolete for quite some time.

    The way to get related beans is what  described, just make sure that your link name ($relationship, in Jeroen's code) is correct. Although I'm not familiar with the use of AcuityLog and I suspect it's an add-on to his particular setup.

    What version of Sugar are you working with?

    FrancescaS

  • AcuityLog is a internal log class we use. I removed it from the code example. 

  • It´s - now - Sugar 10 we have in use. With quite old custom code.

    Jeroens Code helped me a lot and it work´s now like a charm.

    Currently i only struggle again to add to documents a new relationship to existing cases. you haven´t a helper too? ;-)

  • To relate the child record to the bean:

    $relationship = 'my_relationship' //the relationship link name as above;
    $child_record_id = '123' //the id of your child record;
    //$bean is the parent bean 
    
    if($bean->load_relationship($relationship) && !empty $child_record_id)){
      $bean->$relationship->add($child_record_id);
    }
    

    You may want additional check to make sure your child's record ID exists etc...