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
Dipa87Dipa87 

How to get login hours of a particular profile using apex?

Is there a way to retrieve the login hours for a profile using apex?

 

I want start and end time for a particular user.

 

Thanks in advance!

AdrianCCAdrianCC

Hello,

 

I don't think there's currently a way to get the end time. Not from LoginHistory object anyway. LoginHistory contains only the start time. link

 

Also, what happens when the user connects from multiple sources, like a dev who uses at the same time force.com ide, chatter and the browser. Would that time be cumulative?

 

Regards,

Adi

Dipa87Dipa87

Yes Adrian , But can we retrieve start and end time from the profile.

In profile we have something called login hours. Can we get the login hours from profile using apex.

AdrianCCAdrianCC

The LoginHistory object supports query() so you can retrieve records from it using a soql in your apex code.

For example:

List<LoginHistory> logHistList = [SELECT UserId, LoginTime FROM LoginHistory WHERE UserId='youruserid'];

 

The problem is the object only has the start time(LoginTime) . There's no end time, no way to see when has the user logged off.

Dipa87Dipa87

I have got two fields named StartDay and EndDay in User object.

 

but not able to query those fields.

 

Can anyone tell me how to query StartDay and EndDay fields from user object.

AdrianCCAdrianCC
User someUser = [SELECT Id, StartDate, EndDate FROM User WHERE Id='xxxxxxxxxxxxx' LIMIT 1];

if (someUser != null) {
    Datetime start = someUser.StartDate;
    Datetime end = someUser.EndDate;
}

Question: How do you populate StartDate & EndDate in the first place?

Dipa87Dipa87

We cannot  query StartDate and EndDate from User object.

 

I have found out an alternative solution.

 

Created two formula text fields(Start_Time__c,End_Time__c) in User object.

 

which will store the StartDate and EndDate field values like dis :    text(Startdate)

 

Its working fine.

Now I  am able to retrieve the value in the VF page like dis  {!$user.Start_Time__c}