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?

0

2 Answers 2

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);
4
  • Thank you Adrian, you have provided more than what I asked for. I really appreciate your help.
    – Nick
    May 1, 2018 at 20:54
  • Hi Adrian , Thanks for the retrieval feature above, my requirement is to set login hours programatically, do you have any suggestion for that ?
    – Quddus
    Apr 13, 2021 at 13:55
  • @Quddus that is a notably different and non-trivial question. Please pose it as a Question, not a Comment.
    – Adrian Larson
    Apr 13, 2021 at 14:50
  • 1
    @AdrianLarson : I did that already thanks - salesforce.stackexchange.com/questions/340318/…
    – Quddus
    Apr 13, 2021 at 14:52
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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .