4

I have to insert (User) through test class for that I have inserted (Profile) first , then after inserted (User) and passes that profile to profileId. It is Getting Saved but when i run the test class it's through an error that 'Manager is Required'(FIELD_CUSTOM_VALIDATION_EXCEPTION, Manager is Required). How to solve this error. Please Help me.

Profile pro = [SELECT Id FROM Profile WHERE Name='Standard User']; 
    User u = new User(Alias = 'standtrd', Email='[email protected]',  EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', LocaleSidKey='en_US', ProfileId = pro.Id,  
                    TimeZoneSidKey='America/Los_Angeles', UserName='[email protected]'); 
    insert u;

4 Answers 4

1

Your org has a validation rule that requires a value for a Manager field (ManagerID) on User. Ideally you would create a second test User to assign as your first test User's manager. You get into a chicken and egg scenario though, because you can't create and insert a test Manager without providing their manager.

Solution 1:

Create two test Users, assign them as each other's managers, and insert them at the same time (lol):

Profile pro = [SELECT Id FROM Profile WHERE Name='Standard User']; 
User testUserA = new User(
    Alias = 'standard', Email='[email protected]',  
    EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
    LocaleSidKey='en_US', ProfileId = pro.Id, 
    TimeZoneSidKey='America/Los_Angeles', UserName='[email protected]'); 

User testUserB = new User(
    Alias = 'standard', Email='[email protected]',  
    EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
    LocaleSidKey='en_US', ProfileId = pro.Id,  
    TimeZoneSidKey='America/Los_Angeles', UserName='[email protected]'); 

testUserA.ManagerID = testUserB.id;
testUserB.ManagerID = testUserA.id;

List<User> usersToInsert = new List<User>();
usersToInsert.add(testUserA);
usersToInsert.add(testUserB);
insert usersToInsert;
// rest of test here

Solution 2:

Use @isTest(seeAllData=true) and query for an actual User ID to use as a dummy Manager for your test Users:

User dummyManager = [SELECT Id, Name FROM User WHERE Name = 'RAHUL SRIVASTAVA' LIMIT 1];
User u = new User( 
   ManagerID = dummyManager.id, // provide a value for Manager here
   Alias = 'standtrd', Email='[email protected]',
   EmailEncodingKey='UTF-8', LastName='Testing', 
   LanguageLocaleKey='en_US', LocaleSidKey='en_US', 
   ProfileId = pro.Id, TimeZoneSidKey='America/Los_Angeles', 
   UserName='[email protected]'); 

insert u;

// rest of test here

While there is an open proposal for a system option to disable validation rules during test scenarios, it would kind of eliminate the point of testing in the first place, so I wouldn't hold my breath and wait on it for a solution :P

There is almost certainly a better solution than this, I will be interested to hear what more experienced folks recommend in this situation :)

3
  • Thanks for your reply, I tried it but it's not working .
    – SFDC 44
    Jul 15, 2015 at 14:22
  • Manager is not a look up user it is Hirarchy data type in manager field.
    – SFDC 44
    Jul 15, 2015 at 14:29
  • "Hierarchy" is a user-to-user lookup... Nov 16, 2015 at 22:53
0

I think your User object has a validation rule that will block saving the User record without filling up the Manager lookup field (API name: ManagerID). You can re-write the validation rule with additional criteria to allow saving User record even without Manager field filled in for certain User records.

0

Query the User and then add the same id as the ManagerId when you are inserting the new User.

User u1 = [SELECT Id FROM User WHERE Name='Test User'];
Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator'];
User u = new User(
                firstname='test',lastname='record',username = '[email protected],
                email = '[email protected]',emailencodingkey = 'UTF-8',
                localesidkey = 'en_US, languagelocalekey = 'en_US',
                timezonesidkey = 'America/Los_Angeles', managerId= u1.id,alias='cspu',
                ProfileId = p.Id)
0

You can add your current user (normally System Administrator) as Manager. You don't need to use seeAllData=true to see User records as they're considered data to manage your organization. See document here

So your code should look like this:

User u = new User(Alias = 'standtrd', 
Email='[email protected]',  
EmailEncodingKey='UTF-8', 
LastName='Testing', 
LanguageLocaleKey='en_US', 
LocaleSidKey='en_US', 
ProfileId = pro.Id, 
TimeZoneSidKey='America/Los_Angeles', 
UserName='[email protected]', 
ManagerId = UserInfo.getUserId()); 

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .