CRT-450 Salesforce Certified Platform Developer – Retired Lectures
- 3.1- Object Schema – part 1
Hey guys. This is section three, logic and Process Automation. And this is lecture number one, Object schema. And this lecture will talk about how we can access our own metadata information from within Apex, how we can access objects and fields metadata information from within Apex. Note I advise you to watch this lecture after fully understanding Apex. But for now, let’s get started. First of all, what’s object schema? Object schema is a way to programmatically learn about the metadata of your data model within Apex. So within coding inside coding inside Apex, you will learn about the different objects metadata, the different fields of these objects and so on. And to do so, we can use two different namespaces. First of all, we can use the schema class methods and the system namespace. And also we can use many different classes and the schema namespace. Let me go to the Apex Developer Guide and let me show you these. So what I will do, I will go to the Apex Language Reference section and then I will go to the system namespace. And then you can notice there is a class called Schema class. So if I click on this class, I have a list of methods. These methods can be used to get metadata information about different objects, about different fields.
So this metadata information is something like what’s the object, what’s the API name, what’s the blur name and so on. What’s the field type, what’s the maximum length, and so on. So we can use these methods to get that. And also we can use the schema namespace. So if I click on this, I have different classes. So let’s click on one of these classes. I have also these methods. And then if I click on one of them, be it in the schema namespace or in the system namespace, I get the signature, the return value. I also get some examples. First of all, let’s talk about schema class methods and the system namespace. As we have seen in the PDF salesforce, Schema class contains many different static methods that are used to obtain schema describe information. Let me give you some examples. First of all, let’s talk about the schema that get a global describe method. If you invoke this, you will get a map of all your orgs s object names as the keys and the s object tokens as the values. We’ll know exactly what does a token mean, what’s a token? In the next slide, we can also invoke this method like schema that describe s objects. And then with an argument of s object types, this will return that describes metadata, the object properties for the specified s object or for the list of s object that is specified. Also we’ll know what does it describe mean in the next slide.
And also we can have a schema that describe tabs method. This will return information about the standard and the custom applications that are available to the running user. So let’s now know exactly what is a token and what is it described. Now, what’s a token? A token is simply a placeholder of the object or fields that it points to. A token is very small in size, so it’s less than 20 bytes. And it does not contain any information about the fields or the objects such as labels, such as data types and so on. It’s only a pointer that can be used to get more information. We’ll talk about two different tokens here. We’ll talk about S object tokens and about field tokens. An S object token has a data type of Schema S object type and a field token has the data type of Schema S object field. So if I go back to the previous slide, we have mentioned that invoking this method will return to me a map of S object names and S object tokens. So this will be the key and this will be the value. So in this case, the value will be what will be Schema S object type and the name will be the name of the S object. So in this case, if I invoke this, I will get a list of all my S object names and their corresponding tokens.
Now, what we can do with a token, let’s see that. To know what we can do with the token, let me explain what a describe is. A describe can contain hundreds or even thousands of bytes of data. It has all the details of the S object token. So it uses a token to return the information of this token. So it takes as input a token and then it will return the information of this token, the information of the fields, the information of the objects and so on. We will talk about two different describes. We will talk about the S object describe which were returned to me the list of all the details of an object using a token and the data type of this is what it is Schema that describe S object result. And we will also talk about a field describe which will return to me the details of a field using a token and the data type of this is Schema that describe feed result. So to understand, we have three things. First of all, we have the name of the S object, the name of the feeds and so on. In the middle we have the token that is corresponding to this S object or to this field. And then we have the describe.
The describe is the detail of the field or of the object that takes input the token objects. Let’s see that in detail. This is the method that we have talked about Schema that get global described. It will return to us a map of S object names as the key and their corresponding token as the value. And we have seen that the S object token, data type is what it is schema that s object type. So this will be the map, it will be key, string and value schema that s object type. We can see the return type here. So if we invoke this, first of all, we have to define a map. And after that, we have to invoke this method. And if I debug the result, I will have what? I will have the map, it will have the key, the name of the s object, and then it will have the value as the token of the s object. So I will have all of that by using this method and as we have stated the key will be a string of S object name and the value will be schema s object type which is the token that is corresponding to the S object.
Once we have the map we can extract all the S objects into a set and then into a list to sort it. So as you can see here we have this method from the previous slide and then we are defining a set that will take the keys of this method and then we are defining a list that will take the set as an argument and then we are sorting this list and then we are showing it. So the result of this code will be what? It will be a list of all the S objects within My and they are sorted from A to Z. Until now we have seen how we can access a list of all the orgs s object names and their corresponding tokens. But what if we just need one token? For example, what if we just need the token of the account as object? To do that we have two different ways. First of all we can access the s object type member variable on one s object type such as account and the return type will be a token. So as we have mentioned a token has the data type of schema that s object type. So we are defining a variable of this type and then as you can see here we are using one s object type that is account and then we are using the dot notation to specify the s object type member variable.
So invoking this we return to us the token of the account as object. The second way is to call the get s object type on the account itself or on the list or on a map. To do that we just type the variable itself or the list or the map and then we use the dot notation to call the get s object type function and the return will be what? It will be a token. So as we can see here we have an account called A and then we are using the get s object type on this account and the return will be a token t. And as we have mentioned the data type of a token is what it is Schema S object type. So these are the two different ways that we can get a token of a single S object. Now that we have a token in hand or a list of all our orders token, what can we do with them? Tokens by themselves do not have any value but we can use them to get the details of the S object. So in this case, as we have mentioned, the details of the S object is called S object describe results and the data type is what it is schema that describe S object result.
So this is the real deal. This is the real details of the S object and we can use tokens to get these. We have two different ways to get Schema that describe S object result. First of all, we can use the Schema S object type static variable with the name of the S object directly. So in this case I am defining a Schema describe S object result variable called DSR. So this is an S object describe results and then I’m using the Schema S object static variable with the name of the S object directly and using that I will get the S object describe result of the account as object. The second way is to call the get describe method on the token itself. So this is the real use of tokens. So as you can see here, we had a map from the previous example. We can use this map to get the value of the account key and then we can use the dot get described to get the describe result of this. And also we can use directly on a token without a map. So as you can see here, we are defining a S object describe results called DSR and then account that S object type will return to me a token and then I’m appending this to the get describe method to get the describe result of this token.
- 3.1- Object Schema – part 2
Now let’s talk about fields tokens. Up until now we have seen that S objects have tokens and we can use these tokens to describe information about the S object. But also fields of a single S object have tokens and we can use these tokens to get more information about the fields themselves. First of all, let me show you how we can get a map of all the fields an S object. To do so, we have to first define a map and this map will take assignment schema that describe S object result and then it will be followed by the fields that get map. So as we can see here, we are defining a map. This map will have a key of string and the value will be what? It will be the token of the field and data type, as we have seen earlier on, is Schema s object field. And then we are defining the variable name and then we are giving the describe and then we are following this by fields getmap and then I can create a set and then a list and then I can sort this list to get all the fields of this s object.
I can also use the map example that we have seen in the first slide. So I can use the map of S object names and S object tokens and then I will append this by get described to get the describe of the account S object and then I append this by fields get map. So this is the exact same thing as the previous slide but now we are using the map that we got from the first slide. So we get a list of all the S object names and then their corresponding tokens and then we get the value of the account name this is the token and then we append this with the get describe method to get the describe of this token. And then we append this with the fields that get map to get all the fields of this as object. And then I can do set list and I can sort the list and then I can debug this to get the list of all the fields of the account as object. Now, how we can access a single field token?
As we have mentioned, a field token data type is what it’s a schema that S object field. And we have two different ways to access a field token. First of all, we can access the static member variable name of an S object static type for example account that name. So as we can see here, we are defining a field token called F. And then as you can see here we are defining the S object and then the name of the field. So in this case the return of this will be the token of the name field of the account as object.
The other way is to use the get S object field method and this can be used on the field describe result. So we have to first extract the field describe result and then we have to append this by the method Get as object field. So this is the example as you can see here we have DFR. DFR is a field described result and then we are appending this by the Get s object field a method to get the token of the name field of the account as object. Why? Because this feed result is on the name field. Now that we got the field token in hand beat a list of all the feed tokens of one S object or a single field token, what can we do with it? What we can do, we can get the field describe results from the token and as we have mentioned, the field describe result data type is what it is schema that describe field result and to get this we can use two different ways. First of all we can access the field a member variable of an S object token with a field member variable such as name, billing, city and so on.
So here we are defining a field described called DFR and then we are using Schema s object type that account, that field, that name. So using this we will get the field described result of the name field of the account as object and the second way is to call the get described method directly on the feed token so if we have the feed token in hand we can use this method to get the field described result. So as you can see here we have schema that describe field result DFR. So this is a field describe result variable and then we are using account description describe account. That description will return to me a token and then I am appending this with the get describe method.
To get the field described of this token you can also use the different classes and methods of the Schema namespace so if you check the Schema namespace you can see that it has many different classes and each one of them has many different methods that can be used to get metadata information. For example, we can use these classes and the schema namespace. We can use the describe as object result class, we can use the describe field result class, we can use the child relationship class and so on. And each one of these has a list of methods that we can use to get metadata information about different metadata type and the salesforce. org. Now let’s go to salesforce and let me show you some examples and after seeing these examples you will fully understand tokens and describe.