 Hello friends. I'm Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. In this video, I'm going to demonstrate you how you can use LIST in Apex. So first we are going to cover the introduction part of LIST and then I will be explaining each and every method that you can use with the help of LIST in Apex. So I will be demonstrating them as well. So if you want to watch more videos related to Apex and Salesforce, so I have provided the complete playlist link in the description of this video. So you can use that link to watch other videos as well. So now coming on to the list. So basically a list is an ordered collection of elements. Then each element of LIST has an index for identification and index position of first element is always zero. So you can put element on a particular index as well and you can fetch values from a particular index as well. Then LIST can be nested and even multi-dimensional. Elements can be of any data type like primitives, collections, S objects, user defined types and built-in types. User defined types are like if you have created any class in Apex, so you can use that class with LIST as well. And built-in type means if class is already implemented, so you can use that as well. So now I'm going to show you a few examples like how we can create the list. So here you can see string and integer both are primitive data types. So we are creating LIST of string and LIST of integers. Then at second point you can see LIST of accounts, so it is basically referring to S object. Fourth is also referring to S object but here we are also using SOQL. So we are querying the data from the Salesforce object. So these are some examples. So let me open developer console and I'm going to create a new class naming it as LIST demo. So inside this class I'm going to create a method, so public void naming it as LIST method. So inside this method I'm going to create all these things. So let's say I'm going to create LIST of string naming it as strList equals to new LIST of string. So this way you can create instances for LIST of string type. Now, if you want to add values to it, so we need to move to methods. So first I'm going to explain methods with the help of string then we will also cover how we can use S objects with LIST. So here you can see first method is add and inside parenthesis we need to pass the element that we need to add. So I'm going to write strList.add and inside bracket in single quotes I'm writing India. Like semicolon. Now I need to add one more value. So I'm adding USA. Now I need to show whatever I added into LIST. So I'm using system.debug strList equals to then plus strList. So this way you can debug whatever values are available in your list. So I'm saving this. Now to execute this class directly we can open execute anonymous window. So here we can create instance of this list demo class and we can execute the method which is mentioned inside it. Because right now we are not implementing any kind of trigger or we are not calling this class from any automation. So I'm going to create instance of list demo. So let's say L equals to new list demo. Now L.List method, right? And I'm checking this checkbox as true so that debug log will be opened automatically. And I need to execute both the statements so I'm not selecting them. I'm just clicking on execute. So you will see debug log will be opened automatically. And I'm clicking on debug only. So here you can see strList equals to India comma USA. So both the values are available here. So this way this first method I hope you understood how we can add values to list. Now if you want to add value to a particular index. Now let's try this. So I am adding strList dot add. So right now what is happening on zero index India is available on one index USA is available. So I'm putting new value on one index like UK. Right. And again I'm opening anonymous window and executing this code. And clicking on debug only. So here you can see on zero index right now India is as it is on one index now UK is available and on two index USA is available. So if you insert a particular value on a particular index and if that index is having value already so that will be shifted to further indexes. So this way you can add value to a particular index as well. Now add all. So if you want to add values of a list into another list, then you can use this at all. So this is our first list. Let me create one more. So list of string strList two equals to new list of string. Right. Now I'm going to values into it. So strList two. Let's say Australia. I'm adding Australia into it. And then strList two dot add all. And here I'm passing strList. And I'm going to print both. So this way I created two lists. I'm going to execute them. So two results will be there. So let's see what happens in output. So clicking on debug only. So here you can see list strList one is displaying India UK of USA and strList two is displaying Australia then India UK USA. So Australia we provided as a value with add method and with add all method we provided strList. So complete list is added to another list. So this way you can use add all methods so that you can add a list into another list. Now at fourth number we have one more method at all, which is receiving set. So in my another video that is after this list in the playlist. In that video I will be explaining the complete use of set, but here for demonstration purpose. I'm just creating a simple set and I will be adding that set into a list. So I'm creating set of string str set equals to new set of string, right. Now I need to add values to this. So here I'm adding values for set str set dot add. Let's say Japan and strList two dot add all str set, right. So I'm just printing both the list and let's comment this one. I'm just printing list two because I'm adding set into list two. So I'm displaying that only I'm closing this output executing this. So now you will see the output so debug only so here you can see Australia India UK USA was added through list Japan is added through set and Australia we added individually. Then str list we added then str set we added. So this way you can add complete list to another list. Even you can add complete set to another list as well. Right. So I hope you understood all the all these things. Now we have clear methods. So let's say we are printing str list to here. And I'm calling str list two dot clear here and I'm copying this and pasting it here. So before doing clear, we are going to print the result and after calling the clear method we are going to print str list two. So let's see what happens. So one more time I'm executing this. Sorry, I just forgot to save this. I'm saving this. So here you can see str list two is displaying the values in that is displayed on line number 16. But at line number 18 you can see list is empty. Why so because we applied this str list two dot clear. So clear automatically raises all the values which are available inside the list. Then we have clone. So that is to create copy of the list. So let me erase all these things and removing this set as well. So now you can see we have values inside str list only. And I'm going to clone this to str list two. Right. And I'm going to display both str list and str list two. So both will be displaying same results. So I'm executing this. So this way you can copy content of a list into another. You can see both are displaying same results. So this way you can use clone method. Then we have contains method. So contains check whether a particular element is available inside list or not. So it returns Boolean value through false. So Boolean result. Then result equals to str dot contains. And here I'm passing India. And here I'm, let me comment these. So I'm commenting these both and I'm using one more system. And here I'm going to print result equals to. Yes. Right. So if India is available in str list, then areas will be printing true. Otherwise falls. So let's execute this. So right now India is available. So let's hope it will print true. So it is printing true. And if I'm typing India one and now if I execute this, you will see it will be displaying false. So this way you can check whether a particular element is available inside the list or not. So all these operations I am performing on string, but you can do these for other as well for integers for doubles for s objects. So as per the data type, which you are using inside list, you can use the methods. Then equals. So we can compare two lists. So, so str list is containing three values, but str list two is empty. So let's see what happens. It also returns Boolean value. So it will be displaying false because both lists are not having same values. Right. So this way I hope you understood all these methods, how you can use them. Now we have next method as get index. So I'm preventing this. I'm going to use this people. So now I'm going to use that method inside the debug. Right. So we need to use get method based on index. So str list dot get and I'm passing index number two. So what will happen? It will display the value which is available on index number two inside this list. Right. And I'm commenting this as well. So on zero two index, we have USA because on one index we have UK on zero index, we have India. So on two index USA is available. So that will be displayed. So now I'm executing this debug only. So yeah, you can see str list is displaying USA only. So this is working as well. Now get S object type. So for this purpose, I need to use S object. So I'm removing all these statements. This is basically SOQL. So through SOQL, I am just querying ID and name of account object and limit one is telling only one record will be queried and that will be stored into S object. So now I'm going to use this method gas S object, get S object type. So here I'm going to use object type and after plus I'm going to use that. So I'm using SOBG dot get S object type. So here you can see this method is available. So this is a generic variable SOBG which is of type S object. And it is retrieving or receiving values related to account object through this SOQL. So automatically this method will identify type of this SOBG like which type of values are available in this. So I'm saving this executing this. So now we need to check the S object type as output. So it is executed. So in debug only, you can see object type is equals to account because we queried account. If I put contact here, if I put contact here, and if I again executed, so it will be showing object type as contact. So this way, if you are using generic variable and storing specific type of object values into that variable, so you can check which type of records are available in that SOBG, generic origin. So you can use get S object type method for that. Then index of, so here we can identify index of a particular value. So I'm making comment and system dot debug. I'm just calling str list dot index of India. So it will tell us like on which index India is available and it will search for the first occurrence only. If India is available multiple times, so first occurrence will be printed. So here you can see it is showing zero because India is available on 0th index. It is working well. Then is empty. So we can check it as well. So str list dot is empty. And you can also store this result into Boolean variable and then you can display that Boolean and you can directly display this in system dot debug as well. And you can also use this str list dot is empty into if condition. So sometimes in our code, we need to check whether list is containing some values or not. So in that case, in if condition, we apply this condition. Sorry, this method. So I am executing this. This empty method is very useful to check whether list is containing some content or not. So here you can see it is showing false because our list is not empty. Then remove a particular index. So this I think you can use at your own. Then call remove and pass the index. So that index value will be removed from the list. Then set is similar to add this one second method. We need to pass the index and list element that will be set on that particular index. Then size and sort. We can use so str list dot size. I'm executing this so it will tell us the size of the list. So this you can also use. So it is showing three because list is having three values India, USA and UK. Then we can have sort as well. So right now it is printing India, then UK and then USA. So it is already sorted. So let me put that here. So on zero index we have Z then on one index we have UK and then USA. So I'm saving this. So let's see. Okay. So it is giving me one error. What it is saying method does not exist or incorrect signature. Why debug. Okay. So I need to resolve this error. It means we cannot directly print this. We need to store this into another list. And then we can display that list. Okay. This is also incorrect. So what we need to do, we need to use this first str list dot sort and then we can use this here. So now it will work. You don't need to store it into another list. So I'm executing this. So this way you can also see if you are using any method incorrectly. So automatically error will be thrown. So UK USA and Z India. So India was on zero index, but now it is displaying on last index. So this way you can sort values which are available inside your list. So this way all the methods are completed. Now let me show you how you can use these accounts. So we already used it here, but now let's use it from the beginning. So list of account ACC list equals to new list of account. So this way you can create list of S object. Now ACC list equals to then you can write select ID name from account and you can say limit. Let's say three. So three requests will be required. Right. And if you want to display those so you can write system dot debug. And here you can write the ACC list. Then plus ACC list. So this will display as it is. And here this ACC list is a variable of this list of account type that we are going to display. So this way you can you can create list of S objects as well. So this is very important. So while doing any operation, we mostly prefer this kind of thing because whenever you are going to perform any DML operation like insert update, so then forcefully you need to create list because those DML operation can only be performed on the list. So now I'm going to execute this. So you will see three records of account will be displayed as output because we have provided limit as three. So this is first ID name, then second ID name. And if I scroll it, so you can see third ID name. So three account records are displayed through this list. So this way you can use S objects with the help of list. And if you have created any class, if you have any, if you have created any class, so you can use that class as well. So inside these angular brackets, you can put the name of class, then you can create a variable, then new list of. So this way you can do, right? So these things you can do as well. So I hope you understood about the list through this video. So I tried to explain the theoretical concepts, then these examples I demoed you like how we can query the list as well through objects, how we can use primitive. So similar to string, you can use integer as well. And all these methods I demoed you one by one. So you can practice them in your org so that you can get familiar how we can use list in a picks. So in my another video, I will be explaining about set and map. So watch those videos as well so that you can understand all three collections list, set and map. All three collections are important if you are working in Salesforce as a developer. So I hope this video will help you to learn about the list. If you want to watch more Salesforce related videos, you can go to description of this video and there you can find links of various playlist. So you can follow them. Thank you for watching this video.