 Friends, I am Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. In this video, I'm going to discuss about database methods. Those are available in Apex. So, first of all, introduction. So in my previous video, I demonstrated you like how we can use a DML statements to perform insertion, updation, et cetera on various records or related to various S objects. So in this video, you will understand how we can use a database class to apply those operations, right? So here in first statement, you can see, it is saying Apex contains the build-in database class. The database class provides methods which can perform DML operations. The database class methods are static and called through database, sorry, called through name of the class. So you can call those methods like this. So database is the name of class and insert is the name of method. And this method is already defined as static. So that's why we are able to call this method directly with the name of class. So this way you can call database.insert, database.update and so on, right? So now we have two options to perform DML operations. So one is we can apply DMLs directly that I demoed in my previous video that is available in the playlist. In that DML operation, we just use insert update, but there is another option that is database class. So what will happen? So we need to focus on this slide that is showing points to remember. So in database methods, we have an option that is all or none parameter. So what is this parameter? So this parameter allows you to specify whether the operation should partially succeed or not, right? So when this parameter is set to false, then 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. So this is the advantage of using this database class. So let's say if you are inserting 10 records and in five records, there is some error and five records are not having any error. So those five records will be committed and the five records, those are having errors, they will be returned with the error message, but provided that you pass parameter as false and parameter is all or none. So if its value is false, then this will happen. Then no exceptions are thrown with the partial success option. And the last point says this feature is not available with the DML statement. So in DML, if error is available, then all the records won't be inserted or updated. But here in this database class, we can partially commit some records, those are successful and for the failed records, errors will be available. So now how we can use this? So you can read the syntax here. In the first statement, database.insert, then the list that we are going to insert and the second parameter should be false. Then you can see the database, a second statement, you can see the database methods, return result object containing success or failure information of each record. For insert and update operation and area of database.save result objects are returned. So last statement is showing the complete statement, to insert some records. So database.insert, record list comma false and it will return the array of type database.save result. And you can create a variable here in form of array. So that array will be storing the success and failure information, right? So now let me show you an example so that you can understand how we can use this. So here at the top, you can see I'm creating a list of contact and in this list, I am initializing three contexts. So first contact is having first name, last name, second contact is having first name, last name, but third contact is not having any value. So these two contexts will be inserted successfully but this contact will not be inserted because in case of contact, we need to provide last name compulsorily, that is a required field. So that is not provided here. So this contact insert will provide an error, right? So now all three contexts are available in this contact list. So here at line number eight, you can read this statement. We are using database.insert, then we are passing this contact list with all or none parameter as false. So if some records are successfully inserted, so those will be committed and if any error is available with particular records, so those success and failure information will be available in this SR list. So database.saveresult and here we have used this array notation and this is a save result list, right? Now you can iterate on this save result list. So one by one, each record will be available in this SR variable, which is of type database.saveresult and then you need to check its status through this method. So if SR.isSuccess, if it is true, it means SR is having information related to a first record and if it is successfully inserted, then this masses, system.debug will be printed in debug logs having a GAT ID. That means the record which we inserted, that records ID will be printed in the logs and if the record is not inserted successfully, so it means this condition is false. So control will transfer to else and here on SR, we are iterating for getting errors. So through get errors method, you can fetch all the errors. Those are available with this SR record that was failed to insert. So those will be available into ERR variable, so you can use any name, but its type is database.error. Then here you can see this message will print as it is. Then through ERR, we are fetching the get status code and then error message and then fields, right? So this way, if any record is having any error while inserting, so those errors will be displayed in the logs and if record is successfully inserted, so that record ID will be displayed. So this way you can apply this for loop so that you can identify like which record inserted successfully and which failed. So now I'm going to execute this. So debug logs will be opened. So I'm going to click on debug only. So here you can see line number 14 is showing successfully inserted context. So this is contact ID. Then successfully inserted contact, this is contact ID. Then for one record, that means this one for which we didn't provide it any field value. So for that, this error is showing, the following error has occurred, required field missing, required fields are missing, name of field is last name and contact fields that affected this error, that is last name. So this way the whole description of error you can display in debug log. So this way we can identify like which record failed which are successfully inserted. So this you can do with the help of database class method. So if record is completely correct, that is that can be inserted successfully. So those will be inserted successfully and the records which are having any error or any required fields are missing. So those won't be inserted and you can identify like what errors are available there. And later on you can use add error method as well. So that from UI if you are inserting any record and if that error is available. So on that record itself that error can be displayed. So that I will be showing you while we will be discussing triggers because if we are inserting record from UI, so we need a trigger so that this apex can be executed. So right now I'm directly executing this apex through this anonymous window. So you can create complete apex class, then method and in that method you can use this and through trigger we can execute this but right now we are focusing on the apex only. So this way I hope you understood a whole scenario how database class methods works. Now if you want to use absurd, so in case of absurd syntax is same but you need to use the method absurd. So database dot absurd then record list then false and here you can see earlier we were using database dot save result. So this is for insert and update but for absurd we have database dot absurd result. So that you need to use and for delete we have this delete method and the result that will be returning so that will be of type database dot delete result. So rest of the things are same. So absurd will perform both insert and update operations together. Delete will delete the record. So if records are successfully deleted or absurded so if success will be true otherwise it will be false. So you can fetch the errors. Those occurred while performing these operations. Now one more thing by default that all or none parameter is true, right? So these all are same. So in DML we use insert only like insert contact list. So in my previous video, if you was that so you can see this statement I demonstrated but in database class we use database dot insert. So if we pass only contact list or any list that we want to insert and we don't provide second parameter. So by default this is, sorry this all or none parameter will be true, right? Or you can write database dot insert contact list comma true. So all will throw an exception if a failure occurs. So at the bottom you can see the note all these three statements are same. So in database dot insert if you are passing through or not passing any second parameter. So this is similar to a normal DML statement that is insert and the contact list. So that won't be working like partial records those are successful will be committed and those records which are having errors so error will be generated. So that scenario won't be covering here. So it will be like if any error is available so it will throw an exception and the insertion or whatever operation you are performing that will be failed. So now which we need to choose DML statement or database methods. So first point says use DML statements if you want to throw error through apex exception and handle them with try and catch, right? Then use database methods if you want to allow partial success of a bulk operation in this way successful records will be committed and errors will be returned for the field records but remember that you need to provide a second parameter as false then this second statement will be true. And last statement database methods can allow can also throw exception similar to DML statement. So if we pass second parameter as true that I explained here. So in that case the database methods also can throw exception similar to DML statement. So this way I hope you understood how we can use database class methods to perform DML operations. So now you have two options either you can choose DML operations purely or you can perform DML operations through database class as well, right? So I hope you understood whatever I demonstrated in this video and you can go to this website where you can find all the Salesforce related videos created by me. Thank you for watching this video.