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);

   //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?


You can execute a SOQL query through the following endpoint


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

  • The names retrieved via a SOQL query are not the name in question. I need the developer name ("FullName") of each available profile. I am trying to retrieve the metadata for each profile and for that I have to provide the "FullName" @KrisGoncalves
    – yassin0
    Jul 2 '21 at 14:35
  • So that's true for standard profiles - but, it's static as noted here. Custom profiles, the SOQL name will match FullName. Jul 2 '21 at 14:37
  • So the best approach in this situation would be to query the "FullNames" of custom profiles via SOQL and hardcode the "FullNames" of standard profiles into my code since they are static? @KrisGoncalves
    – yassin0
    Jul 2 '21 at 14:47
  • "best" is up to you as it depends on your goals with what you're doing with the data and # of profiles. Either way, it'd be more performant than separate callouts for each profile if you use either of the options I listed (standard mapping and one SOQL callout or using compositeRequest). Jul 2 '21 at 14:54
  • Alright, thank you for the insides Kris!
    – yassin0
    Jul 2 '21 at 15:03

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.