3

How can I check the login hours of a user profile that configured in user's profile?

I have looked into LoginHours but could not find.

In which object/soql/apex/rest/tooling api it would be available to use in?

3

You can get this information via the Tooling API if you query it for Profile.

Sample query:

/services/data/v41.0/tooling/query/?q=SELECT+Metadata+FROM+Profile+LIMIT+1

Sample structure:

{
    "size": 1,
    "totalSize": 1,
    "done": true,
    "queryLocator": null,
    "entityTypeName": "Profile",
    "records": [{
        "attributes": {
            "type": "Profile",
            "url": "/services/data/v41.0/tooling/sobjects/Profile/00e..."
        },
        "Metadata": {
            "applicationVisibilities": [],
            "categoryGroupVisibilities": null,
            "classAccesses": [],
            "custom": true,
            "customPermissions": [],
            "description": null,
            "externalDataSourceAccesses": [],
            "fieldPermissions": [],
            "loginHours": null,
            ^^^^^^^^^^^^ the key you are looking for
            "loginIpRanges": [],
            ...
        }
    }]
}

So a simple structure will allow you to deserialize the results:

class QueryResult
{
    List<ProfileResult> records;
}
class ProfileResult
{
    ProfileMetadata metadata;
}
class ProfileMetadata
{
    LoginHours loginHours;
}
class LoginHours
{
    Integer sundayStart, sundayEnd;
    Integer mondayStart, mondayEnd;
    Integer tuesdayStart, tuesdayEnd;
    Integer wednesdayStart, wednesdayEnd;
    Integer thursdayStart, thursdayEnd;
    Integer fridayStart, fridayEnd;
    Integer saturdayStart, saturdayEnd;
}

And here's a sample anonymous script which makes use of that structure:

String auth = 'Bearer ' + UserInfo.getSessionId();
String query = 'SELECT Metadata FROM Profile LIMIT 1';
String endpoint = URL.getSalesforceBaseUrl().toExternalForm() +
    '/services/data/v41.0/tooling/query/?q=' +
    EncodingUtil.urlEncode(query, 'UTF-8');

HttpRequest request = new HttpRequest();
request.setHeader('Authorization', auth);
request.setEndpoint(endpoint);
request.setMethod('GET');

HttpResponse response = new Http().send(request);
QueryResult result = (QueryResult)JSON.deserialize(
    response.getBody(), QueryResult.class
);
ProfileResult record = result.records[0];
system.debug(record.Metadata.loginHours);
system.debug(record.Metadata.loginHours.mondayStart);
system.debug(record.Metadata.loginHours.mondayEnd);
  • Thank you Adrian, you have provided more than what I asked for. I really appreciate your help. – Nick Kahn May 1 '18 at 20:54
3

You can access the Profile LoginHours via the Metadata API or the Tooling API.

E.g. with the Tooling API under the Metadata field.

GET /services/data/v41.0/tooling/sobjects/Profile/00e0g0000010DMr

enter image description here

The ProfileLoginHours format is described in the Metadata API.

Valid values for Start: the number of minutes since midnight. Must be evenly divisible by 60 (full hours). For example, 300 is 5:00 a.m.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.