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
Salesforce Admin 110Salesforce Admin 110 

unit test class fails validation on deployment

hi all i have a trigger that gains 100% coverage with unit test class in sandbox but fails on validation while trying to deploy to production

the validation error

User-added image

heres class
 
@istest
public class contactTriggerTest{

private static Contact ct;

@testsetup
static void createContact(){
    ct = new Contact(FirstName = 'Iain',LastName = 'Banks');
    insert ct;
}

private static testmethod void testReassignUsers(){
    createContact();
    Test.startTest();
        Contact thisCt = [select id, Allocated_User__c, FirstName from Contact where Id =:ct.id];
        system.assert(thisCt.Allocated_User__c != null);
        
        //update a different field shouldnt change the Allocated_User__c
        thisCt.FirstName = 'Steve';
        update thisCt;
        Contact thisCt2 = [select id, Allocated_User__c, FirstName from Contact where Id =:ct.id];
        system.assert(thisCt2.Allocated_User__c == thisCt2.Allocated_User__c);
        
        //update Allocated_User__c to null should reassign Allocated_User__c 
        thisCt2.Allocated_User__c =  null;
        update thisCt2;
        thisCt = [select id, Allocated_User__c, FirstName from Contact where Id =:ct.id];
        system.assert(thisCt.Allocated_User__c != null);
    Test.stopTest();
}

}

here's trigger
trigger popallocateduser on Contact ( before insert,before update) {

 //unittest called contactTriggerTest

Set<String> roleNames = new Set<String>();

roleNames.add('Listers');
    //lister roleid

List<User> userList = [select id from User where isActive = true  and userrole.name in :roleNames];
if(userList.size() > 0){
    for(Contact con : trigger.new){
        if(con.Allocated_User__c == null){
            integer randomIntInRange = Math.round(Math.random() * userList.size()); 
            
con.Allocated_User__c = userList [(randomIntInRange > 0) ? randomIntInRange - 1 : randomIntInRange].id;
        }
    }

}

}


 
Best Answer chosen by Salesforce Admin 110
Pankaj PPankaj P

Looking at your code, In trigger class "popallocateduser", you are fetching Users with a role which is hard-coded as "Listers". So I think There are no User's in your Production Org having Role name "Listers".

Solution : Either you can create User in Production with Role name "Listers" and then deploy. Or You can create User in your testData itself with Role Name "Listers".

Hope this will resolve your problem.

Thanks,
Pankaj.

 

All Answers

pconpcon
Do you have any users in production that have the role of Listers?  You should think about creating active users as part of your test to ensure that there is at least one in place.

Additionally, I would recommend that you use System.assertEquals and System.assertNotEquals to get better debug information
 
System.assertNotEquals(
    null, //Expected value
    this.Ct.Allocated_User__c, //Tested value
    'The allocated user should not be null' //Error message
);

 
Pankaj PPankaj P

Looking at your code, In trigger class "popallocateduser", you are fetching Users with a role which is hard-coded as "Listers". So I think There are no User's in your Production Org having Role name "Listers".

Solution : Either you can create User in Production with Role name "Listers" and then deploy. Or You can create User in your testData itself with Role Name "Listers".

Hope this will resolve your problem.

Thanks,
Pankaj.

 

This was selected as the best answer
Arunkumar RArunkumar R
Hi,

You need to insert a USER ROLE in your test class and need to insert a contact with the inserted user. Find the updated code,
 
@istest
public class contactTriggerTest{

static user insertUser(){

        Profile pf = [SELECT Id FROM Profile WHERE Name = 'System Administrator'];
       
        UserRole ur = new UserRole(Name = 'Listers');
        insert ur;
        
        String orgId = UserInfo.getOrganizationId();
        String dateString = String.valueof(Datetime.now()).replace(' ','').replace(':','').replace('-','');
       
        Integer randomInt = Integer.valueOf(math.rint(math.random()*1000000));
        String uniqueName = orgId + dateString + randomInt;
        User tuser = new User(  firstname = 'TEST First',
                                lastName = 'TEST LAST',
                                email = uniqueName + '@test' + orgId + '.org',
                                Username = uniqueName + '@test' + orgId + '.org',
                                EmailEncodingKey = 'ISO-8859-1',
                                Alias = uniqueName.substring(18, 23),
                                TimeZoneSidKey = 'America/Los_Angeles',
                                LocaleSidKey = 'en_US',
                                LanguageLocaleKey = 'en_US',
                                ProfileId = pf.Id,
                                UserRoleId = ur.Id);
        insert tuser;
        return tuser;
        

}

private static testmethod void testReassignUsers(){

   Test.startTest();
   System.runAs(insertUser())
    {
        Contact ct = new Contact(FirstName = 'Iain',LastName = 'Banks');
        insert ct;
    
        Contact thisCt = [select id, FirstName, Allocated_User__c from Contact where Id =:ct.id];
        system.assert(thisCt.Allocated_User__c != null);
        
        //update a different field shouldnt change the Allocated_User__c
        thisCt.FirstName = 'Steve';
        update thisCt;
        Contact thisCt2 = [select id, FirstName, Allocated_User__c from Contact where Id =:ct.id];
        system.assert(thisCt2.Allocated_User__c == thisCt2.Allocated_User__c);
        
        //update Allocated_User__c to null should reassign Allocated_User__c 
        thisCt2.Allocated_User__c =  null;
        update thisCt2;
        thisCt = [select id, FirstName, Allocated_User__c  from Contact where Id =:ct.id];
        system.assert(thisCt.Allocated_User__c != null);
        }
    Test.stopTest();
}

}

 
Salesforce Admin 110Salesforce Admin 110
thanks this was just a case of getting a user in system with lister role thanks to everyone for your input