You need to sign in to do that
Don't have an account?
Salesforce 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
heres class
here's trigger
the validation error
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; } } } }
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
Additionally, I would recommend that you use System.assertEquals and System.assertNotEquals to get better debug information
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.
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,