3

I have the following code :

Schema.SObjectType objectType = sObjects.get(0).getSObjectType();

I'm trying to write an IF statement to check if the result of objectType is specific object such as Account.

I have written something like this :

if(objectType == Schema.SObjectType.Account)

But this does not work with the Following Error :

Comparison arguments must be compatible types: Schema.SObjectType, Schema.DescribeSObjectResult

So My question is what Comparison argument should i use to validate 'Account' object results? Thanks for any quick help..

1
  • Interesting that SObjectType.Account compiles and returns the same data that the documented Account.SObjectType.getDescribe() returns.
    – Keith C
    Commented Jun 29, 2015 at 20:48

3 Answers 3

6

To get sObjectType of Account, comparison statement in above code should be changed to if(objectType == Schema.Account.sObjectType)

3
  • 3
    Schema is optional, unless there's a class named Account.
    – sfdcfox
    Commented Jun 29, 2015 at 20:26
  • 1
    And you'd probably need to be smoking something pretty strong to name your class 'Account' Commented Jun 29, 2015 at 20:29
  • Yeah naming a class that would be crazy. But not a variable.
    – Adrian Larson
    Commented Jun 29, 2015 at 20:32
3

I go even more brief:

if (objectType == Account.sObjectType)

Only downside is it won't work if you have a variable named Account.

0

Assuming a is an sObject, for Account, you could do something like this:

if('account` == String.valueOf(a.getSobjectType()).toLowerCase())
3
  • string == is already case insensitive, so don't waste time there, and there's more efficient ways of doing this.
    – sfdcfox
    Commented Jun 29, 2015 at 20:25
  • 1
    String typing? I try to avoid it when I can.
    – Adrian Larson
    Commented Jun 29, 2015 at 20:27
  • 1
    Thnx guys. Am always learning from what you and others post! :)
    – crmprogdev
    Commented Jun 29, 2015 at 20:40

You must log in to answer this question.

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