+ Start a Discussion
Rohit K SethiRohit K Sethi 

Create AccountHistory record in test class.

Hi All,

Can we create the AccountHistory Reocrd in test class if it is possible plz post how ?

Thanks.
Best Answer chosen by Rohit K Sethi
Dhriti Moulick 16Dhriti Moulick 16
Hi Rohit,
   Though this is not a right approach you can use Test.isRunningTest() for the coverage of the apex class.Please go through the following link:\
 https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_test.htm

Cheers,
Dhriti

All Answers

Dhriti Moulick 16Dhriti Moulick 16
Hi Rohit,
    You can not perform any DML regarding this AccountHistory,though this particular object is dependent on the Account object.Records in AccounyHistory object stored in SF internally,you can only query to fetch records from this object.

AccountHistory actHistory =[SELECT CaseId,CreatedById,CreatedDate,Field,Id,IsDeleted,NewValue,OldValue FROM CaseHistory];

Please let me know if you need any further help

Cheers,
Dhriti
Amit Chaudhary 8Amit Chaudhary 8
you can not create the AccountHistory  record in test class you need to use SeeAllData=true in this case
Vasani ParthVasani Parth
Rohit,

There is a great answer here (http://salesforce.stackexchange.com/questions/4007/is-it-possible-to-test-apex-that-relies-on-field-history-tracking) for your query 

Please mark this as the best answer if this helps
Rohit K SethiRohit K Sethi
Hi All,
Thanks for your quick response.
 

I know that we cannot create the Account History record . But my main class most of the code contains Account History record  related conditions.

​So I have to cover that area to achieve the 75%.

Thanks.
 
Dhriti Moulick 16Dhriti Moulick 16
Hi Rohit,
   Though this is not a right approach you can use Test.isRunningTest() for the coverage of the apex class.Please go through the following link:\
 https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_test.htm

Cheers,
Dhriti
This was selected as the best answer