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
test vijaytest vijay 

Apex CPU time limit exceeded (Test.loadData())

hi Guys,
I am facing this error - 
System.SObjectException: common.apex.runtime.impl.ExecutionException: Apex CPU time limit exceeded
Source - Class.System.Test.loadData: line 53, column 1
Class.CaseTeamMemberUnitControllerTest.testSetup: line 9, column 1
below is my testClass
@testSetup static void testSetup() {
    Test.loadData(Account.sobjectType, 'records_accounts_with_cases_2021');
    Test.loadData(Contact.sobjectType, 'records_contacts_with_cases_2021');
    Test.loadData(Case.sobjectType, 'records_cases_2021');
    
  }


error is generating from this line Test.loadData(Case.sobjectType, 'records_cases_2021') which is added on line 9 at test class.
and In this records_cases_2021 file , I have only 50 records that i'm loading via static resource.
I have tried many ways and have searched on google but i'm not satisfied those answer.
can someone please show me a good way.
It's very argent.
thanks in advance
 
Best Answer chosen by test vijay
ravi soniravi soni

Hello Vijay,
I have faced this issue and I had tried following ways. I suggest you once you must follow these steps.

Step 1 - Test.startTest(); 
Test.loadData(Case.sobjectType, 'records_cases_2021'); 
Test.stopTest();

OR

Step - 2 -
1.)  
clone this records_cases_2021 file and put 1 or 2 records in this file.
2.) create new static resource and select records_cases_2021 this file 
3.) and put this file name in your test.loadData.
For Example - records_cases_2021_Copy =>  this is your clone file and it has only 2 records in this file.
paste it in your test class.
Test.startTest(); 
Test.loadData(Case.sobjectType, 'records_cases_2021_Copy '); 
Test.stopTest();

because it may be when your file load , that time trigger or process builder or something fired. I think that's why you are getting this error.

let me know if it helps you and marking it as best answer so that it can help to others in future.
Thank you
 

All Answers

PriyaPriya (Salesforce Developers) 
Hi Vijay,
 

Check below example that matches your requirement :- 

 

https://developer.salesforce.com/forums/?id=906F0000000DEUyIAO
 

Please mark as Best Answer so that it can help others in the future.

Regards,

Priya Ranjan


 
ravi soniravi soni

Hello Vijay,
I have faced this issue and I had tried following ways. I suggest you once you must follow these steps.

Step 1 - Test.startTest(); 
Test.loadData(Case.sobjectType, 'records_cases_2021'); 
Test.stopTest();

OR

Step - 2 -
1.)  
clone this records_cases_2021 file and put 1 or 2 records in this file.
2.) create new static resource and select records_cases_2021 this file 
3.) and put this file name in your test.loadData.
For Example - records_cases_2021_Copy =>  this is your clone file and it has only 2 records in this file.
paste it in your test class.
Test.startTest(); 
Test.loadData(Case.sobjectType, 'records_cases_2021_Copy '); 
Test.stopTest();

because it may be when your file load , that time trigger or process builder or something fired. I think that's why you are getting this error.

let me know if it helps you and marking it as best answer so that it can help to others in future.
Thank you
 

This was selected as the best answer
test vijaytest vijay
Thanks Veer, It's working.