function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
VenkataRajaVenkataRaja 

Avoid to get history object(ex: abc__c_hd) when getting custom object list.

Hi ,

I am getting all custom object list from the orgainization, but I am getting historical data objects. I don't want those objects in my list.
Can you please help any one.

List<Schema.SObjectType> allObjectsList = Schema.getGlobalDescribe().values();
Set<String> customObjectsList = new Set<String>();
                                            
        for(Schema.SObjectType objType : listOfAllObjects)
        {           
            Schema.DescribeSObjectResult objectResult = objType.getDescribe();
            if(objectResult.isCustom() && !objectResult.isCustomSetting())
            {               
                customObjectsList.add(objectResult.getName());                                                                                           
            }
        }

In customObjectsList I am getting 'Historical Data' Objects like abc__c_hd. I don't want '_hd' objects.

Thanks 
Venkat
Best Answer chosen by VenkataRaja
Gonzalo AbrunaGonzalo Abruna
Hello Venkata,

those objects do exist behind the scenes, but you cannot see them in Create/Objects path (if you are using eclipse to develop, you will be able to see them in the salesforce.schema of your org).

Anyways, if the issue is that you are getting __History objects, you can use the endsWith method and do something like the following:

for(Schema.SObjectType objType : listOfAllObjects)
        {           
            Schema.DescribeSObjectResult objectResult = objType.getDescribe();
            if(objectResult.isCustom() && !objectResult.isCustomSetting() && !objectResult.getName().endsWith('History'))
            {               
                customObjectsList.add(objectResult.getName));                                                                                           
            }
        }

All Answers

KMForceKMForce
I tried the same but I am not getting any history objects. And it seems that allObjectsList and listOfAllObjects are different things.
Regards,
Kiran Machhewar
Gonzalo AbrunaGonzalo Abruna
Maybe you can check if the API Name of the object ends with __c (using String.endsWith()) method. History table usually ends with __History.

Best regards,

Gonzalo.
VenkataRajaVenkataRaja
Hi Kiran, those variables are not different, that is typo mistake. 
Hi Gonzalo, Yes we can use that, but How we can avoid Historical Data objects using standard methods, I am not able to see those objects directly in salesforce(Create->objects path). In background I am getting these objects.

Thanks
Venkata
Gonzalo AbrunaGonzalo Abruna
Hello Venkata,

those objects do exist behind the scenes, but you cannot see them in Create/Objects path (if you are using eclipse to develop, you will be able to see them in the salesforce.schema of your org).

Anyways, if the issue is that you are getting __History objects, you can use the endsWith method and do something like the following:

for(Schema.SObjectType objType : listOfAllObjects)
        {           
            Schema.DescribeSObjectResult objectResult = objType.getDescribe();
            if(objectResult.isCustom() && !objectResult.isCustomSetting() && !objectResult.getName().endsWith('History'))
            {               
                customObjectsList.add(objectResult.getName));                                                                                           
            }
        }

This was selected as the best answer