2

I want to check whether a given object is standard or custom. At present I have a method which identifies custom objects but that is taking too much CPU time, which leads to salesforce exception.

public static Boolean getAllCustomSObjects(String objectname)
{Boolean customobject;
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);
        } */
        if(name.contains(objectname))
        {customobject=true;
        System.debug('Custom object found');
        }
        else
        {
         customobject=false;
         System.debug('Custom object not found');
        }
    }
}
system.debug('SObjectList****' + SObjectList);
return customobject;
}

Instead of fetching all the custom object names and checking whether the the given object name is a custom object, I want to check the object directly. I tried to modify the line

Schema.DescribeSObjectResult describeSObjectResultObj = objTyp.getDescribe(); 

and to give the object name as parameter for DescribeSObjectResult but it was throwing error.Do anyone help in this?

3 Answers 3

1

You don't need a for loop here. You can do something like

public static Boolean checkObjectType(String objectname) {
    SObjectType objToken = Schema.getGlobalDescribe().get(ObjectNameval);
    if(objToken  != null && objToken.getDescribe().isCustom())
        return true;
    return false;
}

This method will return true if it is custom object or false if it is not. Also one major difference is All custom objects end with __c with Standard object don't have these suffix.

3
  • Your code is returning false all time.
    – Aruna
    Jan 3, 2018 at 9:56
  • @Aruna: try using return false; inside else like else return false;. It shouldn't make any difference with the existing code but just for the safe side.
    – Mahmood
    Jan 3, 2018 at 10:19
  • I tried that too, not working
    – Aruna
    Jan 3, 2018 at 10:21
0

you can use SObjectType class methods to find out. method mentioned by @Tushar gives me TRUE if it is custom object.

public static Boolean checkObjectType(String objectname) {
   SObjectType objToken = Schema.getGlobalDescribe().get(objectname);
   if(objToken  != null && objToken.getDescribe().isCustom()) {
     return true;
   } else {
     return false;
   }
}

pass the objectname as a String and it will return TRUE if is a custom object.

And the easiest way is to use check the suffix '__c' at the end of name. use String class method String s = 'Custom_object__c'; System.assert(s.endsWith('__c'));

String Class

SObjectType Class

5
  • May be you are checking wrong. Plz try with both standard and custom objects.
    – Aruna
    Jan 3, 2018 at 10:19
  • 1
    for me its working. SObjectType objToken = Schema.getGlobalDescribe().get('Account'); System.debug('OBJ ' + objToken.getDescribe().isCustom()); is returning FALSE and SObjectType objToken = Schema.getGlobalDescribe().get('Custom_Object__c'); System.debug('OBJ ' + objToken.getDescribe().isCustom()); is returning TRUE Jan 3, 2018 at 10:23
  • if still not working use string method and check '__c' at the end. Jan 3, 2018 at 10:25
  • When used in SOQL both standard and custom objects will get the suffix '__r'. ex: CarSet__r.CarSetDate__c, CarSet__r.Order__r.EffectiveDate. When I try to give the objects inside the code you gave its returning false all time.
    – Aruna
    Jan 3, 2018 at 10:32
  • For standard objects I want to remove the suffix '__r' and for custom objects I want to replace the suffix '__r' to '__c'. I hope you'll understand my point now
    – Aruna
    Jan 3, 2018 at 10:34
0

@Tushar, Thanks your code was working. Since I was trying to find the custom objects from SOQL a suffix'__r' will be appended to all the objects with relationships.That's why first I got error but then I did small change to the code. ie.I replaced the suffix of objects from '__r' to '__c'. After doing this the code worked fine.

public static String checkObjectType(String objectname) {
String obj=objectname.replace('__r','__c');
SObjectType objToken = Schema.getGlobalDescribe().get(obj);
if(objToken  != null && objToken.getDescribe().isCustom()) {
 return 'true';
} else {
 return 'false';
}
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.