How to remove a team from users->Access->My teams ?

Hi All,

I am trying to remove the global team from user's profile by the following code in logic hook

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

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

//Remove the teams
$team_id_1 = 1
$bean->teams->remove(
array(
$team_id_1
)
);

But this only remove the user from the list of users in Team management screen.I also need to remove the team from the Users->Access->My teams tab.

How can remove a record from the Users->Access->My teams .

Parents
  • Hi Sino Thomas,

    To delete the global team membership, you need to write a logic hook directly in the Teams module and delete the team membership record.

    <?php
    if(!defined('sugarEntry')) die('not a valid entry point');

    class TeamsHookCls
    {
        public function after_save($bean, $event, $arguments)
        {
            //function to remove global team membership
             $this->removeGlobalTeam($bean, $event, $arguments);
        }

        /** remove global team from user and add private team as default team **/
        private function removeGlobalTeam($bean, $event, $arguments)
        {
            //fetch the related user bean
            $user_id = (isset($bean->user_id)) ? $bean->user_id : $bean->associated_user_id;
            $user_bean = BeanFactory::getBean("Users", $user_id);
            //fetch user's private team, if exists
            $privateTeamId = $user_bean->getPrivateTeamID();
            //if team membership exists with global team, delete it
            if($bean->module_name == 'TeamMemberships' && $bean->team_id == '1') {
                //delete the membership with global team
                $bean->delete();
            }
            //if private team exists, create new team set and assign it to user
            if(!empty($privateTeamId)) {
                $team_set_bean = BeanFactory::newBean("TeamSets");
                $team_set_id = $team_set_bean->addTeams(array($privateTeamId));
                //update default_team and team_set_id
                $field_defs = $GLOBALS['dictionary'][$user_bean->object_name]['fields'];
                $GLOBALS['db']->updateParams($user_bean->getTableName(), $field_defs, array('default_team' => $privateTeamId, 'team_set_id' => $team_set_id), array('id' => $user_bean->id));
            }
        }
    }

    The above logic hook class is written inside custom/modules/Teams and it removes the Global team membership from the user profile and add user's private team as a default team.

    Let us know if this helps.

    Regards.

    Hats

Reply
  • Hi Sino Thomas,

    To delete the global team membership, you need to write a logic hook directly in the Teams module and delete the team membership record.

    <?php
    if(!defined('sugarEntry')) die('not a valid entry point');

    class TeamsHookCls
    {
        public function after_save($bean, $event, $arguments)
        {
            //function to remove global team membership
             $this->removeGlobalTeam($bean, $event, $arguments);
        }

        /** remove global team from user and add private team as default team **/
        private function removeGlobalTeam($bean, $event, $arguments)
        {
            //fetch the related user bean
            $user_id = (isset($bean->user_id)) ? $bean->user_id : $bean->associated_user_id;
            $user_bean = BeanFactory::getBean("Users", $user_id);
            //fetch user's private team, if exists
            $privateTeamId = $user_bean->getPrivateTeamID();
            //if team membership exists with global team, delete it
            if($bean->module_name == 'TeamMemberships' && $bean->team_id == '1') {
                //delete the membership with global team
                $bean->delete();
            }
            //if private team exists, create new team set and assign it to user
            if(!empty($privateTeamId)) {
                $team_set_bean = BeanFactory::newBean("TeamSets");
                $team_set_id = $team_set_bean->addTeams(array($privateTeamId));
                //update default_team and team_set_id
                $field_defs = $GLOBALS['dictionary'][$user_bean->object_name]['fields'];
                $GLOBALS['db']->updateParams($user_bean->getTableName(), $field_defs, array('default_team' => $privateTeamId, 'team_set_id' => $team_set_id), array('id' => $user_bean->id));
            }
        }
    }

    The above logic hook class is written inside custom/modules/Teams and it removes the Global team membership from the user profile and add user's private team as a default team.

    Let us know if this helps.

    Regards.

    Hats

Children