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
Alex KirbyAlex Kirby 

Dynamically changing inactive account owners.

HI All,

I have put a small schedulable class together to run in the evenings to identify accounts with inactive owners and change them to a holding owner until they are ready to be allocated again.

The reason they need to be with an active owner is due to us pushing in data from external systems over night to keep out accounts update.

Here is the class:
 
global class inactiveAccountOwnerChange implements schedulable {
    
    global void execute(SchedulableContext SC) {
        id SFUser;
        List <Account> accountToUpdate = [Select Id, owner.Isactive from Account where owner.isactive = False];
        List <User> SFU = [Select id from user where name = 'Salesforce Unallocated'];
        
        SFUser = string.valueof(SFU[0].get('id'));
        
        for(Account a : accountToUpdate){  
            
            a.ownerid = SFUser;         
            
            update a;
        }
    }
}

This seems to work fine the issue I am having is I am only able to cover 75% of the class due to the "inactive" owner...

Here is my test class:
 
@isTest
public class test_inactiveAccountOwnerChange {
    
    static id usr;  
    
    Private Static Void Init(){  
        
        Profile p = [SELECT Id FROM Profile WHERE Name='ACM (Business Movers)']; 
        
        User u = new User(
            Alias = 'standt34', 
            Email='standarduser12234@testorg.com', 
            EmailEncodingKey='UTF-8', 
            LastName='Testing', 
            LanguageLocaleKey='en_US', 
            LocaleSidKey='en_US', 
            ProfileId = p.Id, 
            TimeZoneSidKey='America/Los_Angeles',
            UserName='standarduser123451@testorg.com');
        
        insert u;
        usr = u.id;
        
        account a = new account();
        a.Name = 'Test';
        a.REG_ADD_LINE_1__c = 'Line 1';
        a.REG_ADD_LINE_2__c = 'Line 2';
        a.REG_ADD_LINE_3__c = 'Line 3';
        a.REG_ADD_LINE_4__c = 'Line 4';
        a.REG_ADD_TOWN__c = 'Town';
        a.REG_ADD_POSTCODE__c = 'B63 02H';
        a.Company_Type__c = 'Limited';
        a.OwnerId = u.id;
        
        Insert a;  
    }
    
    
    
    static testMethod void inactiveOwners() {   
        
        Test.startTest();
        
        init();
        
        String CRON_EXP = '0 0 0 1 1 ? 2025';  
        String jobId = System.schedule('testScheduledApex', CRON_EXP, new inactiveAccountOwnerChange() );
        CronTrigger ct = [select id, CronExpression, TimesTriggered, NextFireTime from CronTrigger where id = :jobId];
        
        System.assertEquals(CRON_EXP, ct.CronExpression); 
        System.assertEquals(0, ct.TimesTriggered);
        System.assertEquals('2025-01-01 00:00:00', String.valueOf(ct.NextFireTime));
        
        Test.stopTest();       
    }
}
I insert a test user as active, insert the account with the active user.. I am unable to set this user to inactive at this point which in turn does not cover the final part of my class lines 12 and 14..

Any ideas on how I can get round this ?

Thanks


 
Amit Chaudhary 8Amit Chaudhary 8
Pleased try  below code. I hope that will help you.
According to your code Account owner should be inactive.
@isTest
public class test_inactiveAccountOwnerChange {
    
    static id usr;  
    
    Private Static Void Init(){  
        
        Profile p = [SELECT Id FROM Profile WHERE Name='ACM (Business Movers)']; 
        
        User u = new User(
            Alias = 'standt34', 
            Email='standarduser12234@testorg.com', 
            EmailEncodingKey='UTF-8', 
            LastName='Testing', 
            LanguageLocaleKey='en_US', 
            LocaleSidKey='en_US', 
            ProfileId = p.Id, 
            TimeZoneSidKey='America/Los_Angeles',
            UserName='standarduser123451@testorg.com');
        
        insert u;

        
        account a = new account();
        a.Name = 'Test';
        a.REG_ADD_LINE_1__c = 'Line 1';
        a.REG_ADD_LINE_2__c = 'Line 2';
        a.REG_ADD_LINE_3__c = 'Line 3';
        a.REG_ADD_LINE_4__c = 'Line 4';
        a.REG_ADD_TOWN__c = 'Town';
        a.REG_ADD_POSTCODE__c = 'B63 02H';
        a.Company_Type__c = 'Limited';
        a.OwnerId = u.id;
        Insert a;  
		
		u.isactive = false;
		update u;
		
    }
    
    
    
    static testMethod void inactiveOwners() {   
        
        Test.startTest();
        
        init();
        
        String CRON_EXP = '0 0 0 1 1 ? 2025';  
        String jobId = System.schedule('testScheduledApex', CRON_EXP, new inactiveAccountOwnerChange() );
        CronTrigger ct = [select id, CronExpression, TimesTriggered, NextFireTime from CronTrigger where id = :jobId];
        
        System.assertEquals(CRON_EXP, ct.CronExpression); 
        System.assertEquals(0, ct.TimesTriggered);
        System.assertEquals('2025-01-01 00:00:00', String.valueOf(ct.NextFireTime));
        
        Test.stopTest();       
    }
}


 
Alex KirbyAlex Kirby
I originally tried this and the test failed with the following error:

System.DmlException: Update failed. First exception on row 0 with id 00519000000thn7AAA; first error: MIXED_DML_OPERATION, DML operation on setup object is not permitted after you have updated a non-setup object (or vice versa): User, original object: Account: []
Amit Chaudhary 8Amit Chaudhary 8
You can try RunAs .Please try below code. I hope that will help u
@isTest
public class test_inactiveAccountOwnerChange {
    
    static id usr;  
    
    Private Static Void Init(){  
        
        Profile p = [SELECT Id FROM Profile WHERE Name='ACM (Business Movers)']; 
        
        User u = new User(
            Alias = 'standt34', 
            Email='standarduser12234@testorg.com', 
            EmailEncodingKey='UTF-8', 
            LastName='Testing', 
            LanguageLocaleKey='en_US', 
            LocaleSidKey='en_US', 
            ProfileId = p.Id, 
            TimeZoneSidKey='America/Los_Angeles',
            UserName='standarduser123451@testorg.com');
        
        insert u;

        System.RunAs(u)
		{
			account a = new account();
			a.Name = 'Test';
			a.REG_ADD_LINE_1__c = 'Line 1';
			a.REG_ADD_LINE_2__c = 'Line 2';
			a.REG_ADD_LINE_3__c = 'Line 3';
			a.REG_ADD_LINE_4__c = 'Line 4';
			a.REG_ADD_TOWN__c = 'Town';
			a.REG_ADD_POSTCODE__c = 'B63 02H';
			a.Company_Type__c = 'Limited';
			a.OwnerId = u.id;
			Insert a;  
		}
		
		u.isactive = false;
		update u;
		
    }
    
    
    
    static testMethod void inactiveOwners() {   
        
        Test.startTest();
        
        init();
        
        String CRON_EXP = '0 0 0 1 1 ? 2025';  
        String jobId = System.schedule('testScheduledApex', CRON_EXP, new inactiveAccountOwnerChange() );
        CronTrigger ct = [select id, CronExpression, TimesTriggered, NextFireTime from CronTrigger where id = :jobId];
        
        System.assertEquals(CRON_EXP, ct.CronExpression); 
        System.assertEquals(0, ct.TimesTriggered);
        System.assertEquals('2025-01-01 00:00:00', String.valueOf(ct.NextFireTime));
        
        Test.stopTest();       
    }
}

Please let us know if this will help u
Alex KirbyAlex Kirby
Hi there,

I get the same error :(.

Thanks,
Amit Chaudhary 8Amit Chaudhary 8
PLease try below test class. I hope that will help u
@isTest
public class test_inactiveAccountOwnerChange 
{
    static testMethod void inactiveOwners() 
	{
		Profile p = [SELECT Id FROM Profile WHERE Name='Standard User']; 
        User usr = new User(Alias = 'standt', Email='standarduser@testorg.com', 
					EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
					LocaleSidKey='en_US', ProfileId = p.Id, 
					TimeZoneSidKey='America/Los_Angeles', UserName='standarduser@testorg.com');
		insert usr ;
		
		User u ;
		System.runAs(usr)
		{
			Profile p = [SELECT Id FROM Profile WHERE Name='ACM (Business Movers)']; 
			u = new User(
				Alias = 'standt34', 
				Email='standarduser12234@testorg.com', 
				EmailEncodingKey='UTF-8', 
				LastName='Testing', 
				LanguageLocaleKey='en_US', 
				LocaleSidKey='en_US', 
				ProfileId = p.Id, 
				TimeZoneSidKey='America/Los_Angeles',
				UserName='standarduser123451@testorg.com');
			insert u;

			Account a = new Account();
				a.Name = 'Test';
				a.REG_ADD_LINE_1__c = 'Line 1';
				a.REG_ADD_LINE_2__c = 'Line 2';
				a.REG_ADD_LINE_3__c = 'Line 3';
				a.REG_ADD_LINE_4__c = 'Line 4';
				a.REG_ADD_TOWN__c = 'Town';
				a.REG_ADD_POSTCODE__c = 'B63 02H';
				a.Company_Type__c = 'Limited';
				a.OwnerId = u.id;
			Insert a;  
		}

        Test.startTest();
			u.isactive = false;
			update u;
			
			String CRON_EXP = '0 0 0 1 1 ? 2025';  
			String jobId = System.schedule('testScheduledApex', CRON_EXP, new inactiveAccountOwnerChange() );
			CronTrigger ct = [select id, CronExpression, TimesTriggered, NextFireTime from CronTrigger where id = :jobId];
			
			System.assertEquals(CRON_EXP, ct.CronExpression); 
			//System.assertEquals(0, ct.TimesTriggered);
			System.assertEquals('2025-01-01 00:00:00', String.valueOf(ct.NextFireTime));
        Test.stopTest();       
    }
}

 
Alex KirbyAlex Kirby
Same problem, I have checked out various other posts and can't get my head round it.