Adding a team to a record through coding using after_save logic hook not working

Hi everyone,

I would like to add a team to a record through coding. I am using after_save logic hook but it is not add the team to the record. If I use before_save hook, then it’s working. I would like to use after_save hook as it is recommended by Sugar Documentation. I would appreciate if you can help. My Sugar version is Pro 7.9.20. Bellow is my coding:

sugarcrm\custom\Extension\modules\Accounts\Ext\LogicHooks\addTeam.ext.php

<?php

    $hook_array['after_save'][] = Array(
        //Processing index. For sorting the array.
        1,

        //Label. A string value to identify the hook.
        'Add Team value before_save',

        //The PHP file where your class is located.
        'custom/modules/Accounts/addTeam.php',

        //The class the method is in.
        'AddTeam_class',

        //The method to call.
        'add_team_method'
    );
?>

sugarcrm\custom\modules\Accounts\addTeam.php

<?php
    if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

    class AddTeam_class
    {
        function add_team_method($bean, $event, $arguments)
        {
            //loop prevention check
            if (!isset($bean->ignore_update_c) || $bean->ignore_update_c === false)
            {
                $bean->ignore_update_c = true;
                $module = $bean->module_dir;
                $record_id = $bean->id;

                $GLOBALS['log']->fatal(">>>> add teams executed >>>>>");

                //Retrieve the bean
                $bean = BeanFactory::getBean($module, $record_id);


                //Load the team relationship
                $bean->load_relationship('teams');

                $tl_team_id  = "a799aafc-d021-11e7-990a-00ff10f03434";
                $tcc_team_id = "b79db27c-d021-11e7-819e-00ff10f03434";
                $ft_team_id  = "d1f6ad5e-d021-11e7-a70e-00ff10f03434";

              
                //Add the teams
                $bean->teams->add(
                    array(
                        $ft_team_id,
                    )
                );
                $bean->save();
            }
        }
    }
?>

Sayfur Rahman, Francesca Shiekh, Ramana Raju SanthanaLori Arce

  • Hello,

    You don't need to releoad the bean and most of all you must not erase it like here where you use the same name as the variable name used as argument of your method.

    So remove the retrieve and the save from you code and keep only the team manipulation.

  • hi Cédric Mourizard, i did as you said, but still its not working. It is working when use before_save hook. but i want to do it in after_save hook.

    here is my code:

        if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

        class AddTeam_class
        {
            function add_team_method($bean, $event, $arguments)
            {
                 //loop prevention check
                 if (!isset($bean->ignore_update_c) || $bean->ignore_update_c === false)
                 {
                      $bean->ignore_update_c = true;
                      $module = $bean->module_dir;
                      $record_id = $bean->id;
                      $old_teams = array();

                      $GLOBALS['log']->fatal(">>>> add teams executed >>>>>");


                        //Load the team relationship
                        $bean->load_relationship('teams');

                        $tl_team_id  = "a799aafc-d021-11e7-990a-00ff10f03434";
                        $tcc_team_id = "b79db27c-d021-11e7-819e-00ff10f03434";
                        $ft_team_id  = "d1f6ad5e-d021-11e7-a70e-00ff10f03434";


                        //Add the teams
                        $bean->teams->add(
                            array(
                                $tcc_team_id,
                            )
                        );
                   }
            }
        }
    ?>
  • Hi Raymond Tham

    Try this code.

    if($bean->load_relationship('teams')){
          $bean->team_id = $primary_team_id;
          $bean->teams->replace(array(
              $primary_team_id,
              $another_team_id
            )
          );
    }

    Regards

    Mehul SB

  • hi Mehul  Bhandari,

    i tried the your code. it replaces the new team with old ones. for example, if i have team A and B in a record, and i use your code to add one more team (say it C), then it will remove team A & B and add team C. What i want is to keep the existing teams and add new team as well. bellow is my code:

    <?php

        if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

        class AddTeam_class
        {
            function add_team_method($bean, $event, $arguments)
            {
                 //loop prevention check
                 if (!isset($bean->ignore_update_c) || $bean->ignore_update_c === false)
                 {
                      $bean->ignore_update_c = true;
                      $module = $bean->module_dir;
                      $record_id = $bean->id;
                      

                      $GLOBALS['log']->fatal(">>>> add teams executed >>>>>");

                        //Retrieve the bean
                        //$bean = BeanFactory::getBean($module, $record_id);

                        //Load the team relationship
                        //$bean->load_relationship('teams');

                        $tl_team_id  = "a799aafc-d021-11e7-990a-00ff10f03434";
                        $tcc_team_id = "b79db27c-d021-11e7-819e-00ff10f03434";
                        $ft_team_id  = "d1f6ad5e-d021-11e7-a70e-00ff10f03434";
                        $primary_team_id = "b79db27c-d021-11e7-819e-00ff10f03434";
                        $another_team_id = $ft_team_id;
                        

                        

                        if($bean->load_relationship('teams')){
                              $bean->team_id = $primary_team_id;
                              $bean->teams->replace(array(
                                  $primary_team_id,
                                  $another_team_id
                                )
                              );
                        }

                        $bean->save();
                   }
            }
        }
    ?>
  • Here, use code below to achieve what you want. For some reason team link does not have add() method and you have to use replace(), which is kind of annoying  And just saying -> you don't need to retrieve or save the bean. 

    class AddTeam_class
    {
       //after save logic hook!!
        function add_team_method($bean, $event, $arguments){
            self::addTeams($bean,['d1f6ad5e-d021-11e7-a70e-00ff10f03434']);
        }
       /**
       * @param $bean \SugarBean
       * @param $teams array of ids of teams to be added to bean
       * @Util
       */
       public static function addTeams($bean,$teams){
           $teams = is_array($teams) ? $teams : [$teams];
           if(!sizeof($teams)) return;
           $teamSetBean = new \TeamSet();
           $oldTeams = $teamSetBean->getTeams($bean->team_set_id);
           foreach($oldTeams as $id=>$team){
               $teams[]=$id;
           }
           $bean->load_relationship('teams');
           $bean->teams->replace($teams);
       }
    }
  • hi Karel ÄŒech, I tried your code, it add a new team and keep the old ones as well. this is what i wanted, but there is one problem, if i want to remove one of the existing\old team through UI, then it wont be deleted. Plz see bellow video.

  • hi,

    I will take that in consideration, but i really wounder why add() method is not working. btw, thank you for your help.

  • Do you run it in After safe logic hook? It looks like there is wrong team_set_id on the bean...

  • yes, i run it in after_save logic hook.

    Here is my code:

    1. sugarcrm\custom\Extension\modules\Cases\Ext\LogicHooks

    <?php
        $hook_array['after_save'][] = Array(
            //Processing index. For sorting the array.
            1,

            //Label. A string value to identify the hook.
            'Add Team value before_save',

            //The PHP file where your class is located.
            'custom/modules/Accounts/addTeam.php',

            //The class the method is in.
            'AddTeam_class',

            //The method to call.
            'add_team_method'
        );

    ?>

    2. sugarcrm\custom\modules\Accounts

    <?php

        if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

        class AddTeam_class
        {
            function add_team_method($bean, $event, $arguments)
            {
                 //loop prevention check
                 if (!isset($bean->ignore_update_c) || $bean->ignore_update_c === false)
                 {
                      self::addTeams($bean,['d1f6ad5e-d021-11e7-a70e-00ff10f03434']);
              }

            }

            public static function addTeams($bean,$teams){
                $teams = is_array($teams) ? $teams : [$teams];
                if(!sizeof($teams)) return;
                $teamSetBean = new TeamSet();
                $oldTeams = $teamSetBean->getTeams($bean->team_set_id);
                foreach($oldTeams as $id=>$team){
                    $teams[]=$id;
                }
                $bean->load_relationship('teams');
                $bean->teams->replace($teams);
            }
        }
    ?>