 Hello friends. I'm Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. So this is my website where you can find all Salesforce related videos. Those are created by me. So in this video, I'm going to discuss about SOQL in Epics. So I will demonstrate you various things. So here you can see all these points I will be demonstrating you with the help of Salesforce org. So first I will be telling you how we can write simple SOQL. So basically SOQL stands for Salesforce Object Query Language. So whenever you want to perform any query on standard or custom objects, then you need to write SOQL. So SOQL basically fetch the records which are available in a particular object, whether it is standard or custom. So we will cover these points one by one so that you can understand various things that we can apply on SOQL. So first I'm going to tell you how we can write simple SOQL. So this first point we are going to cover. So moving to the org, so I'm going to open anonymous window where I will be writing the Epics code so that you can understand how we can use SOQL. So first I'm going to create a list of account that is ACCList equals to a new list of account. So always remember whenever you query the records, you store them in a list because there may be chances that you are querying multiple records. So you need to write this. And whenever you write SOQL, you need to use these square brackets. Then try to use the commands that we are using in SOQL in uppercase. But as you know, Apex is case insensitive so you can write in lowercase as well. But best practice is we can write them in uppercase. So I'm going to select ID name. Now from should be in capital, then the object name. So this is a standard SOQL. So I'm going to select ID and name field from account. Now if I execute this query only, so it will fetch all available accounts. Those are available in the account object and will be stored in ACCList. So don't use queries like this. Apply some where clause or limit so that limited number of records should be queried. So right now I'm using limit two. So only two records will be queried and to show them, I'm using system.debar. So this ACC list. So in single quotes, this message will be printed as it is. And this is the list that contains the records which are queried from this SOQL. So this will be displayed on debug log. So now I'm going to execute this and I'm checking this checkbox as true. So it will automatically open the debug logs. So I'm executing this. Now I'm clicking on debug only. So here you can see two records are available. ID and names are available. So this is first record and then second record. So this way we can apply SOQL so that we can query the records related to any object. If you are using any custom object, then you need to use underscore underscore C as well. So here you can see I have two custom objects, students and books. Right? So let me open student. So you can see the API name of student is underscore underscore C and here I have fields. So yeah, for a student name, we have name fields. So I'm going to apply this now. So I'm changing this to student underscore underscore C will be student list. And here I'm pasting object name. So select ID name from student underscore underscore C limit to and I'm going to print student list. So this way you can use custom object in SOQL. So again, I'm going to execute this. So now two students record will be queried and their ID and name will be displayed. Right? So we have record types as well. So those are also displayed. So first student name is Sanjay and record type then another student name is Martin. So record type and ID. So these information are available for custom objects. So this way you can apply SOQL on both standard and custom objects. Right? So I hope you understood how we can write simple SOQLs. Now how to use SOQL for query editor. Right? So this is anonymous window. So here also we can execute SOQLs but here you can see query editor is also available. So here also you can apply SOQL. So you can write select ID name from account and let's say limit 10. So if I click on execute, yeah, so don't put semicolon. So here you can see it is executed. So ID and names are displayed and 10 records are queried. So whenever you are doing development work, so sometimes we just need to validate like we have written correct SOQL or not. So to validate that you can write your SOQL in query editor and if it is executing successfully, so you can see the queried result will be displayed here. So here you can see query results total rows 10 are available with ID and name. So this way also you can execute your queries but if you are using anonymous window, so in anonymous window you can perform any other operations as well like you can create list variable and you can display the queried result that is stored in list. So through system.debug you can display that and you can see the debug logs as well. So in query editor we can apply SOQL only. So if you want to use other statements as well, so you can open anonymous window and then you can execute the codes. So now I'm minimizing this. So we will be using this anonymous window only. So now SOQL with conditions. So we can apply conditions as well. So let's say right now we are using limits. So I am removing this and here I'm applying where name equals to Sanjay. Right. So only those students information will be queried where name is equals to Sanjay. Right. So I am again executing this. So you can use where clause to put any kind of condition. So here you can see the student whose name is Sanjay. The record is queried automatically and it is displayed. So I hope you understood. So based on any field you can apply the condition. Then I'm picking one more thing from the bottom like use variables in query for applying conditions. Right. So I'm now putting this Sanjay in a variable. So let's say I'm using string variable student name and in this I'm assigning this value. So sometimes it might happen like you have some variables and you want to put that variable in the where clause. So how you can do that. So after equals to you need to put semi sorry colon and then the variable name. So if you are putting a name as a constant, then you need to put that in single quotes and without using colon sign. But if you are using variable. So in that case, if you're using variable, so you need to use this colon sign and then the variable name. Right. And variable is having this value as Sanjay. So right now name field will be compared with this variable. If a value matched, then the related records will be queried and those will be available into into this student list that we are printing. So I'm executing this. So clicking on debug only. So here you can see same results are displayed. Right. If I change the name. So let's say I'm choosing Martin executing this code. So here you can see Martin records are available and it might happen like if you put any name that is not available. So let's say Martin one. So this name is not available in student object records. So if I execute this, so you will see nothing is displayed, right, because nothing is queried. So this way you can apply where clause with the help of variables. So I hope you understood this as well. So this limit number of records return we already covered. So now I'm going to pick this null check null check on list return through SQL. So right now you saw I tried to search for the records where student name is equals to Martin one, but no record found. So the student list is containing nothing. So always remember whenever you are applying a loop on this list, let's say I'm applying a loop. So for student underscore underscore C S colon student list. So this way we can apply loop on the list queried. Right. So if this list is containing nothing, so we don't want to execute this loop so that unnecessary these statements won't be executed. So before starting this loop, you can check a student list dot is empty, right. So if this list is empty, so this is empty method will return true. But in that case, we don't want to execute this loop. So we can put not before this and we can put this loop inside this if right. So this way you can implement your statements right now if this condition is true, it means if a student list is not empty, then only this loop will iterate and you can perform operations here. And if list is empty, then this loop won't be repeating. So this way you can apply null check on the list written through SOQL, right. Now moving to next point that is count queried records, right. So we can count the records we queried as well. So for that, I'm going to use this thing count. And I'm going to create one diesel variable that is C. And I'm going to store the result into C. And here I am printing count equals to NC, right. So I'm putting Martin here and making this as comment, right. So if you want like how many reports are queried, you want only number. So you can put count and then brackets here. So the records which are queried, they will be counted and the result will be stored into C. So here you can display that. And maybe if you are implementing this apex and calling it with the help of trigger, so in that case, we won't be using system.debug. Instead, we will be using the returned results to do some operation, right. So for demonstration purpose, I'm using the system.debug. So when you will be seeing my videos related to trigger plus apex. So there you won't be seeing this system.debug. So we will be applying some operation on the queried results. So now I'm executing this. So right now we are getting familiar with the apex. So here you can see count equals to one because one record is available there. Now, if I move here, so here you can see various accounts are available, which are starting with tech, right. So I'm going to count all these. So here you can see total seven records are available, which are starting with tech. So now again, I am converting it to account and here putting account where name equals to. So I'm putting tech here. So I'm just modifying this condition again name. I'm putting like. So I'm removing this variable and putting tech and then percent, right. So if name is starting with tech and having anything after this tech. So those records will be queried. So I'm checking name. So name should be starting with tech and having any other characters as well. So those records will be counted and they will be displayed. So right now we are not using these, sorry, this variable. So I am executing this. So you will see count will be displaying value as seven. So if I debug only, so here you can see seven is the count because the count records whose name starts with tech are total seven records. So this way you can apply this way close as well where we are using name like and then tech person, right. So, so we covered this thing as well now ordering query results. So ordering means we can we can apply ascending and descending, ordering whatever we are querying. So again, I'm going to use this ACC list. So I'm putting it here, ACC list, then ID comma name from account where, so where name is like this, right. And order by name. So by default, order by applies ascending order. If you want to apply descending, so you need to write this BESC. So right now I am going to display it in ascending order on, right. So let me show this ACC list and here I'm writing ACC list. So it will be executed. So now I'm removing this variable as well. So executing this. So you will see the result will be displayed in ascending order automatically. So first account is tech school, then tech school one. And if I go further, then you can see tech school two, then tech school three, tech school four. So this way in ascending order based on name, they are displaying. Now if I want to display these in descending order, so I can apply BESC and click on execute. So this way you will see the output will be available in the descending order. So tech school seven, tech school five, then tech school four. So this way you can see now records are arranged in descending order. So this way you can apply order by based on any field that is available in your object. And if you don't put anything after the field, then order will be ascending. And if you want to display them in descending order, you need to put BESC, right. So I hope you understood this as well. Then limit we already discussed, like you can apply limit 10, limit five. So those records will be queried, then use variables in query for applying condition this we already checked. Now next thing is query related records. So we can query records from parent to child as well, and we can query from child to parent. So first I'm going to cover parent to child. So account is parent for contact. So I'm going to fetch account and all the related contact with a particular account as well. Right. So for that purpose, first I'm going to object manager and I'm searching for contact. So whoever is a child, we need to open that and you need to search for the lookup or master detail. So on contact, you can see account is the lookup. So account is on parent side and contact is on child side. So we will be using this child relationship name. So whenever you query child records through parent, so you need to use this child relationship name in the query. So let me show you how we can do that. So select ID name, then bracket, then inside parenthesis, you need to write child query. So select ID name from context. So here you need to use the child relationship name. Right. So this is, this parenthesis will be querying the child records and here select ID name and after that we have from account. So this will be querying the parent record. Right. And here, if we go here on account, so tech school seven, if I go, so here you can see one contact is associated. So I'm going to put name equals to tech school seven and I'm removing all these things and I'm going to press enter so that it will be in new line. So now you can see, so this select ID name, this will be for account. Then in parenthesis, we are querying the child records of contact. So always remember that you need to go to child record, then you need to identify the lookup to parent and then you can search for the child relationship name. So here you can see, we are querying parent to child. So we are applying SOQL on parent and internally we are querying child records as well. So this is the parent record where name will be tech school seven and its related context will be queried automatically. Right. Now, how we can display the result or how we can iterate that. So we need to check this ACC list. If it is not empty, then we are going to iterate it. So this way I can iterate it. So results are available in ACC list. Right. So I'm going to use this. So if I display this complete variable ACC and let's say I just want to display the context. So ACC context. So here I need to write ACC dot context. The child relationship name will be having the details related to contact. So one account will be available in this ACC and the related context information will be displayed on me. Right. And I'm putting this here. So this will print whole details, including account and related context. But while iterating this loop, we are just fetching the contact details only. Right. So two results will be displayed. So now I'm going to execute this. So here you can see this is ACC list, which is showing on the account details, but it is having context as well that you can see that is displayed in with the help of line number 10 system dot debug. So contact details, contact. So this is the account ID that is available on contact. So if you match these two IDs, which is displayed in first output and account ID, which is available in second output, both are same. Right. And this is the contact ID 0, 0, 3, 2, W, then 0, 0. And this is the contact name Sanjay Gupta. So if you see the related context, so here a contact name is Sanjay Gupta. So only one contact is available here. That's why one contact is displayed. Right. So let me create one more contact here. So here you can see account is automatically populated. So I'm putting Vikas Gupta and saving this. Right. And let me execute that again. And this time I am fetching phone as well. Right. So whenever this ACC dot context will be displayed, so account, sorry, contact ID, contact name and phone, all these information will be displayed. So this time this tech school seven account is having two related contacts. So two contact details will be displayed. So again, I'm going to execute this. So here you can see in first output only account details are displayed and then contact one name Sanjay and phone. Then if I scroll, so here you can see name Vikas Gupta and phone. So these details, so two contacts are related. So both are displayed here. Now, if you want to apply some logic on these contacts, so here you can apply one loop as well, one more loop. So what you can do, you can write contact on equals to ACC dot contacts. And you can apply this. So this ACC is having one account details and that account may be having related contacts. So we are fetching those contacts through here as contacts will be contacts can be more than one. So we can iterate them as well here in this nested loop and you can apply any logic here. Right. So this way you can apply loops as well. So here in this you can see we can apply SOQL for loops as well. So right now I am wearing this and storing into a list and I'm using list here, but later on what you can do, you can cut it from here. You can cut it from here and you can put it here as well. So this is also correct. So whatever you are querying will be available one by one in ACC. And then if you want to fetch the contacts, so you can write ACC dot contacts. So this way you can also apply SOQL in for loop. So I hope this thing you understood. So now next thing is one more thing is remaining that is querying parent values from child. So let me explain one more thing like if we have custom object and we want to apply parent to child relation in SOQL. So how we can do that. So here I have two objects, student and books. So student is parent and book is child. So let me show you that if I search for book and if I go to the field, so here you will see a student look up, yeah, here it is. And if I open this, so now I am on book object which is on child side because it is looking up to student. So that is on parent side. And here you can see child relationship name. So I need to use books while querying the records. So I will be applying the SOQL on student and through student I will be fetching the related books. So let me apply that as well. So here I am putting student underscore underscore C. This will be student list. It will be student. So ID name will be same then for book. Let me search some fields for book as well. So what we can fetch, we can fetch book name ID. Right. So I am leaving them as it is phone I am removing. And here we need to write books that is child relationship name from student where name equals to Sanjay. So the student name should be Sanjay. And for that Sanjay student, whatever books, records are related, they will be queried. Right. And here I am, so let me put this here so that you can understand this also. So I am putting it here. Right. Removing the statement. And this will be printing the student list. So yeah, no, this won't be executed because we are putting query here. And this time, this if condition is also not required because we are putting SOQL in for loop. So if you are putting SOQL directly in for loop in that case, that if condition is not required because if nothing is queried, so this loop won't be executed. So I am removing this loop as well. So we are just going to print book details. So student books details. And here I am using student underscore underscore C STD. And here I'm writing STD dot books. So this way you can modify your query. So we are querying student details as well as the related book details. So this is parent to child query and the results will be available into STD. So through STD, you can fetch the parent details. And if you write STD dot books, so in books, our child records will be available. And further, you can apply a loop as well that I demonstrated already like account contact relationship. So similarly, you can apply that loop as well. So now I'm going to execute this one error. Okay, so, okay, here one thing you need to remember. So earlier we were working on standard objects. That's why we used contacts only. But right now we are working on custom objects. So with child relationship name, you need to append this underscore underscore R as well. So now I'm executing this. Okay, so here also you need to put this so books underscore underscore R. So this thing is very important. So if you are working on standard objects, then you can use child relationship name as it is. But if you are working on custom objects, then for child relationship name, you need to add underscore underscore R. So this thing is very important that you need to remember. So now I'm executing this. So here you can see student book details. So let me go to student records. And I'm opening Sanjay. So here in the letter, you can see two books are available. So let me open, yeah, two books are available. So if I open this, so here you can see book 005 and book 006, two records are valid. So this way I hope you understood how we can apply parent to child query on both standard and custom objects. So this thing is very important. So whenever you will be implementing your apex, so these queries will be required. So I hope now you understood. So one more thing is remaining now child to parent. So I will be demonstrating it for both standard as well as custom. So right now let's understand it for custom first, then I will be applying it for standard objects as well. So I'm removing all these things. So what we need to do now we need to apply query on book. So, so this way I'm modifying it. So this time we have book list. So I'm removing these. So equals to select ID name. And now through this book, I need to query student name as well, right? So whatever book details I want to query, I just want to fetch a student name as well like which student this book is associated. So whenever you are applying child to parent SOQL, so you need to put this thing student underscore underscore R. So again remember, these are custom objects. So whenever you are querying from child to parent, so for parent referencing, you need to use student underscore underscore R then dot and then field name, right? So field name for student name. Yeah, so here it is name. So I'm using it. Then you can apply from book underscore underscore C where name equals to, so let me take a particular name. So I'm taking this name B dash 0005 because it is associated with Sanjay. So I'm putting it D dash 0005 and then system dot debug book list plus book list. So now I'm going to execute this. Yeah, so it is showing some error. Yeah, currently this I need to just remove. So it is executed opening debug only and here you can see it is showing book ID, book name and student name. So that name field field is containing ID. So it is showing that thing. So if you want to fetch any other, if you want to fetch any other field of students, so let me search, let's say we want to show roll number as well. So what you can do, you can write student underscore underscore R dot this roll number underscore underscore C. Let me execute this. So let me check. So what is happening? Basically, we are creating name and roll number. So if we are printing this book list as a whole. So those name and roll numbers are actually not printing. So what we can do, we can iterate a loop. Let's say for book underscore underscore C, then here we can apply book list and inside this loop, you can apply a system dot debug student name. You can write B dot student underscore underscore R dot name and you can copy this, paste it and for student roll number from here you can copy this and you can paste it, right? So now here you can apply if condition as well so that you can check book list is empty or not, right? So we can apply that as well. If not, book list dot is empty. So if this condition will be true, then this follow will be executed. Otherwise not. So I'm putting it here inside if, right? Now if I execute this, so you will see name and roll number will be displayed. So in the top you can see only related student ID is available. So we are querying from child to parent. So parent record ID is available here and then if we iterate that in loop so that we can fetch the name and roll number or other fields as well. So this way you can do. So one by one each book records will be available into B and then you can apply whatever logic you want to apply. So B or you can say book list is having a book ID, book name, then related student name, related student roll number. So all these things are available one by one in this B variable if we iterate the loop and then you can apply the logic and later on also you can apply this whole SOQL here as well. So if you do this, if you apply it here, so in that case you don't need to write this and all the things, those you are querying like related to child record as well as to parent record you can iterate in the loop. So in this case we don't require this, right? So this way you can implement it. So I hope now you understood how we can apply a child to parent. Now one more thing if you want to apply this on standard object, if you want to apply this on standard object, so let me do that also. So I'm removing all these statements. So I'm just modifying this. So what I can do here I am writing account. So I am using child as contact and parent as account. So account name, I'm querying, removing these from contact where name equals to Sanjay Gupta and I'm removing this because I have one feed account.name. So here I can write account.name. So here you can see account name, right? So remember that when we were using custom object, so we used underscore underscore r but this time we are using standard object. So we don't require underscore underscore r here and we can directly query the fields which are available on parent side while querying the child record. So if I execute this, so the contact whose name is Sanjay Gupta, so that context related accounts will be displayed. So clicking on debug only, so here you can see five record matched. So for four there is no account associated with the contact but for one account is associated so that name is displayed here. So this way you can apply child to parent query. So I hope now you understood each and everything that I demonstrated in this video. So I hope you understood whatever related to SOQL I explained and demoed in this video. So do apply all these things in your org as well so that you can learn like how we can use SOQL in APEX in Salesforce. So if you want to watch more Salesforce related videos, so you can go to this website and there you can find links of all the videos that I created. So thank you for watching this video.