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
Prem ChauhanPrem Chauhan 

Urgent: How to cover CreatedDate != LastmodifiedDate in test class for Lead Object

My SOQL: [SELECT ID,Name, Status FROM Lead WHERE CreatedDateNotEqualToLastModified__c = TRUE AND LastModifiedDate >= LAST_N_DAYS:2];

Can anyone help with this URGENT?????
Biswojeet Ray 11Biswojeet Ray 11

Hi Prem,

 

Please try this

Lead leadRec = new Lead (Name = 'Test');
insert leadRec;

Datetime yesterday = Datetime.now().addDays(-1);
Test.setCreatedDate(leadRec.Id, yesterday);


Then update the record in test class itself. Then Its created date will be different from the last modified date.

 

I  hope it helps you.

Kindly let me know if it helps you and please mark as Best Answer.

Thanks and Regards,
Biswojeet,

Prem ChauhanPrem Chauhan
Thanks but its not working
GauravendraGauravendra
Hi Prem,

Can you check if your test method is using SeeAllData = true. You also can’t use setCreatedDate in methods annotated with @isTest(SeeAllData=true), because those methods have access to all data in your org. 

Hope thats helps.
Biswojeet Ray 11Biswojeet Ray 11
Yes. You should remove that "@isTest(SeeAllData=true)"
Prem ChauhanPrem Chauhan
I haven't used this @isTest(SeeAllData=true) but still, the Coverage is 11%  
if I remove the below code then it's showing 56% coverage.  Can  I modify lastModified date?
Datetime yesterday = Datetime.now().addDays(-1);

Test.setCreatedDate(leadRec.Id, yesterday);



 
Prem ChauhanPrem Chauhan
Biswojeet Hi,

I have used @isTest(SeeAllData=true) then my test class covered. Thanks
Biswojeet Ray 11Biswojeet Ray 11

Hello Prem 

You may have to add a check into your actual code using Test.isRunningTest() to see if the code is executing in test mode and if so, not rely on the lastModifiedDate field, rather on something else that you can control through in the test code.

 

thanks