Retrieve API name of all profiles

How do I retrieve the API name of all profiles that exist in a SF organisation?

I can retrieve the API name of individual profiles via HTTP request:

public static String getProfileAPIName(Id profileId) {
    //set httpRequest web call
    HttpRequest req = new HttpRequest();
    req.setHeader('Authorization', 'Bearer ' + Utils.getSessionIdFromVFPage(Page.SessionId));
    req.setHeader('Content-Type', 'application/json');
    String domainURL = URL.getSalesforceBaseUrl().toExternalForm();
    req.setEndpoint(domainURL + '/services/data/v51.0/tooling/sobjects/Profile/' + profileId);
    req.setMethod('GET');

   //webcall profile metadata
    Http h = new Http();
    HttpResponse res = h.send(req);
    ProfileDescription pd = ProfileDescription.parse(res.getBody());
    return pd.FullName;     
}

With this approach I can only retrieve one API name at a time. If a organization has hundred of profiles this would hit callout limits pretty fast. Is there any other option to retrieve all available profile API names on the spot?

Answers 1

  • You can execute a SOQL query through the following endpoint

    /services/data/v52.0/query/?q=select+ID,Name+from+Profile

    The return will be like so:

    {
      "totalSize" : 41,
      "done" : true,
      "records" : [ {
        "attributes" : {
          "type" : "Profile",
          "url" : "/services/data/v51.0/sobjects/Profile/00e4P000000yv0hQAA"
        },
        "Id" : "00e4P000000yv0hQAA",
        "Name" : "External Apps Login User"
      }, {
        "attributes" : {
          "type" : "Profile",
          "url" : "/services/data/v51.0/sobjects/Profile/00e4P000000yv37QAA"
        },
        "Id" : "00e4P000000yv37QAA",
        "Name" : "Guest License User"
      }, etc...
    

    That should work well with custom profiles. For Standard profiles, that won't give the FullName/Api Name - but, there's a mapping created in this question that would help if you stored as a custom metadata type.


    Another option is just using what you currently have, but reduce your callouts by leveraging the composite resources that allows you to execute a series of requests in a single call (You can have up to 25 subrequests in a single call).

    {
    "compositeRequest" : [{
      "method" : "GET",
      "url" : "/services/data/v51.0/tooling/sobjects/Profile/firstId",
      "referenceId" : "firstProfile",
      },{
      "method" : "GET",
      "url" : "/services/data/v51.0/tooling/sobjects/Profile/secondId",
      "referenceId" : "secondProfile"
      }, etc etc
    
      }]
    }
    

Related Questions