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
Tanner RussellTanner Russell 

Future method not updating test users

I have a class that fixes sandbox user emails and a test class that inserts users than calls the method to fix the emails. This code works if I just run it but  cant get it to work on the test data. Anything Im missing? It fails on the assert.
 
//Fix emails of the admin users on the sandboxs automatically
public class FixSandboxUsers {
    @future
    public static void FixUsers(){
        //make sure its a sandbox enviroment
        if([SELECT IsSandbox FROM Organization LIMIT 1].IsSandbox){
            //get the system admin profile
            Profile p = [Select id, name from Profile where name='System Administrator' limit 1];
            //check users with that profile
            List<User> use = [Select id, name, email, Profileid from user where profileid = :p.id];
            //update each users email
            for(User u : use){
                u.email = u.Email.replace('=','@').replace('@example.com','');
                //debug print out the email
               // System.debug(u.Email);
            }
            //update the users
            update use;
        }
    }
}
@isTest
public class FixSandboxUsers_Test {
    @isTest
    public static void FixUsers_Test(){
        Profile profileId = [SELECT Id FROM Profile WHERE Name = 'System Administrator' LIMIT 1];
        List<User> userList = new List<User>();
        for(integer i = 0; i < 10; i++){
            userList.add(new User(LastName = 'LIVESTON' + i,
                                FirstName='JASON' + i,
                                Alias = 'jliv' + i,
                                Email = 'TESTADDRESS' + i + '=asdf.com@example.com',
                                Username = 'TESTADDRESS' + i + '@asdf.com',
                                ProfileId = profileId.id,
                                TimeZoneSidKey = 'GMT',
                                LanguageLocaleKey = 'en_US',
                                EmailEncodingKey = 'UTF-8',
                                LocaleSidKey = 'en_US'
                               ));
        }
        insert userList;
        Test.startTest();
        FixSandboxUsers.FixUsers();
        Test.stopTest();
        For(User u : userList){
            System.debug(u.Email);
            System.assert(!u.email.contains('='));
            System.assert(!u.email.contains('@example.com'));
           
        }
       
    }
}


 
Best Answer chosen by Tanner Russell
Amit Chaudhary 8Amit Chaudhary 8
You need to query the user record again. Try like below
@isTest
public class FixSandboxUsers_Test 
{
    @isTest
    public static void FixUsers_Test()
	{
        Profile profileId = [SELECT Id FROM Profile WHERE Name = 'System Administrator' LIMIT 1];
        List<User> userList = new List<User>();
        for(integer i = 0; i < 10; i++)
		{
            userList.add(new User(LastName = 'LIVESTON' + i,
                                FirstName='JASON' + i,
                                Alias = 'jliv' + i,
                                Email = 'TESTADDRESS' + i + '=asdf.com@example.com',
                                Username = 'TESTADDRESS' + i + '@asdf.com',
                                ProfileId = profileId.id,
                                TimeZoneSidKey = 'GMT',
                                LanguageLocaleKey = 'en_US',
                                EmailEncodingKey = 'UTF-8',
                                LocaleSidKey = 'en_US'
                               ));
        }
        insert userList;
		
        Test.startTest();
		
			FixSandboxUsers.FixUsers();
			
        Test.stopTest();
		List<User> lstUpdatedUser = [select email from user ];
		
        For(User u : lstUpdatedUser )
		{
            System.debug(u.Email);
            System.assert(!u.email.contains('='));
            System.assert(!u.email.contains('@example.com'));
           
        }
       
    }
}

Let us know if this will help you

 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
You need to query the user record again. Try like below
@isTest
public class FixSandboxUsers_Test 
{
    @isTest
    public static void FixUsers_Test()
	{
        Profile profileId = [SELECT Id FROM Profile WHERE Name = 'System Administrator' LIMIT 1];
        List<User> userList = new List<User>();
        for(integer i = 0; i < 10; i++)
		{
            userList.add(new User(LastName = 'LIVESTON' + i,
                                FirstName='JASON' + i,
                                Alias = 'jliv' + i,
                                Email = 'TESTADDRESS' + i + '=asdf.com@example.com',
                                Username = 'TESTADDRESS' + i + '@asdf.com',
                                ProfileId = profileId.id,
                                TimeZoneSidKey = 'GMT',
                                LanguageLocaleKey = 'en_US',
                                EmailEncodingKey = 'UTF-8',
                                LocaleSidKey = 'en_US'
                               ));
        }
        insert userList;
		
        Test.startTest();
		
			FixSandboxUsers.FixUsers();
			
        Test.stopTest();
		List<User> lstUpdatedUser = [select email from user ];
		
        For(User u : lstUpdatedUser )
		{
            System.debug(u.Email);
            System.assert(!u.email.contains('='));
            System.assert(!u.email.contains('@example.com'));
           
        }
       
    }
}

Let us know if this will help you

 
This was selected as the best answer
Tanner RussellTanner Russell
Omg such a silly mistake thanks a lot!