AnsweredAssumed Answered

Adding and removing team in logic hooks

Question asked by Caim Astraea on Jun 2, 2016
Latest reply on Jun 3, 2016 by Caim Astraea

Having a bit of an issue getting $bean->teams->remove to work in tandem with add.

What I've noticed none of them  work in an after_save hook despite the recommendation here http://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_6.5/Application_Framework/Teams/Manipulating_Teams_Programatically/ so they have to be in a before_save hook.

All this on 7.7

 

What I'm trying to do is :

If account type is A , set the main team as the user's default team , set another team called NTA .

If account type is G , set the main team as the user's default team, set another team called NTG.

 

If switching between A and G account types I want to remove NTA and NTG respectively , So if an account has type A and teams : <userteam1> , <NTA> and I want to change to type G it should have have teams : <userteam1>, <NTG>.

 

But can't get the bean->teams->remove to work for removing the teams when switching account type. Only bean->teams->replace works but don't really think it fits the use case since an account would have another team that would also get removed(replaced)

 

What do you suggest is wrong ? I feel like I'm missing something or don't understand how this works at a fundamental level.

 

 

--------------------

my hook:

$hook_version = 1;

$hook_array = Array();

$hook_array['before_save'][] = Array(1, 'setPrimaryTeam', 'custom/modules/Accounts/setTeams.php','setTeams', 'setPrimaryTeam');

$hook_array['before_save'][] = Array(2,'setTeamSet', 'custom/modules/Accounts/setTeams.php','setTeams', 'setTeamSet');

 

----------------------------

hook class:

<?php

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

class setTeams
{
    function setPrimaryTeam($bean,$event,$arguments)
    {

        $GLOBALS['log']->fatal(">>>> Setting Teams on creation >>>>>");

        global $current_user;
        $module = $bean->module_dir; // should be accounts
        $record_id = $bean->id;
        $userTeam = $current_user->default_team; //curent user's default team
        $typeClient = $bean->account_type; // P rospect or C lient ?
        $dp_team = new Team();
        $dp_team_idNTA = array($dp_team->retrieve_team_id('NTA'));
        $dp_teamidNTG = array($dp_team->retrieve_team_id('NTG'));

        if ($typeClient == "A") //Agence -> set team to NTA
        {
            $dp_team_id =  $dp_team->retrieve_team_id('NTA');

            $bean->team_id = $dp_team_id;

            $bean->team_id = $userTeam;

            $teamname = $dp_team->getTeamName($bean->team_id);

            $GLOBALS['log']->fatal(">>>>  TEAM NAME {$teamname}");

            $bean->load_relationship('teams');

           $bean->teams->add($dp_team_idNTA); // add NTA team to the team list

        }
        else //it's G group set to NTG ?
        {
            $dp_team_id =  $dp_team->retrieve_team_id('NTG');

         $bean->team_id = $dp_team_id;

           $bean->team_id = $userTeam;

            $teamname = $dp_team->getTeamName($bean->team_id);

            $GLOBALS['log']->fatal(">>>>  TEAM NAME {$teamname}");

            $bean->load_relationship('teams');
            $bean->teams->add($dp_teamidNTG);

          //  $bean->teams->replace($dp_teamidNTG); // add NTG to the team list

        }

    }

    function setTeamSet($bean,$event,$arguments)
    {
        $GLOBALS['log']->fatal(">>>> runnign before save");
        global $current_user;
        $module = $bean->module_dir; // should be accounts
        $record_id = $bean->id;
        $typeClient = $bean->account_type; // P rospect or C lient ?
        $dp_team = new Team();
        $dp_team_idNTA = array($dp_team->retrieve_team_id('NTA'));
        $dp_teamidNTG = array($dp_team->retrieve_team_id('NTG'));

        if ($typeClient == "A") //Agence -> set team to NTA
        {

            //if it's updated from NTG to NTA , remove the NTG team
            $bean->load_relationship('teams');
            try {
                $bean->teams->remove($dp_teamidNTG);//remove NTG if it was changed from Group to Agence
                throw new Exception();
            } catch (Exception $e) {

                ob_start();
                var_dump($e->getMessage());
                $contents = ob_get_contents();
                ob_end_clean();
                error_log($contents);

            }
        }
        else //it's G group set to NTG ?
        {
            $bean->load_relationship('teams');
             $bean->teams->remove($dp_team_idNTA); // remove NTA if it was changed from Agence to Group
        }

    }
}

 

 

 

 

 

 

 

 

 

$hook_version = 1;
$hook_array = Array();
$hook_array['before_save'][] = Array(1, 'setPrimaryTeam', 'custom/modules/Accounts/setTeams.php','setTeams', 'setPrimaryTeam');
$hook_array['before_save'][] = Array(2,'setTeamSet', 'custom/modules/Accounts/setTeams.php','setTeams', 'setTeamSet');

Outcomes