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
8

Here is 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 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

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.