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
Afzaal HassanAfzaal Hassan 

Test class failing for some users but not for others

I have the following test class:
@isTest
public class LPP_ServiceAppointmentTest 
{
    @TestSetup static void setupData() 
    {
        system.runAs( new User( ID = UserInfo.getUserId() ) ) 
        {
            //Create Test Accounts
            List<Account> lst_Account = new List<Account> ();

            User l_user = LPP_TestUtility.createUser( true );
            system.assertNotEquals(null, l_user.Id);
            User l_user2 = LPP_TestUtility.createUser(true);
            system.assertNotEquals(null, l_user2.Id);
            User l_user3 = LPP_TestUtility.createUser(true);
            system.assertNotEquals(null, l_user3.id);
            system.runAs(l_user){
                OperatingHours testOperatingHours = LPP_TestUtility.createOperatingHours('Base Operating hours', 'America/Chicago', true);
                system.assertNotEquals(null, testOperatingHours.Id);
                //l_loc & l_location
                ServiceTerritory testServiceTerritoryAssert = LPP_TestUtility.createServiceTerritory('Test Service Territory', testOperatingHours.Id, true);
                system.assertNotEquals(null, testServiceTerritoryAssert.Id);   
                //parent territory for STM
                ServiceTerritory testServiceTerritoryParent = LPP_TestUtility.createServiceTerritory('Test Parent Territory', testOperatingHours.Id, true);
                system.assertNotEquals(null, testServiceTerritoryParent.Id);
                testServiceTerritoryAssert.ParentTerritoryId = testServiceTerritoryParent.Id;
                testServiceTerritoryAssert.User__c = l_user.id;
                update testServiceTerritoryAssert;
                system.assertEquals(testServiceTerritoryAssert.ParentTerritoryId, testServiceTerritoryParent.Id);
                //l_photog
                ServiceResource testServiceResource = LPP_TestUtility.createLeadPhotographerFSL('Lead Photographer', l_user.Id, '1234', true);
                system.assertNotEquals(null, testServiceResource.Id);
                //l_geo_1
                Geographic_Assignment__c testGeoAssign1 = LPP_TestUtility.createGeoAssign('MN', 'Hennepin', null, null, l_user.Id, testserviceResource.Id, true);
                system.assertNotEquals(null, testGeoAssign1);
                //l_acc1
                Account l_acc1 = LPP_TestUtility.createAccount('Minnesota' , 'MN', 'Hennepin', 'Minneapolis', '55404',  false);
                l_acc1.Schools_Verified_Acct__c = true;
                 insert l_acc1;
                 System.AssertNotEquals(null,l_acc1.Id);   
                //Create Test Opportunity
                Opportunity l_opp1 =  LPP_TestUtility.createOpportunity( l_acc1.id , 'Spring' , string.valueOf(system.today().year()), 'Low Interest' , 'NSS - Opportunity' , 'Group' , 'Classroom Groups' , '2019-2020' , false );
                l_opp1.NSS_Won_Reason_Codes__c = 'Referral';
                insert l_opp1;
                system.assertNotEquals(null, l_opp1);
                //l_Photographer1
                ServiceResource testServiceResource1 = LPP_TestUtility.createPhotographerFSL('test photographer 1', l_user2.Id, '2345', true);
                system.assertNotEquals(null, testServiceResource1.Id);
                //l_photographer2
                ServiceResource testServiceResource2 = LPP_TestUtility.createPhotographerFSL('test photographer 2', l_user3.id, '3456', true);
                system.assertNotEquals(null, testServiceResource2.Id);
                //l_PictureType
                WorkType testWorkType = LPP_TestUtility.createWorkType('Same Day Proof', 8, true);
                system.assertNotEquals(null, testWorkType.Id);
                //creating contact
                Contact l_cont1 = LPP_TestUtility.createContact(l_acc1.Id, true);
                system.assertNotEquals(null, l_cont1.Id);            
                WorkOrder testWorkOrder = LPP_TestUtility.createWorkOrder(l_acc1.Id, l_opp1.Id, l_cont1.Id, testWorkType.Id, true);
                system.assertNotEquals(null, testWorkOrder.Id);  
                system.assertNotEquals(null, testWorkOrder.WorkTypeId);
                //create STM for Assigned Resource
                ServiceTerritoryMember testSTM = LPP_TestUtility.createMember(testServiceResource1.Id, testServiceTerritoryAssert.Id, null, false);
                testSTM.EffectiveStartDate = system.now().addDays(-10);
                insert testSTM;
                system.assertNotEquals(null, testSTM.Id);
                //create Service Appointments
                ServiceAppointment testServiceAppointment1 = LPP_TestUtility.createServiceAppointment(l_opp1.Id, 'Scheduled', testWorkOrder.Id, testServiceTerritoryAssert.Id, system.now()-8, false);
                testServiceAppointment1.Photographer__c = testServiceResource1.Id;
                insert testServiceAppointment1;
                system.assertNotEquals(null, testServiceAppointment1.Id);
                //create Assigned Resource for 1st service appointment
                AssignedResource testAssignedResource1 = LPP_TestUtility.createAssignedResource(testServiceResource1.Id, testServiceAppointment1.Id, true);
                system.assertNotEquals(null, testAssignedResource1.Id);
                ServiceAppointment testServiceAppointment2 = LPP_TestUtility.createServiceAppointment(l_opp1.Id, 'Scheduled', testWorkOrder.Id, testServiceTerritoryAssert.Id, system.now()-7, true);    
                system.assertNotEquals(null, testServiceAppointment2.Id);
                //create Assigned Resource for 1st service appointment
                AssignedResource testAssignedResource2 = LPP_TestUtility.createAssignedResource(testServiceResource1.Id, testServiceAppointment2.Id, true);
                system.assertNotEquals(null, testAssignedResource2.Id);   
            }         
        }
        
    }
    // This method covers SamePhotographerError method in LPP_ServiceHandler class 
    @isTest
    Static void checksameDayError() 
    {
        system.runAs( new User( ID = UserInfo.getUserId() ) ) {
            test.startTest();
            List<ServiceAppointment> salist = [select Id, ParentRecordId, Opportunity__c, WorkTypeId, Status, ServiceTerritoryId, schedStartTime, Photographer__r.Id from ServiceAppointment LIMIT 100];
            try
            {
                ServiceAppointment saTest = LPP_TestUtility.createServiceAppointment(salist[0].Opportunity__c, salist[0].Status, salist[0].ParentRecordId, salist[0].ServiceTerritoryId, salist[0].schedStartTime, false);
                saTest.FSL__Related_Service__c = salist[0].Id;
                saTest.FSL__Same_Day__c = true;
                saTest.Photographer__c = salist[0].Photographer__r.Id;
                insert saTest;
            }
            catch (Exception e)
            {
                Boolean expectedExceptionThrown =  e.getMessage().contains('Photographer cannot be the same when Same Day is selected.') ? true : false;
                System.AssertEquals(true, expectedExceptionThrown);              
            }
            test.stopTest();
        }
        
    }
    // This method covers updateOpptyStage method in LPP_ServiceHandler class
    @isTest
    static void updateOpptyStage()
    {
        
        system.runAs( new User( ID = UserInfo.getUserId() ) ) 
        {
            Test.startTest();
            List<ServiceAppointment> salist = [select Id, ParentRecordId, Opportunity__c, WorkTypeId, Status, ServiceTerritoryId, schedStartTime, Photographer__r.Id from ServiceAppointment LIMIT 100];
            salist[0].Status = 'Dispatched';
            update salist[0];
            ServiceAppointment saTest = LPP_TestUtility.createServiceAppointment(salist[0].Opportunity__c, salist[0].Status, salist[0].ParentRecordId, salist[0].ServiceTerritoryId, salist[0].schedStartTime, false);
            Map<Id, String> saMap = new Map<Id, String>();
            saMap.put(saTest.Opportunity__c,'Background Options');
            LPP_ServiceAppointmentHandler.updateOpptyStage(saMap);
            Test.stopTest();
        }
    }
    // This method covers updateOpptyStage method in LPP_ServiceHandler class
    @isTest
    static void updateOpptyStartDate() 
    {
        system.runAs( new User( ID = UserInfo.getUserId() ) ) 
        {
            Test.startTest();
            List<ServiceAppointment> salist = [select Id, ParentRecordId, Opportunity__c, WorkTypeId, Status, ServiceTerritoryId, schedStartTime, Photographer__r.Id from ServiceAppointment LIMIT 100];            
              salist[0].SchedStartTime = system.now()+10;
            salist[0].SchedEndTime = system.now()+11;
            update salist[0];
            delete salist[0];
            undelete salist[0];
            Test.stopTest();
        }
    }
    @isTest
    static void testClearPictureDayLinking()
    {
        system.runAs(new User(Id = UserInfo.getUserId()))
        {
            Test.startTest();
            List<ServiceAppointment> salist = [select Id, ParentRecordId, Opportunity__c, WorkTypeId, Status, ServiceTerritoryId, schedStartTime, Photographer__r.Id from ServiceAppointment LIMIT 100];            
            List<ServiceResource> testSR2;
            
            testSR2 = [Select Id from ServiceResource where name = 'test photographer 2'];
            ServiceAppointment saTest = LPP_TestUtility.createServiceAppointment(salist[0].Opportunity__c, salist[0].Status, salist[0].ParentRecordId, salist[0].ServiceTerritoryId, salist[0].schedStartTime.addDays(1), false);
            saTest.FSL__Related_Service__c = salist[0].Id;
            saTest.FSL__Same_Resource__c = true;

            saTest.Photographer__c = testSR2[0].Id;
            insert saTest;
            saTest.Status = 'Canceled';
            saTest.FSL__Same_Day__c = true;
            update saTest;
            Test.stopTest();
        }
    }
    @isTest
    static void testResetStartEndTimePA()
    {
        system.runAs(new User(Id = UserInfo.getUserId()))
        {
            Test.startTest();
            List<ServiceAppointment> salist = [select Id, ParentRecordId, Set_Up_Time__c, Opportunity__c, WorkTypeId, Status, ServiceTerritoryId, schedStartTime, Photographer__r.Id from ServiceAppointment LIMIT 100];            
            Time testTime;
            //create test service appointment
            ServiceAppointment saTest = LPP_TestUtility.createServiceAppointment(salist[0].Opportunity__c, 'Scheduled', salist[0].ParentRecordId, salist[0].serviceTerritoryId, salist[0].schedStartTime, false);
            
            //set testTime to theoretically updated start time when test is over
            testTime = Time.newInstance(20, 30, 0, 0);   
            saTest.Set_Up_Time__c = '7:30AM';
            insert saTest;
            system.assertNotEquals(null, saTest);
            //triggers the code to run because it's in the BeforeUpdate method
            saTest.Set_Up_Time__c = '8:30PM';
            update saTest;
            //requery and assert
            ServiceAppointment saUpdated = [select Id, ParentRecordId, Set_Up_Time__c, Opportunity__c, WorkTypeId, Status, ServiceTerritoryId, schedStartTime, Photographer__r.Id from ServiceAppointment where Id =:saTest.Id];                        
            system.assertEquals(testTime, saUpdated.SchedStartTime.time());
            Test.stopTest();
        }
    }    @isTest
    static void testResetStartEndTimeAB()
    {
        system.runAs(new User(Id = UserInfo.getUserId()))
        {
            Test.startTest();
            List<ServiceAppointment> salist = [select Id, ParentRecordId, Set_Up_Time__c, Opportunity__c, WorkTypeId, Status, ServiceTerritoryId, schedStartTime, Photographer__r.Id from ServiceAppointment LIMIT 100];            
            Time testTime;
            //create test service appointment
            ServiceAppointment saTest = LPP_TestUtility.createServiceAppointment(salist[0].Opportunity__c, 'Scheduled', salist[0].ParentRecordId, salist[0].serviceTerritoryId, salist[0].schedStartTime, false);
            //set testTime to theoretically updated start time when test is over
            testTime = Time.newInstance(11, 30, 0, 0);   
            saTest.Set_Up_Time__c = '7:30AM';
            insert saTest;
            system.assertNotEquals(null, saTest);
            //triggers the code to run because it's in the BeforeUpdate method
            saTest.Set_Up_Time__c = '11:30AM';
            update saTest;
            //requery and assert
            ServiceAppointment saUpdated = [select Id, ParentRecordId, Set_Up_Time__c, Opportunity__c, WorkTypeId, Status, ServiceTerritoryId, schedStartTime, Photographer__r.Id from ServiceAppointment where Id =:saTest.Id];                        
            system.assertEquals(testTime, saUpdated.SchedStartTime.time());
            Test.stopTest();
        }
    }
    @isTest
    static void testResetStartEndTimePB()
    {
        system.runAs(new User(Id = UserInfo.getUserId()))
        {
            Test.startTest();
            List<ServiceAppointment> salist = [select Id, ParentRecordId, Set_Up_Time__c, Opportunity__c, WorkTypeId, Status, ServiceTerritoryId, schedStartTime, Photographer__r.Id from ServiceAppointment LIMIT 100];            
            Time testTime;
            //create test service appointment
            ServiceAppointment saTest = LPP_TestUtility.createServiceAppointment(salist[0].Opportunity__c, 'Scheduled', salist[0].ParentRecordId, salist[0].serviceTerritoryId, salist[0].schedStartTime, false);
            //set testTime to theoretically updated start time when test is over
            testTime = Time.newInstance(22, 30, 0, 0);   
            saTest.Set_Up_Time__c = '7:30AM';
            insert saTest;
            system.assertNotEquals(null, saTest);
            //triggers the code to run because it's in the BeforeUpdate method
            saTest.Set_Up_Time__c = '10:30PM';
            update saTest;
            //requery and assert
            ServiceAppointment saUpdated = [select Id, ParentRecordId, Set_Up_Time__c, Opportunity__c, WorkTypeId, Status, ServiceTerritoryId, schedStartTime, Photographer__r.Id from ServiceAppointment where Id =:saTest.Id];                        
            system.assertEquals(testTime, saUpdated.SchedStartTime.time());
            Test.stopTest();
        }
    }
    @isTest
    static void testResetStartEndTimePC()
    {
        system.runAs(new User(Id = UserInfo.getUserId()))
        {
            Test.startTest();
            List<ServiceAppointment> salist = [select Id, ParentRecordId, Set_Up_Time__c, Opportunity__c, WorkTypeId, Status, ServiceTerritoryId, schedStartTime, Photographer__r.Id from ServiceAppointment LIMIT 100];            
            Time testTime;
            //create test service appointment
            ServiceAppointment saTest = LPP_TestUtility.createServiceAppointment(salist[0].Opportunity__c, 'Scheduled', salist[0].ParentRecordId, salist[0].serviceTerritoryId, salist[0].schedStartTime, false);
            //set testTime to theoretically updated start time when test is over
            testTime = Time.newInstance(12, 30, 0, 0);   
            saTest.Set_Up_Time__c = '7:30AM';
            insert saTest;
            system.assertNotEquals(null, saTest);
            //triggers the code to run because it's in the BeforeUpdate method
            saTest.Set_Up_Time__c = '12:30PM';
            update saTest;
            //requery and assert
            ServiceAppointment saUpdated = [select Id, ParentRecordId, Set_Up_Time__c, Opportunity__c, WorkTypeId, Status, ServiceTerritoryId, schedStartTime, Photographer__r.Id from ServiceAppointment where Id =:saTest.Id];                        
            system.assertEquals(testTime, saUpdated.SchedStartTime.time());
            Test.stopTest();
        }
    }
    @isTest
    static void testResetStartEndTimeBadData()
    {
        system.runAs(new User(Id = UserInfo.getUserId()))
        {
            Test.startTest();
            List<ServiceAppointment> salist = [select Id, ParentRecordId, Set_Up_Time__c, Opportunity__c, WorkTypeId, Status, ServiceTerritoryId, schedStartTime, Photographer__r.Id from ServiceAppointment LIMIT 100];            
            Time testTime;
            //create test service appointment
            ServiceAppointment saTest = LPP_TestUtility.createServiceAppointment(salist[0].Opportunity__c, 'Scheduled', salist[0].ParentRecordId, salist[0].serviceTerritoryId, salist[0].schedStartTime, false);
            //set testTime to theoretically updated start time when test is over
            testTime = Time.newInstance(7, 0, 0, 0);   
            saTest.Set_Up_Time__c = '7:30AM';
            insert saTest;
            system.assertNotEquals(null, saTest);
            //triggers the code to run because it's in the BeforeUpdate method
            saTest.Set_Up_Time__c = 'this is a test';
            update saTest;
            //requery and assert
            ServiceAppointment saUpdated = [select Id, ParentRecordId, Set_Up_Time__c, Opportunity__c, WorkTypeId, Status, ServiceTerritoryId, schedStartTime, Photographer__r.Id from ServiceAppointment where Id =:saTest.Id];                        
            system.assertEquals(testTime, saUpdated.SchedStartTime.time());
            Test.stopTest();
        }
    }
    @isTest
    static void testResetStartEndTimeNull()
    {
        system.runAs(new User(Id = UserInfo.getUserId()))
        {
            Test.startTest();
            List<ServiceAppointment> salist = [select Id, ParentRecordId, Set_Up_Time__c, Opportunity__c, WorkTypeId, Status, ServiceTerritoryId, schedStartTime, Photographer__r.Id from ServiceAppointment LIMIT 100];            
            Time testTime;
            //create test service appointment
            ServiceAppointment saTest = LPP_TestUtility.createServiceAppointment(salist[0].Opportunity__c, 'Scheduled', salist[0].ParentRecordId, salist[0].serviceTerritoryId, salist[0].schedStartTime, false);
            //set testTime to theoretically updated start time when test is over
            testTime = Time.newInstance(7, 0, 0, 0);   
            saTest.Set_Up_Time__c = '7:30AM';
            insert saTest;
            system.assertNotEquals(null, saTest);
            //triggers the code to run because it's in the BeforeUpdate method
            saTest.Set_Up_Time__c = '';
            update saTest;
            //requery and assert
            ServiceAppointment saUpdated = [select Id, ParentRecordId, Set_Up_Time__c, Opportunity__c, WorkTypeId, Status, ServiceTerritoryId, schedStartTime, Photographer__r.Id from ServiceAppointment where Id =:saTest.Id];                        
            system.assertEquals(testTime, saUpdated.SchedStartTime.time());
            Test.stopTest();
        }
    }
}

I live in EST timezone. My other developer lives in CST. When I run this test class, all the methods testResetStartEndTimePA() onwards are failing. Saying the system.assert values are failing because it expected 1130 for example, actual 12:30. They are all 1 hour behind. When my CST timezone friend runs the test class, everything is passing. I am very confused by this. Does anyone have any insight? Thank you
Christian Schwabe (x)Christian Schwabe (x)
Hi Afzaal

Greetings to you and sorry that nobody responded earlier.

Related to the documentation "Using the runAs Method" (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_tools_runas.htm) my suggestion is to use a standardized User-Object in System.runAs(<User>) and set a timezone to ensure the tests run in the same environment and are not dependent on the current / executing user.

I hope my tip will help you to move further. If not, please respond with your result or if you have any other questions regarding my answer feel free to ask more questions.

P.S.: Remember to write classes in the first letter with capital letters. Just a small hint for better readability of Apex. :)

Best regards,
Christian