 Hello friends. I'm Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. This is my website, studysalesforce.com. So here you can find all the videos that I created on Salesforce together on a single platform. So in this video, I'm going to demonstrate how you can use database class and its add error method in Apex trigger, right? So let's first understand the scenario what we are going to do. So I have already implemented a trigger on account. So there are various operations like is before, is after, sorry, before insert, after insert, before update, after update, right? So we are going to focus on this one that is after update, sorry, after insert, right? So what it says, it says create related opportunity whenever an account record is created, right? So I have created account trigger handler. So here you can see this class is available, account trigger handler, and it is having all the methods. So I am just commenting these methods. So these methods won't be calling so that we can focus only on this one. So it will run in case of after insert. So it will call create related opportunity and trigger dot new context variable will be passed that will be having the new list of opportunities, sorry, new list of accounts. So what we want to do whenever an account record is created, we just want to create a related opportunity record automatically. So new list will be available in this method in this variable new list. Then I'm iterating on new list then creating one opportunity record and adding that opportunity in this opportunity to be inserted list. And then after completion of this loop and checking whether this list is empty or not and then if it is not empty so new opportunity list will be inserted, right? So this is the simple method that is called by the trigger. So I'm saving these changes, right? So right now it will execute properly and right now I'm using normal DML statement, not database class DML operation. So let me show you what happens. So if I try to create new account, so you will see related opportunity will be created automatically. So this is the list account 19 and I'm clicking on save. So as account record creates, you can see related opportunities created with the same name that we provided as account, right? So right now this is working fine. Now what we want to do, I'm just commenting this statement, right? So on a opportunity this close date is required field but I'm commenting it. I'm not populating this. So whenever we have any error like system validation or custom validation. So if we don't follow that validation, right? Like if you don't provide close date here, so that system validation will be fired. And in case if you don't provide any value or you don't obey the custom validation then also error will be there. So right now I'm saving this change. So I'm not providing a closed date. So now if I try to create any account record, so you will see an error message will be there. So right now I'm putting test as account name and clicking on save. So here you can see this trigger error is available here, right? This error is a bit technical, like account trigger exhibition of after insert caused by this, this, this and this and line numbers. So here you can see this is the error message that we want. Required fields are missing and field name, right? So this is the only error message that we want but it is showing all the technical details. So if this kind of error message is available for the user, so that is not good approach. So now we need to find out the solution so that we can show this specific error message only. So what we can do, so we have two options. If you're using DML, so you can apply try and catch block so that if any exception is available, so that operation will be rolled back and other operation can be executed successfully. Right, so let me first show you that thing. So I am putting this in try, right? And after try I'm implementing catch. DML exception E and system dot debug, E dot get message. Right, so if any exception will be there so that will be handled by this try catch and debug will be showing this message. So I'm saving this. So in that case you can see account will be inserted but opportunity won't be created. So again I'm going to create new account test ACC to zero so you will see error won't be coming account will be created but in related list you will see opportunities are not created opportunity closed it we didn't provide it so try catch handle that. So now if I go to locks so you will see that error message there. So let me open. So here, many locks are available so one by one you need to open them and you need to search for there. So here you can see insert field. The exception on row zero first required field missing and close it. So this is handled by the try catch. But again this is not the good approach because user is not aware like why opportunities not created. So now the solution for this problem is you can use database class insert method and then you can apply the adder method. Right, so now I'm going to show you how we can implement that. So here you can see I have implemented this code. So what I'm going to do, I'm a trading new list and for each account I'm creating this opportunity to be inserted list. Right, so both are in same order so zero in if in new list at zero index one account is available. So it's related opportunity will be available in on zero index on this list for one index. The new list one index account related opportunity will be available on one index. And here I'm using database dot insert passing this opportunity list and false as a parameter. So this is partial insert operation. So it will insert those records, which are successfully inserted and not having any kind of error. So if some records are having errors so those won't be inserted right so records which are proper, they will be inserted otherwise records won't be inserted. So the result will be available in this as our list, which is off a type database dot save insert so it is an array, and it contains all the success and error messages, right. So I'm iterating this as our list through this variable I, which is started from zero. So if new list zero index account related opportunity that is available on opportunity ID, sorry opportunity to be inserted list at on zero index. So if that failed. So we need to fetch that error message. So I started from zero. So on SR list zero dot is success. If it is false. I'm using not here. If it is false. It means that zero index opportunity is not inserted successfully. So to receive the errors, we are iterating this loop. So as our list zero dot get errors one by one all the errors will be available in this era, and those will be appended in this eight errors string after completion of this loop in new list that we are using here. So in new list zero index because zero index opportunity is not inserted. So in new list that I index will be having added and the message will be this. So we are extracting the specific error message and we are putting it in new list. Right. So we are creating new list. Sorry, we are inserting new accounts. So that's why we need to insert that error message in this new list only. So now this Lou will add errors in the new list as a particular opportunity is unable to insert. So now if I save this. So you need to track the indexes like new list and the SR list because whenever you apply database dot insert so in the same way of errors and failure results will be available. So whatever list you pass. So if zero index record is not inserted. So at zero index failure message will be available if one index record is successful. So on one index, our success message will be available. So that way it works. So now if I try to create new record. I put test here and click on save. So you will see a specific error message will be there. So here you can see the quiet fields are missing and close it. Right. And if you want to add on some masses like you want to show on opportunity. This required fields are missing so that you can also do so you can add this that specific message as well. So this way you can customize customizes as per your requirement but the concern is like if you are using trigger and you want like you want to extract the specific error message then you can use this database database dot insert or update or delete whatever you want. And accordingly you can iterate this list and you can fetch the specific error message and you can add them in the new list. So this way, you can use this database class insert method and you can use this add a method. So I hope this way you understood how we can use them together in case of a big trigger so that we can show specific error messages and normal dml statements through the technical error message. So this you can control with this add error and database class methods. So I hope you understood whatever I demonstrated in this video. And if you want to watch more Salesforce related videos so you can visit this website here you will find all the video links together. Those are created by me. Thank you for watching this video.