+ Start a Discussion
divya1234divya1234 

Test Classes are getting failed due to validation rule on user object

I wrote the validation rule on user object for picklist field value should be A, B,C. now my test classes are getting failed.can i just add one line user.picklist__c = 'A", before user record get an insert or i need to add a line for each picklist value.i.e user.picklist__c = 'A"user.picklist__c = 'B"user.picklist__c = 'C"
 
v varaprasadv varaprasad
Hi Divya,

Add user.picklist__c = 'A" on user record and run testclass
 
Profile p = [SELECT Id FROM Profile WHERE Name='Standard User']; 
        User u = new User(Alias = 'standt', Email='standarduser@testorg.com', 
            EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
            LocaleSidKey='en_US', ProfileId = p.Id,picklist__c = 'A',  
            TimeZoneSidKey='America/Los_Angeles', UserName='standarduser@testorg.com');
			
			Insert u;

        System.runAs(u) {
		  //Add your code here
		
		}
Hope it helps you.
Please let me know in case of any other help.


Thanks
Varaprasad
 
Rahul KumarRahul Kumar (Salesforce Developers) 
Hi Divya Bhagat,

May I suggest you please refer the below link to reference. Hope it helps.

Please mark it as best answer if the information is informative.so that question is removed from an unanswered question and appear as a proper solution.

Thanks
Rahul Kumar
 
divya1234divya1234
Thanks fo the replay there are almos 30 classes which is causing issue. so i shall add the same code in all classes or i should add in one utility calss. i am new to apex it will be awesome if you can explain me in kore detail
v varaprasadv varaprasad
Hi Divya,

Create one helper class and then call it wherever you need like below : 
 
public class userTest{
   
   public static user cretUser(){
     Profile p = [SELECT Id FROM Profile WHERE Name='Standard User']; 
        User usr = new User(Alias = 'standt', Email='standarduser@testorg.com', 
            EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
            LocaleSidKey='en_US', ProfileId = p.Id,<b>picklist__c = 'A'</b>,  
            TimeZoneSidKey='America/Los_Angeles', UserName='standarduser@testorg.com');			
			Insert u;
   
      return usr;
   
   } 

}

In your test class use below code :

user us = userTest.cretUser();
System.runAs(u) {
   //Add your Test code here

}

Hope it helps you.

Thanks
Varaprasad
 
divya1234divya1234
 i am getting an error saying variable u does not exist, when i am trying to call helper class in my class
public static testMethod void testAccountPhoneFax(){
        TestHelper.createCustomSettings();
        setupData();
       TestClassHelper.CreateUser();
        System.runAs(u)
        {
            System.debug('Current User: ' + UserInfo.getUserName());
            System.debug('Current Profile: ' + UserInfo.getProfileId());
        }
v varaprasadv varaprasad
Hi Divya,

Use below line instead of  TestClassHelper.CreateUser();

user u = TestClassHelper.CreateUser();

Hope it helps you.

Thanks
varaprasad
divya1234divya1234
i have one more method public User CreateUser(string userType) . how can i call this method

user u = TestClassHelper(---) ,what should be argument ..this is my helper class do i need 2  methods

@isTest
Public class TestClassHelper
{    
     public static User CreateUser()
    {
        //Get a profile id from the data base to assign to our user in this case make them a sys admin
        Profile p = [select id from profile where name='System Administrator'];
           
           //Create a new sys admin user and do an insert  
        User u = new User(alias = 'standt', email='noEmail@testorg.com',
                          emailencodingkey='UTF-8', lastname='Testo', languagelocalekey='en_US', localesidkey='en_US', profileid = p.Id,
                          timezonesidkey='America/Los_Angeles', username='MrTestUser@testorg.com',Resource_Type__c='Employee');
                          
        User u1 = [Select u.Profile.Name, u.ProfileId, u.Name, u.Id From User u where u.Profile.Name = 'System Administrator' limit 1];
                          
        return u;
    }
    
    public User CreateUser(string userType)
    {
        //Get a profil id from the data base to assign to our user in this case make them a sys admin
        Profile p = [select id from profile where name=:userType];
           
           //Create a new sys admin user and do an insert  
        User ur = new User(alias = 'standt', email='noEmail@testorg.com',
                          emailencodingkey='UTF-8', lastname='Testo', languagelocalekey='en_US', localesidkey='en_US', profileid = p.Id,
                          timezonesidkey='America/Los_Angeles', username='MrTestUser@testorg.com',Resource_Type__c='Employee');
           return ur;
    }
}
divya1234divya1234
Can you please assist me here :)
v varaprasadv varaprasad
Hi Divya,

You can call like below:


TestClassHelper tc = new TestClassHelper();
User usr = tc .CreateUser('System Administrator');

Hope it helps you.

Thanks
varaprasad