How to get logged-in users all teams via query.

Hello,

I am using sugar cloud,

I want to get current user's all teams using query.

Those all teams comes under user's->access tab-> My Teams.

Please assist me for the same.

Best Regards

Shiv

Parents Reply Children
  • Hi Jeroen Somhorst,

    When i am using below code in path:custom\clients\base\api\myappi.php

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

    <?php


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

    class UpcomingBirthdayApi extends SugarApi
    {
    public function registerApiRest()
    {
    return array(

    'GetHours' => array(
    //request type
    'reqType' => 'GET',
    //set authentication
    'noLoginRequired' => true,
    //endpoint path
    'path' => array('Contacts','getBirthday'),
    //endpoint variables
    'pathVars' => array('',''),
    //method to call
    'method' => 'getbdyData',
    //short help string to be displayed in the help documentation
    'shortHelp' => 'Get Upcoming and last 7 days birthday',
    //long help to be displayed in the help documentation
    'longHelp' => '',
    ),
    );
    }

    public function getbdyData($api,$args){
    global $db,$current_user,$sugar_config;
    $userBean = BeanFactory::getBean('Users', $current_user->id);

    //Load relationship of teams
    $userBean->load_relationship('teams');

    //$relatedTeams conatins an array of beans of all the teams of the user
    $relatedTeams = $userBean->teams->getBeans();
    }
    }

    ?>

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

    In this case, i am able to get admin user's team if admin logins(only global team) but not for other user's teams any data. i am printing $relatedTeams in log also.

    Please have a look of my above stuff.

    Thanks

  • When you login with another user it should give the data of that specified user. If you want to have the data for all users you need to adapt the code in such a way that it retrieves all the users from the database

    $q = new SugarQuery();
    
    $q->from(BeanFactory::newBean('Users'));
    $q->select(['id']);
    $result = $q->execute();
    
    foreach($result as $row){
     $userBean = BeanFactory::getBean('Users',$row['id']);
    
    if(!empty($userbean)){
     // export the teams etc.
    }
    }

    this is just an example. You need to take into account the fact that you are not able to retrieve all beans when you are a normal user ( you need to set the security to falls on the getbean method)

    But still th question is why do you need to have the Teams information for ALL users ?

  • Hi ,

    Thank you for your valuable time.

    I want to get current user's all team(non admin user).

    When i login using another user(non admin),In this case i am getting below outcome in log file:

    Wed Jul 14 10:31:19 2021 [22388][593cf944-e3e2-11eb-8e10-025041000001][FATAL] relatedTeams : Array
    (
    )

    And when login using admin ,In this case i do get global team of admin, while  admin had multiple team.

    I build a dashlet, where i want team wise visibility of record in dashlet. In the SQL query i want to add teams of logged-in user,so that only those team member can have access.

    Please assist me for the same, I am unable to get all team of any user.

    Thank You