 Hello everyone. I am Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. So today we have day 42 of this Salesforce learning bootcamp. And today we will be discussing about DML statements and database class. Those are available in Apex. So today I will be completing this important topic which is related to Apex and tomorrow I will be having one more session where I will discuss some real-time scenarios that we'll be implementing with the help of Apex. Right? So I hope you are enjoying all the sessions and you are learning a lot. And I am just seeing all your reviews, those you are providing on YouTube comment sections on LinkedIn. So everywhere I am seeing positive feedback and people are learning, people are revising their skills. So I'm happy like this bootcamp is helping you. And this is the first phase of development bootcamp that we are going to cover that is Apex implementation. Right? So from next week we will be starting Apex trigger learning. Right? So just practice everything related to Apex so that you can just understand Apex trigger very well as well. Okay. So hello and welcome everybody on this channel. And I can see like this channel is becoming more and more popular. And I'm sure when one more bootcamp will be available free of cost for almost six weeks, that is cyber security. So maybe tomorrow I will announce everything about that. So we just need to do one testing. So today after this session, I will just do a testing like the consultant who will be delivering the session. He is on different location. So we'll test like how we can be available on the same platform. And from tomorrow I will be sharing all the details about this that bootcamp. Right? So those who don't know about me, you can go through this slide here. Everything about me is mentioned. Right? So you can see in the first line, it is mentioned. I'm founder of Sanjay Gupta Tech School. So right now on this tech school, I am delivering all the sessions. But now I just decided to take one more step so that I can build a community where I can collaborate with other tech persons, consultants who have lots of knowledge with them. So I'm going to make this platform a global platform. Right? So those who have some additional or excellent skills, so I'm just partnering with them. So I will be helping them to create their own YouTube channel so that they can share their knowledge. So I will be guiding them and they will be sharing their knowledge free of cost on my channel. So Sanjay Gupta Tech School will be a platform for them. So I will be helping them to grow individually as well as I will provide this platform so that they can showcase their skills and they can boost their own YouTube channel and overall benefit will be yours. Like you will be having all the knowledge free of cost, right? I won't be asking you to pay any single rupee or dollar, right? So everything is for you. You just need to give your time and efforts, right? So I hope you are happy with this initiative of mine. So soon you will be hearing different, different boot camps I will be arranging. And if you know anyone who want this platform to showcase their skills, they want to grow as a YouTuber or a trainer or instructor. So just tell them my name so that they can reach out to me and I can guide them to like design their journey and they can benefit the community free of cost, right? So I know like nowadays education has become a business but I am trying my bit to change or to break that chain, okay? So our main focus is on learning quality things, right? So I hope you are following all these learning best practices. I can see lots of, lots of folks are doing practice. You are asking questions in the telegram group, right? And people are answering them as well. So this is very good practice. So keep that up, right? So moving forward. So this is very important. So along with this Salesforce bootcamp, if you join that cybersecurity bootcamp, so again consistency is the key for everybody. So if you follow anything, so just follow it from the beginning to end and try to practice everything, right? So someone is asking that cybersecurity bootcamp will be related to Salesforce dedicated. Yeah. So it can be utilized for Salesforce ecosystem and other ecosystem as well. So nowadays what I am seeing freshers are not having enough opportunities. So what I did, I just asked my partner like please design that cybersecurity bootcamp in such a way so that folks can start freelancing as well, right? So there are lots of opportunities. So he will be sharing all the job upon all those jobs of job opportunities which are available in the field of cybersecurity. So you will be surely benefited with that. Okay. So we are in week 11 and in previous slide, like I didn't explain you. So we'll be doing all the explanation and demonstration. So I will be doing that. And then you can ask questions. So I will be answering those and with this session, like apex will be completed, but tomorrow's session is also important. Tomorrow I will be explaining only use cases. So whatever we have understood in these three weeks, so everything will be summarized and I will be implementing those features with the help of real time scenarios. Okay. So if you have not followed my initiative, Sanjay Gupta Tech School, so just follow it on all four social media platform so you so that you don't miss any notification, right? And please share it with the folks who really want to learn cybersecurity for free of cost, right? So please share everything and as I promised you, like cybersecurity bootcamp is on the way. And after that, I think I will be picking another one and like this will go on and on. So after this lots of talk, let's focus on the today's topic, which is DML in Apex. So this is very important topic if we talk about Apex. So till now in last two sessions, what we discussed, we discussed about SOQL and SOSL. So SOQL and SOSL basically focuses on the existing data. If you already have existing data in your org and if you want to query that, right? If you want to fetch that data. So for that purpose, we use SOQL and SOSL, right? Now let's talk about other part of the data. So if you want to insert some data, if you want to create some record through code, if you want to update existing records through code, if you want to delete any record through code, if you want to undelete any record through code. So for that purpose, we have DML. So DML stands for data manipulation language, right? So through DML, you can perform insert, update, delete and undelete operations, right? And we have one more operation that is known as absurd. So absurd is basically combination of insert or update. So if you want to do insert or update together, so instead of writing two separate DMLs, you can have only one that is known as absurd. And it is suggested like always perform DML in bulk. So I will explain you why because we have some governor limits. And we need to handle exceptions as well. So exceptions are basically runtime errors. So I will explain that as well. Okay. So whenever in interview, someone asks, like, what is DML? So you can just explain whatever is written in front of you. Okay. Now, one by one, we will be doing all the demonstrations together. So if you have laptop with you, you want to do it right away, you can. But if not, then you can just watch the recording. So right now you can just focus on understanding things and later on you can practice. And if you see the exercise sheet, so here, lots of self practice questions are available for yesterday's session. And you can see for today's sessions, I have provided lots of questions here that those you will be able to practice. Okay, so I can see lots of people joined this bootcamp. So this is very great. And now I'm just moving to our org so that we can practice. So here, I'm just refreshing this developer console as well. So first of all, what I'm going to demo you. So I will be demoing you like how we can insert the records through code. Okay, so here you can see first I will be demoing you how we can insert only one record. And then I will show you what are the drawbacks of inserting records separately and what is the benefit if we insert records and bulk. So we'll see governor limit check as well. Okay. So I'm coming here. So I'm closing all these steps so that we can open new apex class and there I will be demoing you. So I'm creating new apex class and I'm naming it as DML demo. Okay, and here I'm creating a method public static void and I'm naming it as insert demo. So right now I'm going to demo you how insert operation works. So I will be implementing all the statements. So for example, I'm taking account object for all the operations. So for example, I'm creating account instance. So this way I can create this. So if you remember the data type session where I explained you about s object, like how we can use s object as a data type. So this account is a s object, we are creating its variable and through new we are initializing it. Now what you need to do you need to fill the values for required fields. So required field is named. So here I am assigning test DML 101. So this is the name of account record. And in my org active field is also required. So I am going to populate that as well. So there may be possibilities in your org it is not required. But if you if I show you my org, so in my org it is required. So I'm just going to open the sales application. And here I'm going to show you this so so that you can see whether it is required or not. So you just need to verify in your org as well like what all fields are required. So here you can see this active is required. So that's why what I'm doing I'm populating it here as yes. And then I'm writing insert ACC. So this way basically I can insert a new record. So new record will be inserted in the org and you will be able to see that. Okay. But what I'm going to show you one more thing like whenever you insert any record. So immediately the variable that you are using that will be having the newly inserted record ID as well. Okay. So here what I'm going to write account ID equals to and then plus ACC dot ID. Okay. So this way if you run this method so new account will be created and that new account ID will be available in the logs with this system dot debug. So I'm going to show you both. So here I'm going to run this. So copying the name of class dot copying the name of method and then opening and closing parenthesis. Now I'm going to run it. So first of all I will show you whether this record is created in the org or not. So I'm moving here and refreshing the page. So here you can see this test DML one zero one is created. If I go to details. So here it is. And if we check the created and last modified date. So it is today's. So right now it is showing PST time zone. Okay. And active is having us. So this way record is created. Now if I move to logs and just select debug only. So here you will see the ID as well. So this is the record ID. Okay. And if you want to compare this record ID. So if you go here. So here is it. Here it is. So in the URL you can have that record ID. If you copy it and if you find so you can see it is matching. This record ID is matching. So the account record which is created in the org that account records ID will be available with that ACC variable that is available here. Okay. So whenever you process any code where you are creating any record. So you just need to remember that code will be having the newly created records ID as well. Okay. Now I'm going to show you what if we want to create two records. So what I did I just explained you how to insert one record. I showed you how we can fetch ID just after insertion of record. Right. So these two things I already explained you then next we have how to insert records in bulk using list. So before moving to that I'm going to show you governor limit check. So I'm going to insert two records separately and then I will be inserting those two records through list so that you can have bulk demo as well. Okay. So what I'm going to do I'm just copying it and pasting it here and changing it to ACC one. Okay. So here how many DMS we have. This is first DML and this is second DML. So this insert statement will be known as DML. Okay. This is DML number one and this is DML number two. Now I already told you how to check governor limits. Okay. So I am just saving this code. Now here I am changing the name so it will be one zero two and one zero three because both the records will be inserted in the org as well. Okay. So if I run this code if I save it and if I run this code so I am going to open the logs so that I can show you the governor limits. So I'm just going to click on execute. So I'm just waiting so that this class can be saved. So it is saved now. Now I'm just clicking on execute. So it is executed. Now logs are opening and I'm scrolling down. So lots of operations happened. So you need to just go to at the end. And here you can see total number of DML statements are four. So two we executed and two executed through any other process. So if I go above so you will find all the DML statements. So you just need to go above and here you will able to find those. So what you can do you can just try to search insert. Right. So here you can see insert operations are performing. So to perform through any other process and to perform because we implemented insert statements. Right. So that's why here four DML statements are available. Right. Now how you can fix it how you can reduce number of DML statements. So answer is try to create a list. Okay. Try to create a list. So before creation of list I'm going to show you this test DML one zero two and test DML one zero through one zero three is created as a record in the org. So here these are one zero two and one zero three. Okay. Both are created together but they are utilizing two DML statements. So now if you want to fix that so you need to create a list. So we can create account list. Now instead of writing insert ACC you can write ACC list dot add and then ACC. And here also what you can do you can write ACC list dot add ACC one. So this way both records are added into the list. Now what you can do you can just write insert ACC list. So this is the appropriate way to insert the records and it is known as bulk insertion. So if you add all the records into a list so it will be treated as bulk insertion. So now I'm changing its name for and five I'm saving the code. So earlier you saw total number of DML statements were four. Now they will be reduced. So I'm executing the code again. Logs will be opened. I'm just checking debug only and here we don't Oh sorry I don't need to check debug only because we don't have any system debug. We just need to scroll down to end. And here you can see number of DML statements are two. Okay. From four they are automatically reduced to two because we are using bulk DML. Right. So this way this is the appropriate way to write DML statement. So what you need to try you need to try less number of DML statements that you can use in your code. Okay. So for that you will be always using collections like list set or map whatever is suitable and always remember the similar way when we apply a SQL and result will be in form of list. So similarly DML statements whether it is insert update delete undelete absurd these DML statements you can apply on list only. Okay. DML statements you cannot apply on set or map they can be applied on list only. Right. So I hope you understood how we can check the governor limits how we can bulkify our code. Now last I just need to show you how we can insert related records. So what I'm going to do I will be inserting account and contact records together. Okay. So I'm going to show you that thing. So I'm going to create list of contact that is con list equals to new list of context. So what I'm going to do along with account creation I am going to create one related contact as well for both the accounts. So one account will be there and related contact will be created as well. So what we can do after adding this account into the list. So I can create a contact. So contact con equals to new contact. Then I'm writing con dot first name equals to so let's say what I'm doing. I'm writing a CC dot name. Okay. And con dot last name equals to a CC dot name. So both first name and last name will be account name. Okay. Now con dot account ID. So if you want to relate your newly created contact with the account so what you need to write you need to pass that a CC ID into this con dot account ID. So this account ID is a lookup field that will be receiving the ID of your account that we created above. Now after this you can write con list dot add and then con. Okay. So what happened here we created account and added account into the account list. Then we created contact connected contact with the account and added the contact instance into the con list. Now we need to do same steps for second account. So I'm creating one more contact con one equals to new contact. Then con one dot first name equals to a CC one dot name. Then con do con to sorry con one dot last name equals to a CC one dot name. Then con one dot account ID equals to a CC one dot ID. And then we can add this con one into the contact list. Now I can write insert con list. So this way, along with two accounts, two contacts will also be inserted. Right. So I'm just saving this code. And if I execute this, so I'm just changing these names. So it will be six and it will be seven. Right. So if I go here, so I'm going to show you four and five, those are created. If I open four five. So here there is no contact, no related contact is available. But now the records which I will be inserting whose name will be 106 and 107. They will be having one related contact created automatically. Okay, so I'm going to execute this. And if I go here and refresh, so you can see six and seven two records are available. If I open six, okay, it is not created. I'm just refreshing. So I just need to check what happened. Okay, so I just did a mistake. Instead of writing this insert a CC list, I just need to write it above. Okay, so what I'm going to do, I'm just changing this code little bit because we need IDs to link. So what we need to do first, we need to insert all the accounts and then we can insert the contacts. Okay, so just see the arrangement again. So this is first account. This is second account. Then I'm inserting account list. Okay, and the IDs will be available into this ACC list now. So what I'm going to do, I'm changing it to this ACC list of zero. So you I hope you remember whenever we create a list. So list basically contains indexes. So the first record is available on zero index that is ACC. If you want to receive that ID, so you need to write ACC list zero dot ID. And here you need to write ACC list one dot ID. So now it is correct. Okay, then we need to insert contacts now they will be connected. Okay, so first we need to insert all the accounts then only we can insert the related contacts. Now if I save it and execute it, so before that I'm going to change the names. So it will be eight and it will be nine. So I'm going to execute it. And if I go here and refresh, so eight and nine are available if I open it. So now you can see in the related list of contact is created. Okay, earlier also contact created but it was not linked. So you can see for six and seven contact is created but it is not connected with any account, right? Because we place the code in different order incorrect order. So I just corrected that. So this way testing is also important. Guys, so remember whenever you implement the code. So after implementation, no matter how pro you are in development, you always need to rely on the testing, right? So that's why in, in an organization where like a project is available. So we have developers and testers. So for tester QA role is available, right? So development do development and QA just test whether that functionality is implemented correctly or not. Okay, so right now I'm doing both the roles and you are also doing that because whenever you implement anything. So I'm sure you are testing it as well. So this way I hope you understood how we can insert related records. Okay, before moving further, I'm going to show you one more thing. Like if you want to insert more than two records, how you can do that? Because this way code is very lengthy. If you want to insert 50 records, 200 records, so you won't be able to write them again and again. So in that case, what we can do, we can just implement a loop. Okay, we can implement a loop. So I'm just going to show you that. So I'm commenting this code. I just commented all the code and commenting this line as well. So here what I'm going to do. I'm creating a loop. So integer i equals to one. So you all know like governor limit is 150. Governor limit for DML operation is 150. I'm going to insert 200 records. I'm going to insert 200 records. So I'm just cop cutting it and pasting it, right? And I'm commenting this as well. And I'm going to write insert ACC here. So you can see inside loop I am writing DML, which is incorrect. But knowingly, I'm writing it so that I can show you how error will be coming. Okay, so guys, please focus on this. If you are watching live, then also if you are watching the recording, then also please understand it carefully. So right now I'm writing DML inside the loop, which is against best practice. But I am knowingly right writing it so that I can show you how you will see the error message. Okay, and rest of the code is commented. Only these lines will be executed. So loop will iterate 200 times and 200 records we are trying to insert. So let's see what happens. And don't forget to save your code after the updation. So I'm just saving it. Now it is saved. I'm going to run it. Clicking on execute. And here you can see, okay, it is showing duplicate record error. So I just need to fix it. So what I'm going to do test DML and here I'm writing plus I so what will happen? The name of account will become test DML one test DML two. This I will be concatenated with this name so that unique names will be created. So this is the way you can create unique names for account creation because we have duplicate rule created. Okay, so I'm just saving this code. Now I'm going to run it. And you will see an error message that that will be related to the violation of governor limit. So it is taking a lot of time. So here you can see this is the error message line number 10 is showing the error. So at line number 10, we are having this insert ACC inside the loop. And what is the error message too many DML statement 151. So limit is 150 loop is executing for 200 times. So we are crossing 150 limit. So that's why it is showing error on 150 first time. The similar error we show while using SOQL inside the loop. So for SOQL limit is 100, right? So these are very important interview questions. And this way you can try to explain the answer for these questions. Okay, now what I'm going to do instead of writing insert here, I'm just erasing it. And here I am writing acclist.add and ACC. Okay, before executing this, I'm going to show you how many account records I have. So all accounts and if I scroll down, so I have total 55 account records, right? I have total 55 account records. Now I'm just adding these accounts into acclist. And I'm uncommenting this line. I'm uncommenting this line so that it can be executed. Okay, so I'm just saving it. So now you can see inside loop, we don't have any DML statement. All the account records are added into acclist. And after loop, this acclist will be executed. Sorry, so I don't need to write it like this. I will be writing it like this insert acclist. So this is correct. Okay, so 200 records will be inserted and they will be available under all. So again, let's see total number of records are 55. Now after execution, it will become 255. So I'm just saving the code and going to execute it. So it is going to execute. Okay, again, duplicate error is available. So I just need to do some changes. So let's see what happens now. Okay, now it is executed completely. And if we see the number of DML, so it will be long because we inserted 200 records. So you just need to scroll. So from right hand side you can just pick this and you can scroll it at the bottom. And here you can see number of DML is two. We inserted 200 records till number of DML is two. And if I take you here under accounts, so you will find 255 records. So here you can see test DML test 1234 up to 200 you will find. So all these 200 records are created. So till here you can see 200 and then rest records. So total number of records will be 255. Here it is. So if you see last, it is 255. Okay, so this way with the help of loop, you will be able to insert bulk records. So I hope I hope you understood all the concept behind DML. And I guess you understood everything properly, right? And now I'm jumping to the next step, which is update record. So it is very simple. You just need to query existing records. And then you can update those records and then you can apply update DML. Right? So now I'm going to implement another method. So it it was for insert demo. Now I'm going to create one more method that will be for update demo. Now here I'm again creating a list. So I'm just copy pasting so that I can save some time. Then ACC list equals to here I'm writing select ID name rating from account where rating equals to hot. Okay, so what I'm trying to do, I'm just fetching all the records where rating is hot. So this is simple as SOQL. And if you are following all the sessions of the bootcamp, so I'm sure you know what is SOQL, right? So this SOQL will return all the records where rating is hot. Okay, so let's see. So now how to test these type of examples. So you can just copy this SOQL. And here you can paste it and just execute it. So you can see total 16 records are available where rating is hot. Total 16 records are available where rating is hot. So I'm just fetching their industry as well. So rating is hot and few are having industry as media and few are having some other. Okay, now I'm going to update. So what I'm going to do first of all, I'm implementing a loop. And before implementation of this loop, I can just have null check also. So if not ACC list dot is empty, right? So if this list is not empty, then only I just want to implement this loop. Okay, now here what I want to do, I just want to change the industry. So wherever rating is hot, I just want rating should be media. Wherever rating is hot, whatever records are available in this ACC list. I want all the records industry should be media. So right now how many records we have 16, but all records are not having industry as media, but I want to update them. So this way I just updated this. So if you change the value of ACC, it means you are updating this account list. Now after this, you can have one more if condition or what you can do after this loop, you can write update ACC list. Right? So remember, after completion of this loop, we are writing this DML. This DML is not inside the loop. It is outside the loop. Okay. So now I'm going to execute it. So this is another method which is update demo. So this update demo will be querying total 16 records where rating is hot and all those 16 records will be updated. Their industry will be media. So I'm just going to save this code first. So it is saving here. And I can see someone is asking why it is showing two DML Manan. So basically in my or some other process, maybe flow or trigger is already implemented. So that's why it is two, right? But for our it is one. In your case, if you practice it, you will see only one DML. Okay, so don't get confused with that. So it is saved. Now I'm going to execute it. So changing it to update demo and executing it. Okay, I'm missing required field. So one more thing that you need to follow. So if you are querying existing record, and on that existing record, if any required field is not populated, and if you are updating that record, so you need to populate that required field as well. So this active is required field. So we need to populate that as well. So here I'm going to populate that field also. Okay, otherwise our records won't be saved. So I'm repeating it again. If you query any existing record, and on that existing record, if required field value is not populated, then you cannot update that record without populating that required field value. So in our case, in my or active field is required. So I'm just populating it with yes. Okay, so I'm just saving it. And now I'm going to execute it. So it is executed locks are opening. So again, what I'm going to do, I'm doing nothing. I'm just moving here to my query editor. So I just executed update DML. After that, records are updated in the org, but I just want to check them. So again, you can just run the same SOQL just click on execute, you will see the results again, and you can just click on refresh grid. So here you can see. So just click on refresh grid. And here all industry is media, wherever reading is hot industry became media. So this way, all together, you can update your existing data. Okay, so such and in the first time it was not updated. You saw it was showing error. Then when I wrote this statement active equals to yes, then I executed again, and then it executed. So I think in the live session, also, you can go back and you can just watch that thing. Okay, so this way, I hope you understood how we can update existing records. And for your practice, if you go to the exercise sheet, so here you will find lots of questions. So these are very much important to practice. Okay, so session number 40 session number 41 session number 42, all three sessions are very much important if you want to master in Apex, SOQL and DML. So total 15 use cases are available. So do practice these use cases. And if you encounter any problem in any of the use case, just throw the requirement in the telegram group. And I am sure many folks will be able to solve them and they will be sending the answers to you so that you can understand that. Okay, so moving forward. Now we have absurd. So absurd is basically combination of both record insertion and updation. Okay, so I'm going to show you that as well. So here you can see we are updating this list. Now what I'm going to do, I'm again querying all the records where rating is hot. And this time I'm just updating them with another industry that is energy. And along with I'm going to insert few records as well. So what I'm going to do, I'm just copying this code and I'm pasting it here. So just see it carefully. So first of all, here, SOQL is written. SOQL is querying the records where rating is hot. Then this block will be updating industry as energy. So instead of writing update here, I'm just removing it. Then I'm having this loop which will be creating for example, five records. And here I'm writing test absurd, right? And I'm adding them into ACC list. So here we are updating the account list. Here we are inserting adding some accounts into ACC list. Now after this, I can just write if ACC list is not empty, then I can write absurd ACC list. Okay, so what will happen? These records will be updated to energy and five records will be inserted automatically, right? So I'm just saving this code. Now what I'm going to do from here, I'm just running this SOQL so that we can see all the records here industries media, right? And total number of records are 255, I guess, if I go here, and if I scroll to the bottom, so total number of records we need to check. So total number of records are 255, right? Now I'm going to run this code, which is containing both update and absurd. Sorry, update or insert and I'm writing absurd DML. Okay, so this is the code you won't see any update or insert DML. So I'm going to call this method clicking on execute. So it got executed. Now I'm going to run this SOQL clicking on execute clicking on refresh grid. So you can see industry converted into energy. Wherever rating is hot industry is energy. Now let me go here and refresh. So I want to see five more records. So here they are test absurd 12345. And if I go down, so total number of records will be 260. So here you can see total number of records are 260. So five records got inserted and 16 records got updated. So this way you can apply absurd statement. Okay, so this way in whole screen, you can see the code. So this is the method which I implemented for absurd. So above part is SOQL in the beginning, then the first block line number seven to line number 12 is for update line number 13 to line number 18 is for insert. And while we did all the operation then after all the statements, I'm just writing absurd statement so that all the data will be updated or inserted. So how absurd works. So the record which are having ID, the records which are having ID, so they will be updated. And the records which are not having ID, they will be inserted. So here whatever records we are creating, they are not having any ID, so they will be inserted. But here, whatever records we are querying, they are having ID, so they will be updated. Right. So I hope this way you understood this as well. And I can see lots of questions are available in the chat. So once I complete my explanation, so I will be explaining each and every question in detail. So if you have asked any question, so don't leave the session until I answer your question, right? So I have all the questions listed here, so I will be answering them one by one. So next we have delete. So it is also very simple. You just need to query the data and you need to apply delete DML. And whenever you delete the records, they will automatically move to the recycle bin. So if you want to verify your deleted record, so you just need to go to recycle bin. Okay, so now I'm going to show you that with another method. So public static void, then delete demo. So what I'm going to do, I'm just trying to query some records. So I'm just having this list as well. So I have a countless then I'm querying some records. So what I'm doing, I am just going to query the records where rating is hot. So we already know we have total 16 records where rating is hot. So what I can do after querying, I can just write delete ACC list. That's it. So this is the method which we need to execute. So just create a list, query the data which you want to delete and then apply delete DML. And what you can do, you can just apply null check. So null check is very important. Why it is important because there may be chances when you apply SOQL. And if SOQL doesn't find any data, so your list will be empty. And if you apply DML statement on empty list, so you will encounter null pointer exception. Okay, so this way it is correct. Now, before executing this code, I am moving here and I'm going to show you recycle bin. So from here under app launcher, you can find recycle bin. So under recycle bin, we have lots of records. And so here already we have deleted records. So let's see the number of deleted records. So we have total 200 records, right? And they were deleted on 25 March. All these records were deleted on 25 March. So I'm going to delete more records and they will be part of this. And if you want to restore, we have this button, you can just check those records that you want to restore, they will be and if you want to empty recycle bin, you can. And if you want to delete them one by one, so delete button is also available. Okay, and I think after 15 days, these records will be deleted automatically. So maybe 15 or 30, I'm not sure. So you can just search on Salesforce documentation, you will find that. So now I'm going to execute this code for you. Okay, so I will be executing this piece through this delete demo method. So I'm just going to save it. So before execution, don't forget to save your code. So here I'm writing delete demo and I'm clicking on execute. Okay, so there is one issue, like if any account is having any case related to that account, so you won't be able to delete the account record, right? And if any account record is having any error, so a whole process will be rolled back. So nothing got deleted, right? Nothing got deleted. So what I need to do, I just need to change this SOQL. Okay, so what I'm going to do moving to accounts, and I'm going to delete these five records test absurd one, right? So what I'm going to do, I will be using where name like. So here I'm writing test absurd, and then post it, right? If name is like this, so if name is starting with test absurd, and if it is having 12345 anything, so all those records will be deleted. So this way you can change your SOQL as per the requirement. So now I'm going to run it. So it got executed. Now let's see what is available in the recycle bin. So I'm coming here. So here you can see all five records are available. And they are deleted today. So this this whole data is sorted basis on deleted date. So those those five records are deleted right now and rest of the records are deleted earlier, right? So this way, I hope you understood how we can implement delete. So as per your SOQL, whatever records are available in this list, those will be deleted. Okay, so someone is asking what if we write a CC list dot size greater than zero. So my answer will be that is also correct. So what I can do, I can just show you that as well. So you can comment it and I'm just copying it and pasting it after this. So you can just check null through this way as well. Both are okay. If you write this, then also it is fine. If you write is empty method, then also it is fine. But here remember, you need to use this not operator. Here you don't need to use. So both are correct. Okay, so now next is how we can undelete the records, the records which we deleted right now, how we can undelete them. So it is very simple. So undelete is very simple. So now I'm going to show you undelete. So what you need to do, the same SOQL, which you applied for delete operation, you need to use the same DML. Sorry, you need to use the same SOQL and you just need to use all rows here. What you need to add all rows. So if you add all rows, what does it mean? It means your SOQL will be searching for the records in recycle bin as well. Okay, if your records are available in recycle bin, then those records will be queried into this ACC list. If this ACC list is not null, what you can do, you can just write undelete ACC list. Okay, so what will happen? Whatever records you deleted, they will be undeleted. So it means those records will be restored automatically from recycle bin to your org. Okay, so I'm just going to save it. And I'm going to execute it. So before that, I just want to show you like these five records are available here in this recycle bin. Now if I run this code, they will be undeleted. I just executed. If I go here and refresh it. So you can see five records are gone. And if I go to accounts, they will be available here. Okay, so this way you can just undelete the deleted records as well. Okay, now comes DML statement. So sorry, DML statement exception, like we need to understand how this exception handling basically works, right? So first, we need to understand what exception handling is. So you saw, like whenever I executed, so due to some required field, or due to some duplicate record creation, you saw some errors. So those are runtime errors, those are known as exceptions, right? So because of that exception, your whole code will break. Because of those exceptions, your whole code will break. So if you don't want to break your whole code due to that particular error, so what you can do, you can just use try and catch block to implement exception handling, right, you can implement try and catch block to implement exception handling, right? So I'm going to show you how we can implement exception handling. Okay, so if I take you here in the code, okay, so here you can see if I show you this code for update demo. So this is update demo, right? What we are trying to do here, we are trying to update some records, and here we are trying to create some records. Now, knowingly, I'm just commenting this line. Knowingly, I'm commenting this line, and let's let's remove this. So by mistake, you forgot to provide that required field value, right? Now, you have a piece of code where you think error may come or error may not come. But because of this error, because of this piece of code, your whole part will be broken. Your whole piece will be broken. So what you can do here, you can write try. And this loop you can shift inside try. Okay, and after this try, you can write catch dml exception e, and then here you can write system dot debug. Okay, so I think there was some internet issues. Now, it is working fine. So basically, what we need to do. Okay, so here it is showing some error. So let me just check it. So okay, it is showing error for catch. So basically, here it is for if so I'm just checking why it is showing the error. I think I used this if statement maybe because of that. So let me just cut it from here and let me save it now. Okay, instead of writing this loop, let me just try writing this account this way. Okay, still it is showing. So let me just use exception only. Okay, still it is showing error. So I'm just copying this and moving to anonymous window. Let's see whether it works here or not. So syntax is correct. Don't know why it is showing that error. Okay, so here what I'm trying to do, I'm trying to create an account record. And I'm not populating active as yes, right? Yeah. Yes, I think when you just got the error, actually, this file is like it is saving it, it got stuck. So that's why it is showing this. So I'm just closing it and I'm going to open it again. Yes. Now, here you can see it is not showing any error. Okay, so what I was trying to do here, this this piece of code is having a loop and here we are having insert statement, right? And we know name is not populated, so it will break. So that's why this try catch is available here. And for above code, I am writing update statement here. So update a CC list, right? So this piece of code I want to be executed, no matter whether this try block executes or not. So right now, this code is having two parts. This code is having two parts. So the above one is for update this this one, this one is for update and the below one is for insert. So if insert one is breaking still, this above one will work successfully. So this way basically we implement exception handling, right? So exception handling means if a piece of code may or may not break on runtime. So if you are not sure, so you can just apply try and catch block around that piece of code. So what will happen if you apply try and catch? So your whole code whole method won't be breaking that particular piece will be breaking. Okay, and you won't see any error on the UI because that will be controlled with the help of this try and catch and the error message you will find under the locks. So I'm going to save this and going to run this update demo. So name of classes dml demo dot update demo, right? So you will see I didn't provide account name still, it won't show any error. So if I execute, it is executed. But if I go here under debug only, so here you will you will see insert failed. And this error is coming. But update happened successfully. Because of this update happened successfully. But try catch just handled this error, right? So this way you will be able to understand it. And if your screen is blurred, so just refresh your page. So you will be able to see that. So I think this way you are able to understand like how we use try and catch. But if you have this chance of having error in particular piece of code or like in particular list, if you have lots of records, and few records are failing and few are successful. So in that case, we don't prefer dml statement. Instead, we go with the database methods in Apex, right? So now I'm going to explain this quickly. So this is the last topic for today's session. And it is very much important. So wherever you won't be able to implement dml statement, so the solution is database database class provides methods which can perform dml operations. And these are the operations insert update absurd delete. So whatever we can perform this parameter allows you to specify whether the operation should partially succeed or not. So what will happen? Suppose you are inserting a list of record. So for example, that list is containing total four records. And out of those four records, if one is failing, so in case of dml, all other three records will also fail. Now your requirement is if one record is failing. So in that case, you won't like all other three records should be successful. Right? So in that case, what you can do, you can just use database class is set to false. If error occurs on a partial set of records, then the successful records will be committed and errors will be returned for the failed records. Right? So this is very much important. So that's why only for this purpose, we go with the database class methods. So no exceptions are thrown with the partial success option. This feature is not available with dml statement. Right? So this is very much important to understand this, right? And like in interview, normally this question is being asked, like, what is the difference between dml and database class? So this is the difference like in dml. If any record is failing through the list, so all the records will fail. But in case of database class, if any record is failing, so only those records will fail and other records will be successfully committed. So if I go further, so this is the example, right? So database dot insert, then this is the list that you need to pass and then we have false parameter. So the database method returns result object containing success or failure information of each record for insert and update operation and array of database dot save result objects are returned. So this way you will be writing the code. Okay, so first of all, I'm going to show you this demo so that you can understand and then we'll discuss the theoretical part. Okay, so I'm going to give you database class demo. Okay, just go through this code carefully. So right now I'm not using dml. Sorry, I'm not using database class method, I'm using dml statement. It is a dml statement. Right? It is a dml statement. And here you can see just go through this here through curly basis, I'm just initializing these records. So this is another way to set the data. So for every instance, I'm using new here. So total total four contacts I'm trying to create for three, I just provided first name, last name department. But for fourth one, I didn't provide any value to any field, right? So we are sure this contact won't be inserted because last name is required. But other three should be inserted. But what is happening? I'm using dml statement. So in case of dml statement, if inside this list, any one record is failing. So all those all other records also fails. Right? So now if I execute this, I'm just saving it, you will see an error message. If I execute this. So what will happen? You will see an error message. Required field missing line number 12. So while I'm performing insert operation, so required field is missing. So whole operation is rolled back. No, no record is inserted. Right? So this is a drawback of dml. I hope you understood. So again, I'm repeating if from a list, any one record is failing. So all other records will also fail. So the solution to this problem is to use database class method. Okay, so now I'm going to show you how you can use that. So I'm commenting it. And after this, I'm going to paste few lines of code. So you will be able to understand that. Okay, now we will be understanding it line by line. So I'm just removing this. Okay, so here you can see this is the list where we just created the data. Now here, what I'm doing, I'm just using this database dot insert. So what is happening here in this database dot insert? We are just passing con list and additional parameter that is false. So if you pass this parameter, then only if any record is failing in the list, so that only that record will fail, other records will be successfully inserted or updated whatever operation you are performing. So here we are performing insert. So these three records will be inserted successfully. But the fourth one will fail. Okay, and how we can process it. So the result will be available into this SR list. It is of type database dot save result. You need to use this always if you are inserting and updating. Okay, that's what is written here. So if you read this, now you will be able to understand it. The database method return result object containing success or failure information of each record for insert and update operation and array of database dot save result objects are returned. So this way you need to write it. And if you want to write absurd, so you will be using it like this. Database dot absurd and here written type will be database dot absurd result. And for delete, it will be database dot delete result. Okay, now let's understand this code this for you. So what is happening here? The order which you are having for this list in the same order, records will be inserted. So first three will be passed for first three will be successfully inserted. Fourth one will fail. So in the same way, the success and failure information will be available into this as our list. So we are iterating on this as our list one by one each record will go to this as our if it is having success. So we can just check it with this is success method. So if it is successful, what we are trying to do, we are just showing its ID, right? If it is failing, it will go to else. And inside else, what we are trying to do, we are getting the error messages. And here one more loop is available, which will show all the error messages with the help of this err variable. So we are getting the error status code. And we are getting the error message as well. And the affected field, right? So this is this is the set code that you need to use. What you need to change, you need to change the list. And rest of the code will be same. You can just reuse this code every time whenever you are trying to use database class methods. Okay, now what I can do, I can just run this code. So I'm just first saving it. So it is saved. Now I'm going to run it. And three records will be inserted successfully, you will see their ID as well. So if I click on debug only. So here you can see three records are inserted successfully, you are having their IDs as well. And if I go here to context, so you will find those records. So if I see all records, okay, recently viewed in recently viewed, they were available. So at top, you can see these three, Caroline, Kathy and Joy. So they are available here. If we see the code. So these are the names, right? And if I show you this error message, so here you can see line number 22, 23 and 24 are showing error message as well for one record. So three are inserted successfully and one is failing. So this way, if you want to track success and failure information, then you can just use database class methods. Okay, now this is also important. If you write insert contact list or so I don't know why it is breaking. So now I am just going to explain it again. So if you write insert contact list or database dot insert and you just pass contact list and if you pass contact list comma true. So all three statements are same. If you are seeing screen as blood, so just refresh the page so that it gets improved. Right? So what will happen? So these three statements are same. If something is failing, so it will throw an exception. And if you want to track success and failure information, so you just need to pass false as a second parameter. So here instead of true, you will be passing false like we did in the code. So here we just passed false here. Right? Now if I move forward, so here you can see we have DML statement or database method comparison. So this is important to understand. So here you can see it is written use DML statements if you want to throw error through apex exceptions and handle them with try and catch. Use database methods if you want to allow partial success of a bulk operation. In this way, successful records will be committed and error will be returned for the failed records. And database methods can also throw exception similar to DML statement. But in that case, you will be using true in place of this false. Right? So I hope this way you understood the differences between DML statement and database class methods. And I'm sorry for the internet issues like few few part. I hope you missed. So you can just go through the recording again if you missed. And still if you have any issues, so you can ask your questions through comment section of this video. And otherwise, you can just ask questions through the telegram group. Okay, and if you if you want this code to be available with you, so I'm just going to copy and pasting this code in our document. So here I'm going to paste this code. Okay, so so I just pasted it here so that you can just see it, right? So after the session, I will arrange it for you so that you can just copy and test it. Okay. So just to practice all these scenarios which are available here. And if you encounter any issue, so just ask me. So now I'm going to take all the questions. So I think today's session got a bit long. But I just wanted to cover all the topics. So tomorrow we will be doing all the scenario based questions. So if you have no question, you can just leave. Otherwise, you can be with me so that I can answer the questions. Okay, Sachin is asking why we prefer lists. So it is part of syntax. Like a picks doesn't allow set and map. So this is the way it is created. So Phyrus is asking, will you get error if you try to insert a record without required field? Yes. So we as asking, do we have record ID are similar in the real time? Also, yes, record, I record ID will be same. This this 00 format is auto number for record. This is not record ID. So Divya, I just use contact account name for context so that I don't need to hard code that contact name, right? So we are just reusing whatever account name we provided as a contact name. If you want to use different contact name, you can use that as well. So Manan, I just use capital F. So it all it is also right. So uppercase lower case doesn't matter. So I'm just checking if I get more questions here. So people are asking why I'm assigning same name in the context so you can have different name as well. So I just use account name because I didn't want to pass the different contact first name and last name. So that's why it depends on you. So Manan for your question, why is it showing D2DML because in my org, a few more processes are available. That's why those are also executing. But in your org, you will see one only. Yes, such in weekend parameterized list as well. So this we will do in the trigger. In trigger, we will be passing list as a parameter. So at that time, your answer will be validated. So I think Abhishek, this I already amode you database dot update like this is part of database class method. So I showed you database dot insert. Similarly, you can use database dot update as well. So if you if you if you're updating any record and your required field is not populated because it may be possible you mark that field required after few record creation. So in that case, you need to populate that value. Otherwise, you won't be able to update them. So Seeker, I already answered this like why two DML were there because in in my org, there is one another process like one trigger is already there. That's why it is counting two DML. If you practice this in your org, you will get one DML. So subject asking why we are not added ACC variable into ACC list. Because when when you apply this loop on update list. So if we come here, so you are right, you are iterating this list, and you are updating this record one by one. So here we don't need to add this ACC into list, because we already iterating on this list. So that's why we don't need to create another list, or we don't need to add this ACC into any list. So Sanjit asking, we have to update all required fields while using update DML. Yes, if those required fields are not having value populated already, if they are having value populated already, then you don't need to update them. So no, no, we cannot use absurd for two different objects. So absurd, you can apply for same object for insert and update. So Shiva, I think it will be available for 15 days, but still you can research on Google. So you will find some Salesforce official document. So first, I think I already answered this question. We can query data from recycle bin. So Seeker, in this case, like if a few records are already having active populated, so you can apply if condition. So you can write if active is equals to null, then only you can store yes, otherwise not. So this way you can apply conditional assignment. Yeah, so Rekha answered this question like you just need to see order of execution. So this is good because this way you will be able to understand like how many processes are there which executes. So if other other processes are also implemented, so they are also part of that transaction. So Vishal is asking about much. So generally, we don't use much frequently. So that's why I just skipped that. So if you want to learn about much, so you can just learn it through trailhead. It is available there. So this I already explained. Yes, so like in my org, I think it is required from page layout. If in your org, it is not required. So no need to worry if it is through validation rule, we can deactivate it as well. So Fizen, yes, integration sessions will be there. So integration, visual force, everything will be there. So I will be updating the Excel sheet. No need to worry. Yes, so I think such as showing best practice for deletion. Yeah, we create bad job because we do deletion on regular basis. So you can just create a bad job for that purpose. So we will be learning about as in chronosypics, then you will understand what bad what bad job is. So such in for unlinking the case from account, you need to go to that account and you need to just unlink that case by removing that account ID. Or maybe you can just create some automation through that if case is connected with particular account, so you can just unlink so you need to create some process for that. So you need to think but it can be done. So Sanjith, we cannot add contact in the account list. This is wrong. So you need to keep them separate. So context should be added into contact list not account not in the account list. Right? So people are asking again and again, same question I already answered. Okay, so so Vinay is asking which one should we use in real scenario like DML or database class? So if you have lots of data in your class, sorry, in your list, and you have some doubt like it can fail at any point of time. So you will prefer database class methods. But if you are sure like it won't fail, so you can use DML. But with DML, you can just use try and catch. So you can just answer an interview like DML only works with list. That's why it is like in the same way in Apex, it is actually implemented. So DML and database class methods works for list only. Yeah, so if you have map, so in that case, you can use map dot values for with DML statement, because map dot values will return the list. So someone is asking about database method and an asynchronous process. So both are different. Right? Both are different. So it depends on the business requirement. So right now, I cannot say which way you go. So it depends on the business requirement what you have. So accordingly, you just need to apply. So Sonu recursion, we will be discussing in trigger this commit and rollback. This is this is a simple concept. If you are DML operation is performed successfully, it means your record is committed. If it is showing any error, so it is rollbacked automatically. Yeah, best practices, we will be discussing once we complete the trigger part. So after trigger completion, I will explain all the apex and apex trigger best practices together. So because I just see to prepare those slides, those are not yet prepared, so it will take some time. So in the sheet I already updated. So apex scenario based on SQL and DML we will be doing tomorrow. And once trigger will be completed, so both apex and trigger best practices will be discussed together. So Sonu, you can use any number of try and catch. Yes, for us, we can bypass, but you need to for required field, we cannot bypass, but validation rules we can bypass. So you can just implement those workarounds. But if your field is required from the field itself, then you cannot bypass that. So Siva file and attachment concept is very easy. If you go to any record there, you will find a related list. So on that related list, you can just have this notes and arrangement, you can just add any file here. So those who are appreciating my patience, thank you for that. It took lots of guts to do these sessions free of cost with such efforts. So you can see I'm not drinking water as well. So just have patience and do consistent practice so that you can just utilize the benefit of these sessions. And by any chance, if I skipped any question, you can ask that again, because lots of questions are there. And I don't know how many are online those who asked the question. So if I missed your question, you can ask. Okay, so my is asking about telegram groups. So let me just share that link. So this is a self help group actually. So if you have any doubt, you can ask and if someone is knowing answer. And if I get some time, so I will try to answer your question. Yes, really, we can send email with attachment. We can send chatter post through apex or trigger. Yes, we can. So while working with trigger, we will see these scenarios. If you want to understand that early, so you can just go here and under trigger scenarios, you will find that. So here is the trigger and test class. So not here if you go to trigger. So here you can see we have one scenario send email notification through a big trigger. Right? So here is an example that you can go through. So shriveli we can write any number of methods in one class. Thank you, Sunil. Thank you for us. So we can use any numbers like 15 or 18 you can use any. So such in database class methods automatically handles the exceptions. That's why try and catch is not required. And by any chance if I missed any questions and if you are still online, you can ask. I will answer and let me see the progress. So today I have good progress. If we see the views so you can see total 543 views I got online and still I have 55 concurrent users. So good growing good. This actually boost me. I generally see this count so that I can make myself happy because I saw lots of live sessions and I can see 10, 20 folks joins those sessions. But I'm happy like my boot camp is going way better. And I can see only 34 likes. So those who are online with me right now, please like the session at least this will give me motivation to do more session with the energy. Okay, I think now you don't have any questions. So okay, so no is asking this process. So it is available freely. So we just need to search for order of execution. So Salesforce official document is available here. So here with this document, you will find trigger and order of execution. Everything is written over here. Right? All the steps are available. So I'm just going to share this document with you. Thank you, Shiva. Thank you, Shrivali. So shake her don't ask trigger related question because trigger we didn't discuss next week. We will be discussing about trigger. So then I will be able to answer. Okay, still I can see only 16 likes increase. So I can see 60 folks are online, almost 59. Please like and share this channel. I share telegram group. So if you are not part of that, please join that. I'm doing this only for you. Yeah, so order of execution works for everything. Whatever transaction you perform. So whenever you create update records, so that order of execution works actually. Okay, still if you have any question you can ask otherwise I'm going to end the session because I think today's session is the longest one. It crossed one hour 30 minutes. So but I'm happy like I'm able to give you enough knowledge. Now I'm feeling tired. So I'm just going to end this session. Okay, so I think there are no more questions. Thank you so much, everybody for joining this session. So tomorrow also we will be having one session for a big scenario. So do join that session. And after that, next week, we'll be starting trigger. Okay, and by any chance, if I missed your question, you can ask your question in telegram group, or you can just ask your question in the comment section of this video. Right. Thank you so much. See you tomorrow.