 Hello friends. I am Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. In this video, I'm going to explain and demonstrate the use of data type in Apex. So we will be discussing how to use primitive data type, S object collection, then enum, then user defined Apex classes and system supplied Apex classes. So all these we can use as a data type in Apex. Now before starting, if you want to watch more Salesforce related videos, like flow, workflows, process builder. So all these videos playlist link you can find in the description of this video. And in my previous video, I explained you about introduction of Apex. So let's start with primitives. So these are some examples how we can create primitive variables. So primitive data type is Boolean integer decimal string ID and is active numPrize as an ID. These are variables. And we can assign related values to these variables as per the data type. So now let me take you to developer console. So in my previous video, I also, sorry, I already demonstrate to you how you can open this developer console and how you can create a file Apex file in this. So if you want to open existing file, so you can go to open. And here you can see various components like classes, trigger pages, page components, objects, static resources, packages. So if you click on class, so all related classes will be available here. Now if you want to create new class, so you can click on file and choose Apex class. So I'm putting name as data type demo, right? So let me first give you brief idea like how we can create class and how we can create instance of the class so that we can execute constructors or methods those are defined inside the class. So first I'm going to create its constructor. So this way I'm defining constructor and inside constructor and writing system.debug. So whenever you want to print something, so you can use system.debug in Apex. So this way I created this constructor. Now to execute this, I need to open execute anonymous window. So here I'm going to create its instance. So data type demo equals to new data type demo. So as we create instance, so automatically its constructor will be called. So there are two options, execute and execute highlighted. So if you want to execute all the statements, then just click on execute. Otherwise you can select a specific statements and then click on execute highlighted. So right now I'm clicking on execute. So when it is executed, so you can move to log and you can open this latest one. So in the latest log, you can see that output. So you need to click on debug only. So here you can see system.debug printed. I am in constructor and this three is showing line number. So at which line that system.debug is available in the Apex class. So it is on line number three. That's why output is showing three as line number. Now here you can see one more option open log. So if you check this checkbox, so automatically logs are open. So we don't need to click on the logs. Those are generated. You just need to click on debug only so that debug results will be displayed. So this way you can use constructor. Now I am removing this and I'm going to create a method for this. So I'm writing public white. And inside this I'm writing system.debug. I'm in method. So methods are not executed directly. We need to call them. So after creating instance, you can call your method. So again, I'm clicking on execute. So logs are automatically open. I'm clicking on debug only. So here you can see it is showing I am in method. So this way you can create constructor as well as methods as per your requirement. Now I'm going to create a primitive. So let's say I'm creating Boolean variable B. And inside this method I'm assigning true in this Boolean variable. And after this I'm writing one more system.debug. So B equals to 10 plus B. So this is the way you can print variable as well as text. So whenever you want to display text, you need to enclose it in single quotes. And then for separation plus and then variable. So remember one thing, all the strings which you want to display must be enclosed in single quotes in case of apex. So this is a primitive variable. So this way we can create comments as well. So this is primitive data type variable. So Boolean is a primitive data type and its variable is B. So inside the method we assigned true in B and then we are printing it. So I'm saving the code moving to execute this code. So I'm clicking on execute clicking on debug only. So here you can see I'm in method and B equals to true. Both the messages are displayed. So this way you can use any variable like which is of primitive type. You can declare it outside the method so that you can use it inside any method. And if you want to create local variable, then that is also possible so that you can create inside a particular methods so that it can become local. Now moving further. So this way you can use these primitive data types. Now comes specific S object. So S object is very important in terms of Salesforce because whatever we create in Salesforce, it stores under a particular object, right? We create records. So records are directly associated with the objects. So we have two type of objects standard and custom. So in first line you can see standard object variable creation is mentioned. And in the last line you can see custom object variable creation is mentioned. And if you want to assign values to the fields related to a particular object, then you can use two notations like in parenthesis, you can use field name and you can assign values directly. Otherwise you can use the variable name like ACC then dot and then field name and then you can assign the values. So let me give you this demo as well. So I'm creating variable for account. So this way I'm creating this. Then ACC dot name. So here you can see as you put dot. So all the related fields of account object will be populated automatically here. So you can choose them directly. So here you can see I am assigning test in name field of account object then ACC dot phone. This way I assign test and 123456 in two different fields of this account variable. Now if I want to display it on output. So what I can write account equals to then plus ACC. So if I print ACC variable directly, so it will be displaying both name and phone because we provided two values as of now. So this way I implemented this. Now I'm executing it. So that we can check the output. So here you can see account name and phone both are displayed. So this way you can create variable for S objects. So right now I created object for standard object similar to standard. You can use your custom objects as well and you can assign the values further. We can apply DML operations as well. Like if you want to insert this account into database so you can use insert. You can use update. You can use delete. So all those DMLs you can apply here. So right now we are not focusing on the DML part. So that will be available in the upcoming video that will be based on DML. Like how we can create a new record through a fix, how we can update the record or how we can delete a particular record. Right. So those operations I will be demonstrating in another video. So here I think you understood how we can use primitive data types and how we can use S object as a data type in a fix. Right. So you can try it in your developer console. So I demoed you standard object. You can use custom object as well. Now one more thing that is generic S object. So right now I created specific account variable, but you can use S object as a data type. So for example, here I can write S object. And OBJ. So with this example, you can see I'm passing name equals to test inside the parenthesis. So account variable will be created and its reference will be stored into S OBJ one. Right. Now further, if you want to access the values, you need to typecast it. So let's say I'm writing name equals to test here. Removing these statements. Right. Now I need to typecast. So what I need to write account ACC equals to and then inside bracket account and then so OBJ is a variable of S object type. Right. It is having reference of this account, but now we need to typecast this OBJ into account so that through ACC, we can access its particular fields. Right. So without having specific data type, you cannot access the fields. So whenever you use a generic S object, so you need to convert them into a specific S object. So now if I execute this, so it will work appropriately. So ACC will be displaying name only because we didn't provide it phone number this time. So here you can see name equals to test. So this way you can create S objects. You can use S object as a generic data type and then you can convert it to a specific. Here you can read the note. One of the benefit of casting is to be able to access fields using dot notation. So once you have casted your generic S object into specific object, then only you can use dot notation, which is not available on the generic S object. So this you need to remember. So what is the benefit of generic S object variable? So we can use them to refer more than one specific object. Like here, I used this OBJ for referencing account, but after typecasting, I can use any other object like this. OBJ equals to new, let's say contact. So this way I can assign the reference of contact into OBJ and then further I can typecast it into a contact variable. So that way we can do this also. And one more thing, whenever there is any error, so you can see this red exclamation sign will be there and you can go through with your problems like this way. So here you can see it is showing duplicate as a duplicate OBJ variable. So we need to remove this as we declared this variable already. So we can just use its name without using any data type. So now you will see this error will be removed. So this way you can use these generic S objects. Now comes list. So there are three more data types list set and map. So in this video, I will be discussing them briefly, but in my upcoming videos or in the playlist of apex, you will see separate videos for list map and set. So in those separate videos, I will be explaining each and every method related to these data types. But here we will discuss briefly about them. So first of all list, it is an ordered collection of elements. Each element of list has an index for identification. Index position of first element is always zero. List can be nested and even multi-dimensional. Elements can be of any data type. So if you want to create a list, so you can create list for primitive for collection for as object for user defined data types or for built-in data types, built-in types, right? So user defined types and built-in types here refer to classes. Now how we can create list. So here you can see list of string. So it is primitive. So list of primitive, then list of accounts or account is S object. So list of S object, then list of integer. Integer is again primitive. So list of primitive. So this way you can create various kind of list. And even you can query the data that is available in the objects. So at the bottom, you can see one query is mentioned here, which is querying account ID and name and storing it into list. So whenever we query data from apex objects, so they return list of records. So we can use list of a particular S object to store those returned value. So now let me give this demo as well. So what we are going to do, we are going to create list of string. So this way you can create variable for list. So here let's say I'm writing str list. This is the variable. So list is data type. So list of string is now completely a data type. So we are creating collection of string, which will be stored inside str list. And this way we initialize it. Now to add values, we can use add method. So what we can do str list dot add. And here we can write, let's say, then str dot str list dot add. Then we can write. So this way you can put the data. And if you want to display it on output screen or in the logs, so you can write list contains plus str list. So both A and B will be displayed as output because list is containing these two values only. So again, I'm going to execute this. So debug log will be opened automatically. I'm clicking on debug only. So here you can see list contains and in parenthesis, two values A and B are displayed. So this way you can create list accordingly. Now let me demo you how we can create the, how we can query the reports from the database. Before that, focus on these as well. So here you can see my list dot add. We already discussed. Then if you want to get a particular index value, so you can use get method. If you want to add a particular, you can add, you want to add value at a particular location or index, then you can use add index comma value. If you want to clean or clear your list, you can use clear. And if you want to identify the size of list, you can use dot size. If you want to check your list is empty or not, so you can use is empty. So all these methods, we will be discussing in detail while I will be demonstrating you the use of list in a separate video. So don't, don't panic. Like I'm not explaining list in detail because for that, I'm going to, I will be preparing a new video for that. So right now I'm going to explain this last line, how we can query the data. So here I am writing list of account. ACC list equals two. And here I am writing select ID name from account and I'm writing limit two. So only two records will be query, right? So this is actually known as SOQL. This is actually known as SOQL. So we are querying the data which is available under account object and that list will be stored inside ACC list variable. Now I'm going to display this. So this way you can end this list. And one more thing apex is case insensitive. So you can write things in uppercase or lower case that doesn't matter. But try to follow the case sensitive nature because that is better to understand. So if you have written, let's say ACC list like this. So always use further in the code like this, like Alice capital and other letters are small. Don't write ACC list in all capital. So follow that notation. Even if apex is case insensitive, right? So now I'm going to execute this. So you will see two records will be received with this ACC list variable and those will be displayed here. So here you can see this is the first record ID name, Jen point. And this is second record. ID and name is United Island based gas UK. So two records only returned into the list because we already mentioned the list as sorry limit as two. So this way you can use a list of account or list of any a subject so that you can query the data and you can store them into a variable. So I hope you understood about basics of list. Then on set. So set is an unordered collection of element. It doesn't contain duplicate elements. So in list, we can store duplicates, but set doesn't contain duplicate elements. Set can contain collection that can be nested with one other. Sorry, within one another. Then elements can be of any data type primitive type collection as object user defined type burden type. So this way we can create set. So in place of list, we can use set as a data type. So set of string set of ID set of integer. Similarly, like we added values in list, we can use add values in set as well. We can remove values directly and we can check whether a particular value exist in set or not. Then we can check the size or it is empty or not. So same way we use list, we can use set as well. So again, I will be creating separate video for set as well so that you can understand the proper working of set through that video. Moving further next data type is map. So map is a collection of key value pair. So here we have two entities key and value. So keys are always unique having a value associated and value can be duplicate. So keys are unique and values can be duplicate. Then apex uses a hash structure for all maps, adding a map entry with an existing key over at the existing entry with new. So map key and value can contain any collection and can contain nested collection as well. Then key and value can be of any data type. So you can use primitive key or value. You can use collection as key and value as object user defined types, built in types. Right. So this is the example of map. You need to use map then string comma string. So a first string will be considered as key and second will be considered as value. This is the name of this map and then instantiation of the map. Now how we can put the value. So you need to use put method. So one will be key and first will be the value. Then two will be the key and second will be the value. So let me create this so that you can have its flavor as well. So map off string string. So demo map is equals to new map string comma string. Right. Now demo map dot put one comma first and demo map dot put comma second. So this way you can put the values. Now if we want to debug this make to check to check what is available in the demo map. So demo map equals to plus. So this way I created a map whose name is demo map and I mentioned key and value pair. Now I'm going to execute this so that you can see how output of map displays. So here you can see demo map equals to inside curly basis one equals to first and two equals to second. So one and two are representing key and first and second are representing as values. So this way you can use map in your apex and you can also use map with the s object. So let's say you want to store ID and account records together. So you can create map of ID comma account then how you can store the value. So here first I have created list of account then I converted that into map. So if you pass if you pass list as an argument into map so that will be converted into map. So this way you can use and in third example I'm using map of account comma list of account list of contact. So here account will be key and list of contact will be values. So this way inside a map you can use list as well as values. So this way you can use a list set and map. So in this video I just gave you brief introduction about set map and list but they are very much important. So for that purpose I'm going to create separate videos for them. So those will be available with the playlist apex and Salesforce. So do watch those videos as well so that you can understand their working in detail. And last one is enum. So enum is an upset data type. Enums are used to define a set of possible values after creation of enum. One can create variables methods. So one can create variable method arguments and return type of that type. Right. So this is the simple example. So we rarely use enum. So I'm just explaining this here only. So public enum. Enum is a keyword. So weekdays is the name of the enum. Then these are the values month, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday. Now if you want to create variable for enum so you can use weekdays as a data type and W as a variable. Then weekdays.monday. So we are assigning Monday to W. Now we are checking W with weekdays.q. So if both are equal then W will be returned. Otherwise not. So this way you can use enums to create symbolic constant values. Right. So we rarely use this enum. So you just need to know like we can use enum in epics, but we don't use it frequently. Right. So this way I explained all these things in this video and user defined classes and system supplied epics classes. These you can use as we used as objects. So you can use name of class and then you can create its variable. So as I demonstrated you how to create instance of particular class in anonymous window. So in anonymous window what we did we created data type demo. D equals to new data type demo. So same statement you can use in a particular class. So what you can do if you have created one class so you can create it its instance into another class. So this way you can link more than one classes together to use method of a particular class into another. So this way we used a demo method of this data type demo class. So we can write the statement in any other class as well. So that is what I want to say like user defined epics classes and system supplied epics classes. So these are also known as data type when you create variables for them. So you can use them as well in your epics classes. So this way I hope you understood about the data types which are available to use in epics classes. So along with theoretical explanation I tried to show you each and every example in form of demo. So you can try it in your demo or so that you can get familiar with all these data types. So if you want to watch all the videos related to epics and Salesforce so the playlist link will be available in the description of this video so you can follow that. Thank you for watching this video.