14

Can anyone help me understand about Apex Describe Information? In plain English please. Thanks.

The describeSObjects method — a method in the Schema class that performs describes on one or more sObject types.

Describe result — an object of type Schema.DescribeSObjectResult that contains all the describe properties for the sObject or field. Describe result objects are not serializable, and are validated at runtime. This result object is returned when performing the describe, using either the sObject token or the describeSObjects method.

Token — a lightweight, serializable reference to an sObject or a field that is validated at compile time. This is used for token describes.

20

A token is a "placeholder" of the object or field it points to. It consumes very little resources, as compared to a describe, because it doesn't actually contain any information about the field or object, such as its label, data type, maximum length, etc. A describe can contain hundreds or even thousands of bytes of data, but a token is significantly smaller (less than 20 bytes).

To consider the savings, if you have 500 custom fields, 500 tokens might be 10,000 bytes of memory, while a full describe for all 500 fields would probably use closer to 500,000 bytes of memory.

As a bonus, you can also use tokens in place of strings when using the generic SObject methods get and set, which reduces the possibility of the system throwing an exception because of a typo; the fields are statically checked at compile-time.

As an example of the prior paragraph, consider this code:

Account a = new Account();
a.put('Nmae', 'Test'); // Run-time error
a.put(Account.Nmae, 'Test'); // Compile-time error

The first error might be harder to track down because it won't show up until you're testing, while the second one will surface immediately because of the compile-time checking.

Schema.DescribeSObjects() is a function that returns full object-level details for one or more SObjects, such as the type of name field (text or auto-number), its singular and plural labels, and accessibility information. You can use this information to render strings dynamically, provide branch logic depending on the accessibility of the object for the current user, and more.

The return value (the result) of the function is one or more DescribeSObjectResult objects that actually contains the describe information from DescribeSObjects or Sobject.getDescribe().

There are two types of describes: DescribeSobjectResult and DescribeFieldResult. As they are named; the former contains data related to an entire SObject, such as its label and child relationships, while the latter contains information such as the formula used for default values, or picklist values).

  • Can you be enough kind to let us know from where do you get this information by which you can tell which method will take how much memory? – Superman Oct 26 '16 at 6:35
  • 2
    @Superman There's no way to know exactly how much memory a particular operation takes, but you can usually approximate it with something like Integer beforeUse = 0, afterUse = 0; beforeUse = Limits.getHeapSize(); FieldDescribeResult result = Account.Industry.getDescribe(); afterUse = Limits.getHeapSize(); System.debug(afterUse-beforeUse); Generally speaking, picklists use the most memory, because all of their picklist values are available. Other field types use varying amounts of memory based on their attributes. There's no documentation that really explains this. – sfdcfox Oct 26 '16 at 14:18
  • Actually its been a while that I'm focusing on your comments and answers here on stackExchange and really impressed. It would be great help if you can tell me the best resources to be successful in apex and especially in the dynamic nature of it that recently got so much attention and eventually it is becoming a need. Your words will help many others who will read this. Thanks. – Superman Oct 26 '16 at 17:33
15

Simply put, a token is the API name for your object (whether standard or custom). The describeSObjects is a method which returns a DescribeSObjectResult - this contains the information returned on calling the describe method on an object or a field. The DescribeSObjectResult therefore can hold objects as well as fields. The best way to understand this is to play with some examples.

If you do

Schema.sObjectType t = Account.sObjectType; //it returns the token 'Account' (which is the API name for standard object Account)
Schema.DescribeSObjectResult D = Account.sObjectType.getDescribe(); //it returns the describe result for the standard object 'Account'

This post and the official Salesforce documentation actually explains all three concepts pretty well with a real world example and some sample code. Check it out

  • All stackexchange sites rather try to build and contain relevant information and answers, and not just link to it like a search engine. Would you mind at least explaining the terms cartman has asked in your answer ? – Samuel De Rycke Dec 26 '13 at 10:15
  • Sdry - I have updated my answer to include explanations for the terms as asked in the original question. I had included the links primarily for reference just in case the submitter of question wasn't aware of these. None the less, I'll ensure that my solutions always provide meaningful info in addition to any external links. Thanks! – Gaurav Kheterpal Dec 26 '13 at 10:40
  • And I have upvoted it :-) – Samuel De Rycke Dec 26 '13 at 10:42
  • 3
    Your description of a token is misleading, because they are not just "the API name" of the entity. They provide compile-time safety, easy access to describe information via getDescribe() without having to use a describeglobal or describeSObjects function call, and can be used instead of actual strings in dynamic code to reduce run-time errors because of misspellings, and also allow the system to link those objects and fields to the class' metadata, all without consuming massive resources that full describes cause. – sfdcfox Dec 26 '13 at 17:12
  • 3
    Also, there's a DescribeFieldResult and DescribeSObjectResult. They are not interchangeable, have different functions to expose different types of information (e.g. DescribeSObjectResult doesn't have getPicklistValues(), because an object isn't a picklist, and DescribeFieldResult doesn't include getChildRelationships). – sfdcfox Dec 26 '13 at 17:14

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.