0

I'm trying to deactivate a user in an apex test. But am getting an error that the user (that I just created in the test) in being used by workflow tasks. The class implements SandboxPostCopy and is to be run upon sandbox refresh. Activation works fine, but deactivation fails. Please see code below.

Error is:

System.DmlException: Update failed. First exception on row 34 with id 00546000001gcOdAAI; first error: DEPENDENCY_EXISTS, Cannot complete this operation. This user is being used by workflow tasks. : Workflow Task : []

Class:

global class PrepSandbox1 implements SandboxPostCopy {
    global void runApexClass(SandboxContext context) {
        System.debug('Org ID: ' + context.organizationId());
        System.debug('Sandbox ID: ' + context.sandboxId());
        System.debug('Sandbox Name: ' + context.sandboxName());

        List<User> toUpdateDeactiv = new List<User>();
        List<User> toUpdateActiv = new List<User>();

        // List of users to deactivate that aren't needed in sandbox
        List<User> toDeactivate = [SELECT Id FROM User WHERE Sandbox_User_Activation__c = 'Deactivate'];
        for (User d : toDeactivate) {
            d.IsActive = false;
            toUpdateDeactiv.add(d);
        }
        update toUpdateDeactiv;

        // List of users to activate and correct email address
        List<User> toActivate = [SELECT Id, Email FROM User WHERE Sandbox_User_Activation__c = 'Activate'];
        for (User a : toActivate) {
            a.IsActive = true;
            if (a.Email.endsWith('.invalid')) {
                a.Email = a.Email.substring(0, a.Email.length() - 8);
            }
            toUpdateActiv.add(a);
        }
        update toUpdateActiv;
    }
}

Test:

@isTest
class Test_PrepSandbox1 {

    @isTest
    static void testSandboxPrep1() {

        // Create 3 test users
        // Inactive to activate
        Profile profileId = [SELECT Id FROM Profile WHERE Name = 'Standard User' LIMIT 1];
        User u1 = new User(LastName = 'TestUser1',
                          Email = 'test@test.com.invalid',
                          Alias = 'Tcode',
                          Username = 'test' + DateTime.now().getTime() + '@test.com',
                          CommunityNickname = 'test' + DateTime.now().getTime(),
                          LocaleSidKey = 'en_US',
                          TimeZoneSidKey = 'GMT',
                          ProfileID = profileId.Id,
                          LanguageLocaleKey = 'en_US',
                          EmailEncodingKey = 'UTF-8', 
                          Sandbox_User_Activation__c='Activate',
                          IsActive=false);
        insert u1;

        // Active to deactivate
        User u2 = new User(LastName = 'TestUser2',
                          Email = 'test@test.com',
                          Alias = 'Tcode',
                          Username = 'test' + DateTime.now().getTime() + '@test.com',
                          CommunityNickname = 'test' + DateTime.now().getTime(),
                          LocaleSidKey = 'en_US',
                          TimeZoneSidKey = 'GMT',
                          ProfileID = profileId.Id,
                          LanguageLocaleKey = 'en_US',
                          EmailEncodingKey = 'UTF-8', 
                          Sandbox_User_Activation__c='Deactivate',
                          IsActive=true);
        insert u2;

        // Active to not touch
        User u3 = new User(LastName = 'TestUser3',
                          Email = 'test@test.com',
                          Alias = 'Tcode',
                          Username = 'test' + DateTime.now().getTime() + '@test.com',
                          CommunityNickname = 'test' + DateTime.now().getTime(),
                          LocaleSidKey = 'en_US',
                          TimeZoneSidKey = 'GMT',
                          ProfileID = profileId.Id,
                          LanguageLocaleKey = 'en_US',
                          EmailEncodingKey = 'UTF-8',
                          IsActive=false);
        insert u3;

        Test.startTest();

        Test.testSandboxPostCopyScript(
            new PrepSandbox1(), UserInfo.getOrganizationId(),
                UserInfo.getOrganizationId(), UserInfo.getOrganizationName());

        Test.stopTest();

        // Check results
        User result1 = [SELECT Id, IsActive, Email FROM User WHERE Id = :u1.id];
        User result2 = [SELECT Id, IsActive FROM User WHERE Id = :u2.id];
        User result3 = [SELECT Id, IsActive FROM User WHERE Id = :u3.id];
        System.AssertEquals(true, result1.IsActive);
        System.AssertEquals('test@test.com', result1.Email);
        System.AssertEquals(false, result2.IsActive);
        System.AssertEquals(false, result3.IsActive);
    }
}
  • Does the error message provide the line # or can you specify which update is throwing the error? – Kris Goncalves Mar 5 at 22:46
0

Since I have never used SandboxPostCopy before, I thought there was something wrong with my code. But it turns out that it was seeing all my real data and there was someone I was trying to deactivate that was in a workflow task. I didn't realize it would include all data in the query I was doing, not just my test users. I did not have (seeAllData=true), so I am confused about that.

| improve this answer | |
  • User records are always copied from PROD to sandbox; testmethods should be designed to ensure only MOCKED users are fetched by the code-under-test – cropredy Mar 5 at 23:44
  • All users are copied, but I wanted to deactivate certain ones. The TEST was failing because it was trying to deactivate certain REAL users when I ran it. I didn't think it would see real data in the test. – Navigatrix Mar 9 at 16:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.