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
SeanGormanSeanGorman 

Test Class causing error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY,

We have a trigger that, when a user is updated from Inactive to Active resets that persons password.

 

When I test in full sandbox it seems fine.

 

When I release it in a changeset it causes the below issue:

 

Failure Message: "System.DmlException: Update failed. First exception on row 0 with id 005800000059ETvAAM; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, UserPasswordReset: execution of AfterUpdate caused by: System.UnexpectedException: UNKNOWN_EXCEPTION: An unexpected error occurred. Please include this ErrorId if you .

Trigger:

trigger UserPasswordReset on User (after update) {
   for (User u : Trigger.new){
      // Compares the new value of isActive flag on User table to the old value  
      if (u.isActive && !Trigger.oldMap.get(u.Id).isActive) {
         // If new value isActive is true and old value is false the user's 
         // password will be reset and an email notification will
         // be sent to them.
         System.resetPasswordResult rpr = System.resetPassword(u.Id, true);
      }
   } 
}

 

Test Class

@isTest(SeeAllData=TRUE) 
private class UserUpdateTriggerTest {

    static testMethod void UserTriggerTest() {

        Profile profile = [select id, Name from profile where name='Consumer Standard User'];
        User thisUser = [ select Id from User where Id = :UserInfo.getUserId() ];
        List<User> uList = new List<User>();
        for (Integer i=0; i<10; i++){
            uList.add(new User(alias             = 'testu'+i
                    ,email             ='test_user_update'+i+'@test.com'
                    ,emailencodingkey  ='UTF-8'
                    ,lastname          ='test_user_update'+i
                    ,languagelocalekey ='en_US'
                    ,localesidkey      ='en_US'
                    ,profileid         = profile.Id
                    ,timezonesidkey    ='America/Los_Angeles'
                    ,username          ='test_user_update'+i+'@test.com'
                    ,Party_Id__c       = 'XX123'+i
                    ,isActive          = false
            ));
        }

        insert uList;

        system.runas(thisUser)
        {
            for (Integer i=0; i<10; i++) {
                uList[i].isActive = true;
            }
            update uList;
        }

    }

}

 

SeanGormanSeanGorman

thing to note:

 

When we "Run All Tests" or run this class in the developer console, no error is shown, only when we try to deploy any change set.

GunnarGunnar

* May not need 'See All Data', you are creating your own data.

 

* Try using valid email addresses. Go to Yahoo and create some.

 

* With the 'update', isn't your trigger firing again? Maybe use 'before'?

 

* To isolate your 'test data' from the test, use StartTest and EndTest in the 'RunAs' area.

SeanGormanSeanGorman
I added the SeeAllData just as a last gasp. I will be looking at the rest now. Thank you Gunnar