0

Here is the test class to create a user and Managers up 3 levels.

   Profile pro = [SELECT Id FROM Profile WHERE Name='LYB Sales']; 
    testDirector =  [select LastName, ProfileId, Email, Alias, TimeZoneSidKey, LocaleSidKey, EmailEncodingKey, LanguageLocaleKey, SAP_User_Id__c  from User  
                    where IsActive = True Limit 1];
              testDirector.ProfileId = pro.Id;
            testDirector.SAP_User_Id__c = 'TDirector';
             update testDirector;  

      testExecutive =  [select LastName, ProfileId, Email, Alias, TimeZoneSidKey, LocaleSidKey, EmailEncodingKey, LanguageLocaleKey, SAP_User_Id__c  from User  
                    where IsActive = True Limit 1];
              testExecutive.ProfileId = pro.Id;
            testExecutive.SAP_User_Id__c = 'TExecutive';
             testExecutive.ManagerId = testDirector.Id;
             update testExecutive;  

        testManager =  [select LastName, ProfileId, Email, Alias, TimeZoneSidKey, LocaleSidKey, EmailEncodingKey, LanguageLocaleKey, SAP_User_Id__c  from User  
                    where IsActive = True Limit 1 ];
             testManager.ProfileId = pro.Id;
            testManager.SAP_User_Id__c = 'TManager';
            testManager.ManagerId = testExecutive.Id;
             update testManager;          

    /* create user */
         testUser = [select LastName, ProfileId, Email, Alias, TimeZoneSidKey, LocaleSidKey, EmailEncodingKey, LanguageLocaleKey, SAP_User_Id__c  from User  
                where IsActive = True 
                   limit 1];
             testUser.ProfileId = pro.Id;
             testUser.SAP_User_Id__c = 'Test User';
            testUser.ManagerId = testManager.Id;
             update testUser;

Error:System.DmlException: Update failed. First exception on row 0 with id 005o00000019BW2AAM; first error: INVALID_ID_FIELD, A user can't be his or her own manager.: [ManagerId]

  • Thanks for sharing your code and error, can you elaborate on what it is that you need help with or that you don't understand? – glls Oct 21 '17 at 15:13
1

You're querying the same user each time (queries are deterministic, meaning you'll query the same record each time). Instead, consider creating new users (they won't persist after the test ends), or query a list of 4 different users, then assign each to their own variable.

| improve this answer | |
  • That helped. Created new users instead of querying a list. – Rohit Oct 22 '17 at 3:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.