4

I am trying to get all Standard and Custom Objects name from Schema class in apex class. I need to remove all shared objects from the list.

  • 1
    I have used Schema.getDescribe() with few common "is" methods like isqueryable, isupdatable. Removed many but still few of them are coming for which I don't have access or isn't in use. – Anant Agarwal Mar 18 '15 at 8:25
11

Here is updated code that will give list of all objects excluding Share, history , Tag & feed. You can add extra condition to filter out other unnecessary sObjects.

List<string> SObjectList = new List<string>();

for(Schema.SObjectType objTyp : Schema.getGlobalDescribe().Values()){
   String name = objTyp.getDescribe().getName();
   // Exclude all the unwanted Sobjects e.g. History, Share etc..

 if((!name.containsignorecase('history') && !name.containsignorecase('tag')&&
    !name.containsignorecase('share') && !name.containsignorecase('feed')) || 
     name.toLowerCase().right(3) ==  '__c'){      
      SobjectList.add(name);
      System.debug( 'Name : ' + name);
  }

}
  • Nope, this isn't working. Template, Rule, Role everything is coming. – Anant Agarwal Mar 18 '15 at 8:23
  • Thats why i said "You can add extra condition to filter out other unnecessary sObjects" – AtulRajguru9 Mar 18 '15 at 9:05
  • I m looking for those extra conditions only ;) . Excluding name manually wouldn't help. – Anant Agarwal Mar 18 '15 at 9:09
  • Check qs' comment for the things i have tried. – Anant Agarwal Mar 18 '15 at 9:10
  • 1
    @SimonLawrence Good point, to handle that we can add OR condition something like this || name.toLowerCase.right(3) == '__c' – AtulRajguru9 Oct 18 '19 at 4:55
1

Here is my way, it works fine. Please test it.

global static List<String> getAllCustomSObjects()
{
    List<String> sObjectList = new List<String>();
    for(Schema.SObjectType objTyp : Schema.getGlobalDescribe().Values())
    {
        Schema.DescribeSObjectResult describeSObjectResultObj = objTyp.getDescribe();
        if(describeSObjectResultObj.isCustom())
        {
            String name = objTyp.getDescribe().getName();
            // Exclude all the unwanted Sobjects e.g. History, Share etc..
            if(!name.containsignorecase('history') && !name.containsignorecase('tag')&&
             !name.containsignorecase('share') && !name.containsignorecase('feed'))
            {
                SobjectList.add(name);
            }
        }
    }
    system.debug('SObjectList****' + SObjectList);
    return sObjectList;
}
  • For standard object, rule, role are also standard object – BruceYue Apr 23 '15 at 1:50
  • 1
    Yeah I get that. Already added couple of more conditions on describeSObjectResultObj . Anyways thanks :) – Anant Agarwal Apr 23 '15 at 7:46
-1

Unfortunately there's not a great way to exclude all objects like share, tag, etc. from the Schema.getGlobalDescribe() results apart from using the String class .contains() method as shown above. This still leaves a lot of unwanted objects in the list though, as not all of them have a set naming convention that can be detected and filtered.

Although not dynamic, depending on your use case you may try something like the following:

    private Set<String> desiredStandardObjects = new Set<String>{'Account','User',...};

    List<String> objectList = new List<String>();
    for(Schema.SObjectType objTyp : Schema.getGlobalDescribe().Values()){
        Schema.DescribeSObjectResult obj = objTyp.getDescribe();
        if((obj.isCustom() && !obj.getName.contains('ChangeEvent')) || desiredStandardObjects.contains(obj.getName()){
                objectList.add(name);
            
        }
    }

This way you filter out most of the unwanted objects and keep all the custom objects and wanted standard objects.

There are still some "system" objects that are technically custom objects. For example every custom object has a ChangeEvent object that is autogenerated by Salesforce. You can filter out by the String 'ChangeEvent' in the API name to help with that.

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.