2

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..

  • Interesting that SObjectType.Account compiles and returns the same data that the documented Account.SObjectType.getDescribe() returns. – Keith C Jun 29 '15 at 20:48
4

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

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

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())
  • string == is already case insensitive, so don't waste time there, and there's more efficient ways of doing this. – sfdcfox Jun 29 '15 at 20:25
  • 1
    String typing? I try to avoid it when I can. – Adrian Larson Jun 29 '15 at 20:27
  • Thnx guys. Am always learning from what you and others post! :) – crmprogdev Jun 29 '15 at 20:40

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.