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
NLTNLT 

Getting the error in test class

Getting this error "System.DmlException: Update failed. First exception on row 106 with id 005800000057YTmAAM; first error: DEPENDENCY_EXISTS, Cannot complete this operation. This user is a target of a workflow field update. : Field Update : []" while executing below test class, pls tell what the issue?

@isTest(SeeAllData=false)
private class DirectRoleServiceTest {   
    private static User usr;
    
    private static void setupData() {
        TestUtility.insertEmployee(true);
        
        usr = [Select id from User where Id = :UserInfo.getUserId()];
    }
    
    static testMethod void test_processAllArea() {
        setupData();        
        
        System.RunAs(usr){
            Test.startTest();
            DirectRoleService dsr = new DirectRoleService();
            dsr.processMultiMaster('Area');
            dsr.processMainMaster('Area');
            dsr.processSharedMaster('Area');
            dsr.processUsersMaster('AVP');            
            Test.stopTest();
        }       
    }
    
    static testMethod void test_processAllRegion() {
        setupData();        
        
        System.RunAs(usr){
            Test.startTest();
            DirectRoleService dsr = new DirectRoleService();
            dsr.processMultiMaster('Region');
            dsr.processMainMaster('Region');
            dsr.processSharedMaster('Region');
            dsr.errorDoubleParent('RD','Manager');
            dsr.errorDoubleParent('RD','Location');
            dsr.errorMultiPNumber();
            dsr.processUsersMaster('RD');
            Test.stopTest();
        }
    }
    
    static testMethod void test_processAllSnrManager() {
        setupData();        
        
        System.RunAs(usr){
            Test.startTest();
            DirectRoleService dsr = new DirectRoleService();
            dsr.processMultiMaster('SM');
            dsr.processMainMaster('SM');
            dsr.processSharedMaster('SM');
            dsr.errorDoubleParent('SM','Manager');
            dsr.errorDoubleParent('SM','Location');
            dsr.errorMultiPNumber();
            dsr.processUsersMaster('SM');
            Test.stopTest();
        }
    }
    
    static testMethod void test_processAllTeam() {
        setupData();        
        
        System.RunAs(usr){
            Test.startTest();
            DirectRoleService dsr = new DirectRoleService();
            dsr.processMultiMaster('Team');
            dsr.processMainMaster('Team');
            dsr.processSharedMaster('Team');
            dsr.errorDoubleParent('TM','Manager');
            dsr.errorDoubleParent('TM','Location');
            dsr.processUsersMaster('TM');                
            Test.stopTest();
        }
    }
    
    static testMethod void test_processAllTeamRep() {
        setupData();        
        
        System.RunAs(usr){
            Test.startTest();
            DirectRoleService dsr = new DirectRoleService();                
            dsr.processMainMaster('TeamRep');
            dsr.processUsersMaster('AE'); 
            Test.stopTest();
        }
    }
    
    static testMethod void test_deactivateUser() {
        usr = [Select id from User where Id = :UserInfo.getUserId()];
        Employee__c emp = new Employee__c();
        emp.Name = 'Shawna Witek';
        emp.NTLogin__c = 'SWITEK1';
        emp.PNumber__c = '12514249';
        emp.Position_Title__c = 'P12545788';
        emp.EffDate__c = System.today();
        emp.Last_FOS_Update__c = System.today();
        emp.Primary_Dealer_Code__c = '3786149';
        emp.BD_Dealer_Code__c = '4489637';
        emp.Co_Sell_Dealer_Code__c = '4847288';
        emp.Segment__c = 'Area Sales';
        
        emp.Area__c = 'North Central';
        emp.Area_Vice_President__c = 'Marty Pisciotti';
        emp.AVP_PNumber__c = '12442681';
        emp.AVP_NTLogin__c = 'MPISCIO';
        emp.AVP_Email__c = 'martin.pisciotti@t-mobile.com';
        
        
        emp.Region__c = 'Houston';
        emp.Regional_Director__c = 'Winston Awadzi';
        emp.RD_PNumber__c = '12545788';
        emp.RD_NTLogin__c = 'WAWADZI';
        emp.RD_Email__c = 'winston.awadzi@t-mobile.com';
        
        emp.Senior_Manager__c = 'leo';
        emp.SM_PNumber__c = '1234567';
        emp.SM_NTLogin__c = '1234567';
        emp.SM_Email__c = 'winston@t-mobile.com';
        
        emp.Team__c = 'Chicago 1';
        emp.Team_Manager__c = 'Kevin Kirby';
        emp.TM_PNumber__c = '12427977';
        emp.TM_NTLogin__c = 'KKIRBY1';
        emp.TM_Email__c = 'kevin.kirby1@t-mobile.com';
        insert emp; 
        System.RunAs(usr){
            UserRole r = new UserRole(DeveloperName = 'AS_Test', Name = 'AS Test');
            insert r;
            User u = new User(
                ProfileId = [SELECT Id FROM Profile WHERE Name = 'Standard User'].Id,
                LastName = 'last',
                Email = 'test@amamama.com',
                Username = 'puser000@amamama.com' + System.currentTimeMillis(),
                CompanyName = 'TEST',
                Title = 'title',
                Alias = 'alias',
                TimeZoneSidKey = 'America/Los_Angeles',
                EmailEncodingKey = 'UTF-8',
                LanguageLocaleKey = 'en_US',
                LocaleSidKey = 'en_US',
                UserRoleId = r.Id,
                FOS_Object__c = 'Employee',
                Last_FOS_Update__c = System.today(),
                IsActive = TRUE,
                P_Number__c='12345'
            );
            insert u;
            List<User> lstus = New List<User>();
            Profile p = [SELECT Id FROM Profile WHERE Name='Sales and Marketing - ISS - ISDC Enabled'];
            UserRole ur = [select id from userrole where name='Area - Southeast']; 
            User u2 = new User(Alias = 'newUser', Email='newuser@testorg.com', 
                               EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
                               LocaleSidKey='en_US', ProfileId = p.Id,userroleid = ur.id, TimeZoneSidKey='America/Los_Angeles', UserName='first.test@infosys.com.prod.backlogs');            
            User u3 = new User(Alias = 'newUser123', Email='newuser123@testorg.com', 
                               EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
                               LocaleSidKey='en_US', ProfileId = p.Id,userroleid = ur.id, TimeZoneSidKey='America/Los_Angeles', UserName='first.test123@infosys.com.prod.backlogs');           
            User u4 = new User(Alias = 'newUserqwe', Email='newuserqwe@testorg.com', 
                               EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
                               LocaleSidKey='en_US', ProfileId = p.Id,userroleid = ur.id, TimeZoneSidKey='America/Los_Angeles', UserName='first.testqwe@infosys.com.prod.backlogs');
            User u5 = new User(Alias = 'newUserrty', Email='newuserrty@testorg.com',
                               EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
                               LocaleSidKey='en_US', ProfileId = p.Id,userroleid = ur.id, TimeZoneSidKey='America/Los_Angeles', UserName='first.testrty@infosys.com.prod.backlogs');
            User u6 = new User(Alias = 'newUserqaz', Email='newuserqaz@testorg.com', 
                               EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
                               LocaleSidKey='en_US', ProfileId = p.Id,userroleid = ur.id, TimeZoneSidKey='America/Los_Angeles', UserName='first.testqaz@infosys.com.prod.backlogs');
            User u7 = new User(Alias = 'newUserxsw', Email='newuserxsw@testorg.com', 
                               EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
                               LocaleSidKey='en_US', ProfileId = p.Id,userroleid = ur.id, TimeZoneSidKey='America/Los_Angeles', UserName='first.testxsw@infosys.com.prod.backlogs');
            User u8 = new User(Alias = 'newUseredc', Email='newuseredc@testorg.com', 
                               EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
                               LocaleSidKey='en_US', ProfileId = p.Id,userroleid = ur.id, TimeZoneSidKey='America/Los_Angeles', UserName='first.testedc@infosys.com.prod.backlogs');
            User u9 = new User(Alias = 'newUserqsc', Email='newuserqsc@testorg.com', 
                               EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
                               LocaleSidKey='en_US', ProfileId = p.Id,userroleid = ur.id, TimeZoneSidKey='America/Los_Angeles', UserName='first.testqsc@infosys.com.prod.backlogs');
            User u10 = new User(Alias = 'newUserzxc', Email='newuserzxc@testorg.com', 
                                EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
                                LocaleSidKey='en_US', ProfileId = p.Id,userroleid = ur.id, TimeZoneSidKey='America/Los_Angeles', UserName='first.testzxc@infosys.com.prod.backlogs');
            User u11 = new User(Alias = 'newUseriop', Email='newuseriop@testorg.com',
                                EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
                                LocaleSidKey='en_US', ProfileId = p.Id,userroleid = ur.id, TimeZoneSidKey='America/Los_Angeles', UserName='first.testiop@infosys.com.prod.backlogs');
            User u12 = new User(Alias = 'newUserghj', Email='newuserghj@testorg.com', 
                                EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
                                LocaleSidKey='en_US', ProfileId = p.Id,userroleid = ur.id, TimeZoneSidKey='America/Los_Angeles', UserName='first.testghj@infosys.com.prod.backlogs');
            User u13 = new User(Alias = 'newUserujn', Email='newuserujn@testorg.com', 
                                EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
                                LocaleSidKey='en_US', ProfileId = p.Id,userroleid = ur.id, TimeZoneSidKey='America/Los_Angeles', UserName='first.testujn@infosys.com.prod.backlogs');            
            
            for(User us :[SELECT Id, P_Number__c, IsActive, ManagerId, DelegatedApproverId, Area_Vice_President__c, Director__c, Sr_Sales_Manager__c, Sales_Manager__c,
                          Coach__c, rsbr__c, Sales_Engineer__c, uf__User_Champion__c, Advantage_Program_Rep__c, Indirect_RSM__c
                          FROM User ]) {
                              us.ManagerId = u2.id;
                              us.DelegatedApproverId = u3.Id;
                              us.Area_Vice_President__c = u4.Id;
                              us.Director__c = u5.Id;
                              us.Sr_Sales_Manager__c = u6.Id; 
                              us.Sales_Manager__c = u7.Id;
                              us.Coach__c = u8.Id; 
                              us.rsbr__c = u9.Id; 
                              us.Sales_Engineer__c = u10.Id; 
                              us.uf__User_Champion__c = u11.Id; 
                              us.Advantage_Program_Rep__c = u12.Id; 
                              us.Indirect_RSM__c = u13.Id;
                              lstus.add(us);
                          }
            update lstus;
            Test.startTest();
            DirectRoleService dsr = new DirectRoleService();                
            dsr.deactivateOldUsers();
            Test.stopTest();
        }   
    }    
}
Vivian Charlie 1208Vivian Charlie 1208

Hi,
 
I believe your code is trying to deactivate the default Workflow user. You can find this user at
Setup-> Create-> Workflow and Approvals-> Process Automation Settings
 
Inorder to overcome this issue let's try few things
1. private static void setupData() {
        TestUtility.insertEmployee(true);
        
        usr = [Select id from User where Id = :UserInfo.getUserId()];
        Create a new user instance instead and insert this user.
    }

2. In your deactivateOldUsers method, just debug the list of ids of user's and verify if the Default WorkFlow user id appears for de-activation

3. Identify a condition or create a checkbox on the user object Never Deactivate and set it to true for the Default Workflow User. (Remember to update this flag if you change the Default Workflow user in the future)

4. User this checkbox as an additional filter for the deactivation and discard any user's where the checkbox is true

 

Thanks

Vivian