You need to sign in to do that
Don't have an account?
Gaurav Khare 16
OpportunityHistory Data in Test Class
Hi , My entire application works on Opportunity history data (all batches) , There are cases like opportunites close dates changed in current month or previous month and much like this, evaluating previous one year data. I can create oppoturinity but I need its history in previous periods as well for traversing various of my If and else conditions, This seems next to impossible, I cannot use seeAllData=true, as it is a package and when installed on other system may fail. What can be the hack here?
Any help would be appreciated!!!
Any help would be appreciated!!!
Hi Gaurav,
HIstory records are not created in testclasses.(https://success.salesforce.com/ideaView?id=08730000000h6RwAAI)
you need cover that part by creating a fake history data records in your main class.
You can take idea from this below post.
http://salesforce.stackexchange.com/questions/4007/is-it-possible-to-test-apex-that-relies-on-field-history-tracking
Hope this helps.
You need to create fakedata in your main Class. You cannot create history records in testclass .
The approach of creating testdata for history records will be the same.
if (Test.isRunningTest()) {
lstOpportunityHistory.add(new OpportunityHistory(...));
lstOpportunityHistory.add(new OpportunityHistory(...));
}
Gaurav,
You don't need to create any custom object and perform any DML.
You just need to fill the list(data) of OpportunityHistory
You can also set created date shown in below post.
http://salesforce.stackexchange.com/questions/130475/how-to-create-test-data-for-a-class-that-checks-for-historical-data
Try with not assigning the opportunityId , If you are using OpportunityHistory.opportunityId reference in your main Class or if there is block checking the opportunity , then try mimicking the standard object with the modal/wrapper class. You can use below as an example to do this.
http://salesforce.stackexchange.com/questions/115664/test-class-error-compile-error-field-is-not-writeable-opportunity-connectio
Hi Gaurav,
OpportunityHistory object is a read only object . Only change you can do is try with mimicking with a wrapper/modal class.
Hope this helps.
Opportunity opportunity_Obj = new Opportunity(IsPrivate = false, Name = 'Name144',Account_Record_Type__c = 'Tenant', Property_Name_Text__c = 'ogcp', StageName = 'Exchanging Proposals', CloseDate = Date.today(), Budget_Confirmed__c = false, Discovery_Completed__c = false, ROI_Analysis_Completed__c = false, Additional_Approval_Required__c = false, Deal_Submitted_for_Approval__c = false, Deal_Type__c = 'New Lease', Leasing_Property_Lead__c = user_id, Property__c = propertyObj_Id, Has_Options__c = false, Highest_Active_Deal__c = false);
Insert opportunity_Obj;
opportunity opp=[select id,StageName from Opportunity where id=:opportunity_Obj.Id];
opp.StageName='Fully Executed Lease';
update opp;
Test.startTest();
OpportunityFieldHistory coh = new OpportunityFieldHistory(OpportunityId = opp.id, Field = 'StageName');
insert coh;
String Query='SELECT id,OpportunityId,CreatedDate,oldValue,NewValue,Field from OpportunityFieldHistory';
ESRT_SendNotificationsOrEmailsBatch obj=new ESRT_SendNotificationsOrEmailsBatch(Query);
DataBase.executeBatch(obj,100);
Test.stopTest();