2

What I'm trying to do is list out all objects and then go through each field per object to see if that field contains a certain value fieldName.contains('aov'). (AKA trying to see where my field resides on which objects)

When I try to build out my results I keep getting an exception:

System.InvalidParameterValueException: Invalid sobject provided. The Schema.describeSObject() methods does not support the Getting_Started__kav sobject as a parameter. If you are attempting to use a Knowledge Article Type, be sure to use the abstract KnowledgeArticleVersion instead.

If someone could help me loop through to skip the object that is throwing the error that would be great!! Currently I'm running my APEX, waiting for the exception to be thrown, then adding that object to my list to skip. There has to be a better way but I'm too new to know. Here's my script, any advice or suggestions would be awesome.

String[] objectNames = new String[]{};
for ( Schema.SObjectType typ : Schema.getGlobalDescribe().values() ) {
    String sobjName = String.valueOf(typ);
    if ( !sobjName.contains('Knowledge_Article__kav') &&
         !sobjName.contains('Documentation__kav') &&
         !sobjName.contains('Internal_Procedures__kav') && 
         !sobjName.contains('Quick_Starts__kav') ) {
        objectNames.add(sobjName);
        //System.debug(sobjName);
    }
}

Schema.DescribeSobjectResult[] results = Schema.describeSObjects(objectNames);
for (Schema.DescribeSObjectResult res : results){
    //System.debug(res.getLabel());    
    for(String fieldName : res.fields.getMap().keySet()){
        if (fieldName.contains('aov') || fieldName.contains('AOV')) {
            System.debug(res.getLabel() + ' -- ' + fieldName);
        }
    }
}

A second question to follow up on this (not sure if I should create a 2nd post or not), is this script to labor intensive and is there a cleaner way to write it? Will I run into any limit issues with this?

1 Answer 1

3

Yes, describing every field on every object is certainly labor intensive. In my org it takes ~6s. Obviously mileage will vary.

Considering that you just want the String for each sObjectType and not its token, loop through keySet() instead of values().

List<String> objectNames = new List<String>();
for (String sObjectType : Schema.getGlobalDescribe().keySet())
{
    if (!sObjectType.endsWith('__kav')) objectNames.add(sObjectType);
}

A few other minor improvement you can make are to use the containsIgnoreCase method, cache the label, and not cache the describes.

for (Schema.DescribeSObjectResult res : Schema.describeSObjects(objectNames))
{
    String label = res.getLabel();
    for (String fieldName : res.fields.getMap().keySet())
        if (fieldName.containsIgnoreCase('aov'))
            system.debug(label + ' - ' + fieldName);
}
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .