Getting the list of Teams Assigned to the Current User

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Triggerz
    Member
    • May 2024
    • 68

    Getting the list of Teams Assigned to the Current User

    Hi,

    I am trying to get the list of Teams assigned to the current user. I tried the code below in the Formula Sandbox but I am getting an Error 500.
    I need this to check if a user is assigned to a specific team and use this as a condition to make fields read-only. Please help.

    output\printLine(env\userAttribute('teams'));


    Thanks
  • victor
    Active Community Member
    • Aug 2022
    • 799

    #2
    Code:
    $userTeamsNames = env\userAttribute('teamsNames');
    output\printLine($userTeamsNames);
    Click image for larger version

Name:	image.png
Views:	18
Size:	44.6 KB
ID:	115414

    Comment

    • Triggerz
      Member
      • May 2024
      • 68

      #3
      Hi victor,

      Thank you so much, the code you provided works perfectly.

      I also need to get the role names, I tried executing the code below but I am getting a {} output. Not sure if I have the wrong parameter, please help as well on this please.

      Code:
      $userRolesNames = env\userAttribute('rolesNames');
      output\printLine($userRolesNames);
      Thanks

      Comment

      • victor
        Active Community Member
        • Aug 2022
        • 799

        #4
        Click image for larger version

Name:	image.png
Views:	10
Size:	49.3 KB
ID:	115427
        Code:
        $i = 0;
        
        while ($i < array\length(rolesIds)) {
        
        $roleId = array\at(rolesIds, $i);
        $roleName = record\attribute('Role', $roleId, 'name');
        
        $rolesNames = array\push($rolesNames, $roleName);
        
        $i = $i + 1;
        }
        
        output\printLine($rolesNames);​
        Please note that now you must select the User whose role you want to receive.

        You can modify this formula a bit and apply it to teams:

        Click image for larger version

Name:	image.png
Views:	9
Size:	44.8 KB
ID:	115428
        Code:
        $i = 0;
        
        while ($i < array\length(teamsIds)) {
        
        $teamId = array\at(teamsIds, $i);
        $teamName = record\attribute('Team', $teamId, 'name');
        
        $teamsNames = array\push($teamsNames, $teamName);
        
        $i = $i + 1;
        }
        
        output\printLine($teamsNames);​

        Comment

        • Triggerz
          Member
          • May 2024
          • 68

          #5
          Hi Victor,

          I tried to use the code below in the API Before Save Script section of the Entity. The value of the $bRoleExist is always false even if the current user has the role 'Finance Admin' assigned. Please help.

          Code:
          $i = 0;
          while ($i < array\length(rolesIds)) {
          $roleId = array\at(rolesIds, $i);
          $roleName = record\attribute('Role', $roleId, 'name');
          $rolesNames = array\push($rolesNames, $roleName);
          $i = $i + 1;
          }
          
          $bRoleExist = array\includes($rolesNames, 'Finance Admin');
          
          $recstat = record\attribute('CCashar', casharId, 'status');
          
          if(($recstat == 'Approved' || $recstat == 'Approved By Finance' || $recstat == 'Released' || $recstat == 'Liquidated') && $bRoleExist == false) {
          recordService\throwForbidden(string\concatenate('Parent status is set to ', $recstat, '. Adding or Editing of record is not allowed.'));
          }
          Thanks

          Comment

          • lazovic
            Super Moderator
            • Jan 2022
            • 889

            #6
            Hi Triggerz,

            Please tell me in which entity exactly you use this formula script?

            If you use it in the Account entity and want to access the roles of the Assigned User, you should change the formula script a little:
            Code:
            $rolesIds = record\attribute('User', assignedUserId, 'rolesIds');
            
            $i = 0;
            
            while ($i < array\length($rolesIds)) {
                
                $roleId = array\at($rolesIds, $i);
                $roleName = record\attribute('Role', $roleId, 'name');
                $rolesNames = array\push($rolesNames, $roleName);
                
                $i = $i + 1;
            }
            
            $bRoleExist = array\includes($rolesNames, 'Finance Admin');
            
            $recstat = record\attribute('CCashar', casharId, 'status');
            
            if ( ($recstat == 'Approved' || $recstat == 'Approved By Finance' || $recstat == 'Released' || $recstat == 'Liquidated') && $bRoleExist == false) {
                recordService\throwForbidden(string\concatenate('Parent status is set to ', $recstat, '. Adding or Editing of record is not allowed.'));
            }
            Last edited by lazovic; Today, 08:58 AM.

            Comment

            • Triggerz
              Member
              • May 2024
              • 68

              #7
              Hi lazovic,

              I want to get the roles assigned to the current user that is logged in. I tried using env\userAttribute('rolesNames') but it doesn't return the roles that is assigned to the current user.

              Thanks

              Comment

              • lazovic
                Super Moderator
                • Jan 2022
                • 889

                #8
                Triggerz,

                Please use the following formula script:
                Code:
                $userId = env\userAttribute('id');
                
                $rolesIds = record\attribute('User', $userId, 'rolesIds');
                
                $i = 0;
                
                while ($i < array\length($rolesIds)) {
                    
                    $roleId = array\at($rolesIds, $i);
                    $roleName = record\attribute('Role', $roleId, 'name');
                    $rolesNames = array\push($rolesNames, $roleName);
                    
                    $i = $i + 1;
                }
                
                $bRoleExist = array\includes($rolesNames, 'Finance Admin');
                
                $recstat = record\attribute('CCashar', casharId, 'status');
                
                if ( ($recstat == 'Approved' || $recstat == 'Approved By Finance' || $recstat == 'Released' || $recstat == 'Liquidated') && $bRoleExist == false) {
                    recordService\throwForbidden(string\concatenate('Parent status is set to ', $recstat, '. Adding or Editing of record is not allowed.'));
                }

                Comment

                Working...