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

  • What do you mean by query? SQL query? 

    There is no direct DB access to Sugar Cloud instances according to Sugar Cloud Policy.

    Do you consider other options? 

    Best Regards,
    Dmytro Chupylka

    integroscrm.com
    We make work in Sugar CRM system faster, more convenient and efficient

  • Thank you for your valuable response Dmytro Chupylka!

    Yes! I requesting for SQL query.

    I want to get all team of current user in below path:

    custom\clients\base\api\myappi.php

    Thank You

  • Hi Shivashankar,

    You can load the user bean and get all the related teams of user. Refer this link for more information on how to fetch related records.

    //Load the user bean 
    //replace {id} with actual user id
    $userBean = BeanFactory::getBean('Users', {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();

    Thank you,
    Vamshi S

    Bhea Technologies Pte Ltd

    https://bhea.com

  • Thank you your reply Vamshi !

    But my concern not resolved yet using above logic. I am able to get only admin's global team. Neither getting all  teams nor for other user's any team.

    Any other help with this regards will be appreciable.

    I want to get all teams in below path

    custom\clients\base\api\myappi.php

    Thank you 

  • Hi Dmytro Chupylka,

    Is there a way to get all teams of current user(logged-in user). How i can achieve this.

    Thank You

  • Hi Vamshi ,

    Is there a way to get all teams of current user(logged-in user). How i can achieve this.

    Thank You

  • Where do you want to have this information? The code specified by  does exactly that. You need to add it to a logic hook, an api or something else that suits your needs. Can you explain to use the use case you are trying to solve?

    If you create an api for that you need to execute that api using the javascript frontend or using postman (or any other similar tool). In any case you need to be authenticated before you can execute the info. 

  • 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