 Hello friends. I'm Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. In this video, we are going to discuss about S objects that we can use in Apex. So before starting, if you want to watch more Salesforce related videos, so you can find my all videos links on this website, studiesalesforce.com. So do visit this. So now there are three scenarios. So first, we are going to create a subject variable for standard object. Then for custom object and then generic S object variable we are going to create. So let me open developer console. So it is already opened here. So here you can see we have open execute anonymous window. So I already demonstrated how to use this in my previous video. Those are related to list set and back. So we can run Apex code from here as well as we can create an Apex class and we can create that class instance and then we can call the methods as well. So first I'm going to use standard as S object. So let's say I'm using account. S object variable is ACC. So account is object and I'm going to create its variable. And let's say I'm instantiating it like this, right? Now if I want to assign values so I can use this ACC dot name equals to test. So ACC is the variable of this account as object and name is the field and I am assigning value to this. Then ACC dot phone. So this way you can create a subject variable and you can assign values, right? Now if you want to display it so you can write this. So that we can test whether ACC is containing test and 1, 2, 3, 4, 5, 6 and name and phone fields or not. So you can click on execute. So debug log will be opened and from here you can click on debug only. So you can see ACC variable is showing account values, name, test, phone, 1, 2, 3, 4, 5, 6. So all the values are available perfectly, right? So this is anonymous window from where you can execute your Apex instructions. Now let me show you how you can create class and method and how you can call that. So demo S objects, right? So here we can create public void demo method and I can copy and paste this code here, right? So here you can see this method is available. Now to execute this class method, I need to create instance of this class. So I'm going to do this now through D dot demo method. So this way I can call the method. So now here you can see two options are available, execute and execute highlighted. So this time I don't want to execute these statements. I just want to execute these. So I'm selecting these and I'm going to click on execute highlighted. So you will see, okay, incorrect signature. So I just forgot to save this code. So that's why that error was coming. So now I saved it. So whenever you change in your class, you need to save the code. Now I'm selecting this and executing the highlighted statements. So here you will see in debug only same output will be displayed. So these are two ways through which you can test your code. So normally this anonymous window we use for testing purpose. But if in your project, if you want to implement actual Apex code. So in that case, you need to create classes. And then those classes can be called through trigger flows or a component or LWC component. So that way you need to implement your Apex code that will be called by any other automation tool. Right. So now I'm going to remove this code and we will be following this approach. Right. So in this method, I will be implementing all the code and then we will call this method and execute. Right. So, so this way I explained you how we can create as object variable. Now one more thing you can assign these values here as well. So name test comma phone equals to 123456. Right. So whenever you are creating as object variable. So in the constructor. First you can choose the field name and then you can assign the values as well. So if I make this statement as comment so this will work as it is. Right. So this way I hope you understood how we can create as object variable. Now let's understand the customer subject. So for that purpose I'm going to use this student. So for custom object you can see we need to use this underscore underscore C. So I'm going to copy this and here I'm going to use this. Right. And in case if your custom object or let's say your standard object is having any custom field. So you need to use that field with underscore underscore C. So here you can see this phone field is having underscore underscore C how high school phone. So we will be using it that way. So let's say STD dot name equals to test and STD dot phone equals to 123456. And one more thing whenever you type variable and press dot so you can see this developer console is automatically showing all available field with this as object that is student. Right. Then system dot debug and STD. That's STD. Right. So this way here you can see I created variable for customers object and I'm going to display these values through system dot debug. So I'm going to execute the statements. So all these statements are available in a single method. So when we will be executing this. So right now you can see only two statements are available in this anonymous window. So we can just click on execute. Right. So a complete method will be executed and two outputs will be displayed. So click on debug only and here you can see first account information and then student information. So student name is available in high school phone number is available. So this way you can create as object variables. So further you might be thinking we created variable and what is the use of this variable can we use these variable to insert the records in Salesforce. So answer is yes. So in my next video in which I will be demonstrating how to perform DML operation in a picks like how to insert records how to update record how to delete record. So in that video I will show you like how you can use these as object variables to insert update delete records. Right. So this time we are only understanding the behavior of as objects so that you can understand how we can create variable how we can assign values to fields. Those are available in those objects, whether it is custom object, whether it is standard object or whether it is standard field or custom field. Right. So in case of standard object and field we don't use underscore underscore C but in case of custom we use underscore underscore C so that you need to remember. So this way I demoed you standard as object and custom now turn is for generic as object. Right. So now what I'm going to do. I'm going to create generic as object variable OBJ. And I'm going to use this here. Right. And after creating this. So this account record. Then will be created and that will be assigned into OBJ. Now to perform operations on these values I need to typecast it. So I'm going to create account ACC equals to then account and OBJ. So this way you can typecast your generic as object variable into specific as object. This OBJ is generic because we are using as object here as a data type. And we are creating this account assigning values then I'm typecasting this OBJ into account and then this ACC we can use further. Similarly here also we can use this OBJ. And later on. So I'm going to use these name and high school phone here commenting these and here I'm going to create this student STD equals to then we need to typecast this OBJ. So this way you can use only one generic as object variable for creating two specific instances right and then further you can typecast it so that that generic as object variable can be converted into specific one and you can perform further operations. So this time if I execute this so same outputs will be. So this way you can see account and student information are displayed here. So this way you can create generic as object variable then assigned information of specific newly created variables and then further you can typecast that generic as object into specific. Right. So this way I gave you a brief overview how we can use standard object standard as object customer subject and generic as object. Now, further, we are going to use these things together. So in my previous videos, I explained about list separately set separately map separately and in this video from the beginning we are learning about as objects. And now we are going to use all these things with loop as well. So in this video you will see the use of as object with list set map and we are going to use them in loop as well. So I'm removing these statements. Right. So I'm first creating list of account. ACC list equals to new list of account. Right. So here you can see I'm using as objects standard as object account and I'm creating its list. Why I'm creating list because whenever we apply SOQL to query information from objects. So that information return that information always returns in form of list. So here I'm going to use this list. And then I'm going to apply SOQL. So I'm applying select ID name from account and I'm applying limit to so only two records will be received and those information will be available into ACC list. Right. So for testing purpose, we can apply system dot debug. ACC list plus ACC list. So whenever you apply SOQL and store the result into a list. So always through system dot debug you can test whatever information is returned. So again I'm executing these. So in debug. We will click on debug only. So here you can see two records are available here. So their ID and names are displayed. Right. Now I'm going to apply a loop. So let me comment this statement. So for account ACC then colon space ACC list. Right. So this way you can use this list that is having a result returned through this SOQL and you can use this list completely here and one by one record will be available into this ACC and then you can use this ACC dot ID ACC dot name inside this loop. You can use only these two fields because we create these two only. If you want to use another field so you can use those fields by separating them with a comma. Right. So let's say by iterating this loop we are going to create a set of IDs, set of account IDs. So sometimes this also required in the projects. So if you have a list and you want to identify the IDs only. So in naming it as account IDs, new set of ID. So now I'm going to use ACC ID dot add and then ACC dot ID. So this way you can iterate over this list and each record will be available into this ACC and through ACC dot ID I'm going to assign this ID into this ACC IDs set. And later on I can apply this system dot debug. So you will see only IDs will be displayed. So ACC IDs. So I'm going to display the set only. So you will see two account IDs will be displayed. Here you can see. So two records were queried and their IDs are displayed with the help of set. So this way you can iterate over the list and create a set and further you can use this set for further calculation. Right. Now, if we go to this slide, so here you can see I have mentioned apply null check before using list set map in loop. So before using this ACC list in this loop, we need to apply a if condition because sometimes it happens like you, you are going to query the records but the desired records are not available. So in that case this ACC list will be empty. So you can write if ACC list dot is empty. So this is the method that we are discussed in the video related to list. So if list is empty, so this method will return true otherwise false. So if list is not empty, so it will be returning false, but we need to convert it into true because we want to execute the loop. So we can apply not before this and you can place this loop inside the safe condition. Right. So this way you can implement the loop. So first you need to check the list. If it is not empty, then you can iterate it and you can create the set. Right. So do remember this thing. Now, one more option is available. You can use SQL directly in the loop. So instead of creating this list, you can use this SQL directly here. So in that case you won't be requiring this if condition. So I commented it. Right. So this if is now commented and this follow up is using SQL directly. So in that case you don't need to check whether list is empty or not because we are directly querying the records here if nothing is queried automatically this loop won't be executed. Right. So this is absolutely fine. You can use SQL here as well. So I hope this way you understood how we can store SQL result into list, then how we can check whether list is empty or not, then how to iterate over list, how to store IDs from list to set and how we can use SQL into loop. Right. Now you can use set as well in loop for iteration. So now here I am applying if ACC IDs. This is set dot is empty. If set is not empty, if set is not empty, then I want to display the values. So I'm placing it here. Right. And I'm going to display it with a follow. So I'm applying follow. So this way I'm applying it. This is set ACC IDs. It is of type ID. So that's why I mentioned ID here. I is available. And here I'm going to display. So one by one IDs will be displayed on debug. Right. So for demonstration purpose I'm using this statement but in your real projects. So if you are iterating on a set of ID, so maybe you will be applying some more operations. So first let's execute this. So here you can see I'm using SQL here directly. So this won't be executing so we can make it a minute. So I'm executing this. So you will see two outputs now in two separate lines. Number 15, but twice ID of first record and ID of second record because we are iterating the system dot debug inside the loop and earlier we displayed set as a whole through system dot debug. So this way you can iterate list as well. You can iterate set as well. Right. So I hope you understood how we can use these things in apex. Now, next is map. So, now I'm going to create a map. So what we can do, we can create a map of ID and account. I D name it is I D account equals to new map of ID comma account. Right. So now what we can do we can, we can use a SQL here also. So what will be the effect of this. So IDs will be converting into keys automatically and the account information in form of ID and name will be the values. So this map will be created with this SQL. Right. Instead of SQL, you can also pass this account list as well here. Right. So let's say if this statement is written here. So we can use this instead of this SQL. Both are same. Right. So both are same. Now, we are creating IDs ID set by trading list over here. Then we are iterating set here. Here while iterating set, I'm going to apply one more if condition. So what we are going to do, we are going to use this map here inside this if condition and I'm checking map dot contains key and key I'm using as I so as this account IDs is a set so it is containing IDs. Right. So one by one ID will be available into this I variable. I'm passing this I variable into this map with this method contains key. Right. So if this map is having I value as a key. So this condition will be true and I'm going to use system dot debug. I'm going to display map value and to fetch map value. I'm going to use map name dot get and I so whenever we pass key with this get method. So it automatically returns the value that is available inside map with respect to that key. So I'm making this statement as a comment. Right. So now, from the beginning, let's see what we did. So first we created a list, then we queried account record. Then we pass this list to this map. So map will be having ID and account details. Then we traded that list. So we can use that list here as well. Right. So let me use that so that we can use this if condition as well. So I'm uncommenting this. Right. So with this list, we with this list we created map, then we with this list by iterating we created a set. Then we are iterating over set and then we are searching keys into map. And if particular keys available, then we are displaying that key corresponding value through system dot debug. Right. So this time you will see only a map values in form of ID and name will be displayed on will be displayed as output. So I'm going to execute this. So you will see two outputs in two separate lines. Here you can see account ID and name, then again account ID and name and these are values. So only map values are displayed with respect to key if keys available in the map that is in a particular set. So then only map values will be displayed. So this way you can iterate list in map. Sorry, list in loop. You can iterate this set in a loop. Right. And this way you can use map so that you can search a particular key whether it is available in the map or not. Right. So I hope this way you understood how we can use all the collections list set and map with the with loop and with S object. And later on, you can also use a map key set here as well. Right. So for understanding purpose, I'm going to use this. So this is map name dot key set. So if you use this so it will automatically return all the keys available in the map in terms of set. Right. And one by one, those will be available into I and then you can use it further. So let's say if you are having two, three maps. So one map is a trading the loop and another map you can use in the if condition so that you can test better keys available into that another map or not. So this way you can use all these things together. Right. But remember whenever you apply a loop on set map or list, apply this if condition so that you can check whether those collection variables are empty or not. If they are empty, then we don't want to iterate the loop. If they are having some value then only we want to iterate the right. So this way I hope you understood whatever I demonstrated in this video regarding these using off S object, whether it is standard customer generic or how to use S object with list set and map and how to iterate them with the help of you. Right. So if you want to watch more Salesforce related videos so do visit this website so I have placed all video links together on this single platform. So so that you can prepare yourself so that you can walk with a company that is having projects related to Salesforce. So I hope you understood whatever I demonstrated in this video, so do follow my videos so so that you can learn Salesforce. Thank you for watching this video.