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
Harsha SekharHarsha Sekhar 

Test classes on Last Login Date field on User Object

I am writing a scheduled class for sending emails for user who have not logged in to salesforce for 30 days, Code is working fine but now I am confused how to write test classes for that, Because all of my conditions are based on Last Login Date which is Un-Editable. Can I take data from a existing user? If i take in such a way will it effect when i move my code to other sandbox/Production? If so how to solve this.

!! Please Help !!

Thanks & Regards
Harsha.
Raj VakatiRaj Vakati
There is a simple way to do .. 

Create a CSV file to load the data for the test class along with  Last Login Date and run the test class .the Test.loadData method. 
 
List<sObject> ls = Test.loadData(User.sObjectType, 'myResource');

Refer this link

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_load_data.htm
Vishal_GuptaVishal_Gupta
Hi Harsha,

First I suggest you to try Time-Dependent Workflow, you can easily achieve your requirment from it and there is no need to write down the long batch and its test class. if you need any help with Time-Dependent Workflow please let me know.

And if you still want to go with Batch then I would suggest to use the existing user data, it will only cover your code if you will have any user in your system who does not login in system from last 30 days otherwise some code will not be covered in test method.

Please let me know if you need more input. Thanks.
 
Harsha SekharHarsha Sekhar
Hi Vishal,

I have tried doing the same with Time dependent Workflows but I think it is not feasible. But if you can explain how you achevied it through Time Dependent WorkFlows It would be helpful as well and Thanks for the reply

Hi Raj,
As Last Login field in userlogin object is not Allowed to be edited/inserted so I will be a getting a errror if i try to insert those Read-Only fields, Is there any other know work around!?
Thanks for reply.

Thanks 
Harsha
Yuri Caballero 1Yuri Caballero 1
Hey Harsha,

I am trying to do the same thing you did, send notification to users that have not logged in 30 days.  Would kindly share your code?
Priyesh Misquith 12Priyesh Misquith 12
@Raj Vakati

Could you tell me how did you create the users from the static.
when i try i am getting following error.
'Validation Errors While Saving Record(s)'
sample csv data or code might help.
tried same step as per link
Umesh PersonalUmesh Personal

Hi @Raj Vakati I also tried the same thing. I used a static resource with lastlogindate field populated. But somehow while im running the test, the debug is showing the lastlogindate as null.

Any help on this would be much appreciated!!