function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SS KarthickSS Karthick 

Get manager id based on user id

Hi folks,
          Can anyone tell me the soql query getting manager id ??/
like user-->manager-->manager's manager and user1-->manager1-->manager of manager1

I wanna all managers id based on user 

Thanks in advance
Karthick


Best Answer chosen by SS Karthick
Vikash TiwaryVikash Tiwary
If you need to get all ManagerId related to that User you need to perform SOQL inside loop.

String managerId = UserId;

while(managerId != null)
{
   string managerId=  [Select Id, ManagerId FROM User WHERE Id = :managerId].ManagerId;
   if(managerId != null)
       setManagerId.add(managerId);
 }

Hope this helps!

Thanks

All Answers

Jean-NoelJean-Noel
Hello,

Something like that, will give you the user manager ID and the managed users.

Select Id, Name, ManagerId, (Select Id, Name From ManagedUsers) From User
Vikash TiwaryVikash Tiwary
User usr = [Select Id, Manager.ManagerId, ManagerId FROM User WHERE Id = :UserId];

Thanks
Vikash TiwaryVikash Tiwary
If you need to get all ManagerId related to that User you need to perform SOQL inside loop.

String managerId = UserId;

while(managerId != null)
{
   string managerId=  [Select Id, ManagerId FROM User WHERE Id = :managerId].ManagerId;
   if(managerId != null)
       setManagerId.add(managerId);
 }

Hope this helps!

Thanks
This was selected as the best answer
praveen murugesanpraveen murugesan
Hi Karthick,

If I understood you question correctly,

can you try this,

SELECT Name, ManagerID, Manager.Name FROM User where id-:'id which is getting'

Mark this as best answer if its helps.

Thanks.

Praveen Murugesan
krishna chaitanya 35krishna chaitanya 35
@Vikash,
can you explain the scenario a brief like how to embed this code in a class.i am getting error like duplicate managerid and invalid managerid...
If you need to get all ManagerId related to that User you need to perform SOQL inside loop.

String managerId = UserId;

while(managerId != null)
{
   string managerId=  [Select Id, ManagerId FROM User WHERE Id = :managerId].ManagerId;
   if(managerId != null)
       setManagerId.add(managerId);
 }

Hope this helps!

Thanks