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 

Assign manager to user

Hi folks,
      I have to assign user manger  in the orgnaisation...Like user1 manager is User2 and user2 manager is user3 and etc
     for that Ive created code but whether its correct or not
public class UserInsertTest
static testMethod void createTestUser()
List<User> users = new List<User>();
String pid = [Select id from Profile where Name='System Administrator'].Id; // here put your profile name
for(integer i=1; i<=100; i++)
    String orgId = UserInfo.getOrganizationId();
    String dateString = String.valueof(' ','').replace(':','').replace('-','');
    Integer randomInt = Integer.valueOf(math.rint(math.random()*1000000));
    String uniqueName = orgId + dateString + randomInt;
    User tuser = new User(  firstname = 'test',
                            lastName = 'user'+i,
                            email = uniqueName + '@test' + orgId + '.org',
                            Username = uniqueName + '@test' + orgId + '.org',
                            EmailEncodingKey = 'ISO-8859-1',
                            Alias = 'test',
                            TimeZoneSidKey = 'America/Los_Angeles',
                            LocaleSidKey = 'en_US',
                            LanguageLocaleKey = 'en_US',
                            ProfileId = pid);

insert users;


for(Integer i=0;i<users.size();i++){
                update users;


Please Help!
Best Answer chosen by SS Karthick
Hi Karthick,

You should change the following lines as follows:

list<user> userstoupdate = new list<user>();
for(Integer i=0;i<users.size()-1;i++){
update userstoupdate;

The last user will not have a manager user to be added.

If this answers your question, please mark this as the Best Answer for this post, so that others can benefit from this post.