 Hello friends, I am Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. In this video, we are going to discuss about DML operations in Apex. So if you want to watch more Salesforce related videos, so you can go to this website and here I have placed all my YouTube video links together. So you can find them. Now starting with the introduction. So DML is used to insert, update, delete and undelete records. So if you want to insert any record related to any S object, if you want to update, delete and undelete those records so you can perform DML operations. You can use Upsert to either insert or update a record so that you can do for inserting and updating a record together. Then next is use merge when duplicate leads, contacts and accounts are there into one record. So you can merge those duplicates into one record, but this operation can be applied on leads, lead, contact and account object records, and other records are deleted and related records are reparented. So let's say you have two accounts, account one and account two. So you're going to merge account two with account one. So if any child are available with account two, so they will be reparented to account one. And account two will be deleted. So I will also demo you how this operation will be performed. So we will see insert, update, delete, undelete, Upsert and merge all the operations. Then remember this thing always perform DML in bulk so that you won't hit the governor limit. So this I will also show you. And last one is handle exception. So whenever you are performing any DML operation and there is any exception. So you can you can implement your DML with try and catch block. So if any exception is there so that will be handled automatically through catch block and related error will be displayed. So now we are moving to perform our first DML statement that is insert. So first time going to show you how we can insert on the one record, then we will fetch the newly inserted record ID. And then we will apply insertion of records using bulk. So with the help of list of S object. So moving to developer console. So I'm going to open an NMS window. So I will be executing all the quotes here only. So I won't be creating any pics class. So that I demoed in my previous videos as well. So here we just need to insert the record, update the record. So I'm going to use this. Only so as object account a cc equals to new account. Then I'm providing name as tech school. Then phone 123456. Right. So this way I created an instance of account s object. Now, after creation, I can write insert a cc right so always remember that I also mentioned here, you can read the note that is available at the bottom. So it says a check newly created records in org. Whenever you create records through this anonymous window. So those records will actually be created in your org. So I'm going to execute this. So I don't want to open the log. So I'm going to execute this. And then I will take you to the account object tab. And then I will see one account record will be created automatically. There you can see this tech school and phone number is 123456. So you can open this and you can see all the details. So we provided only two field details account name and phone rest fields of length. Right. So this way whenever you create records from here. So those will be actually reflected in the org. So this way I inserted only one record now I'm going to show you how we can fetch the ID. So for that I need to create one more record so I'm going to create a name as tech school one. So after insertion, we are going to show the ID so ACC ID equals to ACC dot ID. So, just after the creation of this account record, the ID will be available with this ACC variable and you can fetch that ID with dot ID. So I'm going to open this log so that we can see the ID and we will also match that ID with the actual record. So I'm clicking on debug only so here you can see this account ID is generated. So now I'm moving to account tab and here you can see tech school one record is created and it's ID is this. So at the end it is having KG RWS. So we can match it KG RWS. So this is the ID of this record. So this way, if you are creating a record. So just after insertion you can fetch the ID so that you can use this ID for further operations. Right. Then insert records in bulk using less. So I will show you the governor limits. So let me create two records. Right. So this will be ACC one, this will be ACC two, insert ACC one, insert ACC two. So this way two records will be inserted. Right. So I'm going to open debug log so that we can see DMLs. So here we are performing two DMLs for inserting two account records. And further we can replace this with the help of list. So first time executing this so two account will be created. So here if we scroll down. So, so here you can see for just after first insert here you can see number of DML statement one out of 150. So total limit is 150 so one is performed. Then one more record will be inserted. And here you can see number of DML statements two out of 150. So we are inserting two records. That's why it is showing like you perform two DML operations and two records will be created in under account objects. So text school two and three are created. Right. So now I'm going to show you how we can create these records with the help of list. So instead of inserting these two ACC one ACC two separately, you can create list of account ACC list equals to new list of account and you can add these into this list like this, and then you can write insert ACC list. So instead of inserting variable separately you can add them into a list and then together you can insert a particular list. So here I'm writing four and five. So I'm going to execute this again so two more records will be created, but this time we are using bulkification. And this is known as bulk insertion because we are inserting records with list. So here you can see at the end, number of DML operation one out of two and number of DML rows are two. So DML statement is one because we are inserting two records with the help of list of account. So I hope you understood how we can perform bulk operation in terms of insertion. I also demoed you how you can check governor limits. So if you insert two records separately, so two DML will be count. And if you insert two records through a list, then one DML will be count and you can insert as many records through that list. Right. And we also saw that newly created insert newly created records are available in the org. Now insert related records. So first we will be inserting account and then contact. So what I will do, I need to insert only one accounts I'm removing this, and I will be inserting ACC. And it will be text school six. Right. Now I'm going to create a contact. And here, first name, and last name, and account ID equals to ACC dot ID. And then you can write insert contact. So first account record will be inserted and then that account record ID will be placed into this field that is available on contact. So the lookup will be filled and contact will be linked with the account. So we are going to create text school six as account and this contact will be created. So I'm executing this. It is showing an error. So I just forgot to mention comma here. That's why it was showing error. So now I'm going to execute this. So you will see both account and contact will be created. Now I'm going to insert related records into account. So here you can see text school six account is created and in related you can see one contact is available. So name is Sanjay Gupta. And if you open this contact so here you can see account is also popular. So this way through apex you can insert related records as well. So I hope you understood all these things like how we can insert one record, how we can pass the ID of the inserted record, how we can insert records in bulk through list, then how we can check the governor limits and then how we can insert related records. Now moving further. So here we are going to perform update DML. So we will be querying an existing card and we will print its value then we will update the record and we will do update DML. Then again we will query the updated record and then we will compare it with system dot assert equal method so that we can validate the update. So now I'm going to query the existing record. So select ID, name, phone, where name equals to text school. So this information will be available into ACC so we can write system dot debug ACC plus ACC right. So whatever details we are querying will be available into ACC and we are going to display it. Then we can write ACC dot phone equals to so earlier we provided 123456 so now I'm going to provide this value. And we can write update ACC right so this will display current information then we are going to update the phone then we are applying update DML right now again I'm going to query this into updated account variable right and after this I'm going to apply system dot assert equal so here expected so updated let's say phone here we can write ACC dot phone and here we can say equal. I'm going to execute all these statements so I forward to check that checkbox so I'm going to open the log. So here you can see account detail is displayed right now these were equal that's why it didn't displayed so if if both are equal then this message won't be displayed. So what I did I query the account and then I displayed it so it was showing that previous value then I updated the phone. So right now ACC phone is having this value. So here I'm using ACC dot phone and here I'm using updated account dot phone so both are equal because here we have modified then updated the account and then we are querying the updated record. So that's why this won't displayed. So if we go to the account record by open the text school. So here in details you can see the phone is updated. Right so this way you can query existing records and you can change the value and you can apply the update DML so your records will be updated and you can apply this assert equal so that you can check whether both the values are same or not. If they are an equal then this message will be displayed so instead of equal we can write an equal here. So if it will be displayed so unequal will be displayed. Now moving further. So update related records. So for this. So now I'm raising these so I'm going to query contact and I'm going to query that contact whose name is Sanjay Gupta and account not not so. Select ID, first name, last name, phone and account dot phone. First name equals to Sanjay. Last name equals to account ID not equals to. Right so now I'm going to update contact dot equals to 123456. Then con dot phone so this way we are accessing account information through contact. So here I'm updating it as let's say 1111. Now what I need to do I need to update contact and I need to update con dot account so this way we can update related records. So first contact will be updated and then related account will be updated. So now I'm going to execute this. So going to contact so here you can see this is the contact. So here we updated 123456 that is available and this account is related with this. So here phone number will be 1111 four times one. Here you can see. So this way we updated related records. So I hope you understood how we can do this. So now moving further so we need to perform absurd operation so in case of absurd we can create one record and we can update the existing one. Right. So so what I'm going to do first I'm going to create new one so new account name equals to X school seven phone equals to 123456. So I'm going to be created and I'm going to query one count select equals to X school six and make it seven. Right. So I'm stopping it here then ACC two dot phone equals to 121212. So text school six phone number will be this so we are going to update this and because this is the existing record that we are querying and updating and ACC one will be creating new records. So now I'm going to create a list of account ACC list equals to new list of account ACC list dot ACC one ACC list dot ACC two. And now I'm writing absurd ACC list. So new records will be created and existing will be updated. So you will see text school seven will be created and text school six to provide space here. Text school six phone number will be one to one two. Right. So I'm going to execute this. We are spelling mistake. So executed now clicking on the account tab so you can see text school seven record is created and text school six phone number is one to one to one. So this way through this upset we created one record and we updated existing one. Right. So I hope this also you understood. Now the time for merging. So only lead contacted account records can be merged and you can merge up to three parts of same subject type. So if three leads are having same value so you can merge them. Then three contacts or three accounts and so two records will be deleted and they will be much to only one. Right. So, so here you can see what I did I queried to existing records. So in master account I query text school seven and in merge account I query text school six. So I'm going to merge this text school six account with text school seven. So let's see here. If I open text school seven so you will see it is not having any related contact but if I open text school six. So you will see it is having one related contact. So I'm merging text school six into text school seven. So this contact will be reparented and it will, it will be reparented with text school seven automatically. And for merging I'm using this merge statement DML so master account and merge account so this merge account will be merged with this master account. So I'm going to execute this moving to the orb clicking on the account tab. So here you can see text school six is deleted text school seven is available and if I click on this. So you can see this account that was earlier with text school six now it is with text school seven. So this way you can merge your accounts contacts already. Right. So remember that whenever you merge. So the merge record will be deleted, and those two or three records will convert into one record. So that you need to remember. So now our next thing is deletion of records so we need to query the record and then we can apply the delete DML. It will be easy. So let's say ACC and I'm going to query text school five. And here you can write ACC. Right. And if you are acquiring more than one record so better you can create a list of account. And then you can apply delete ACC. So you can click on execute. So if I go here click on account. So you can see text school five is also deleted. Right. So this way you can apply delete DML so that you can delete particular records that those are queried. Now, next is undelete. So what we can do insert a record delete that record now very deleted record using all rows and query and then apply undelete statement. Right. So right now we deleted text school five. So we can query deleted records using all rows, and then we can apply undelete statement right. So what I am going to do deleted account. Right. And here we can write all rows. Right. So this will query all the accounts whether they are deleted or undeleted so text school five. So the same account record will be queried and that will be available in this. So here we can apply undelete and delete ACC. So right now you can see a text school five is not available here. Now I'm going to execute this. And again I'm refreshing this. So the text school five is now these two. So from deleted, deleted records it, it, it queried and it is now undeleted so it is available to use. Right. So, you can see all the details which are available in this record. So this way I explained you or demoed you how we can undelete the deleted records. So I understood this. Now, next is DML statement exception. So we can apply try and catch to handle exceptions raised when DML operation fails. So now I'm going to tell you how you can apply exception so ACC equals to new. So I'm not providing any value to any field and I'm trying to insert ACC right. So here I'm writing try. I'm putting it inside this. And just after try you can apply catch. And here you can write DML exception E and system dot debug. So if any error will be available, DML exception error will be available. So that will be handle and that will be displayed through this. So it is a method. So use parenthesis as well. So I'm checking this checkbox because exception will be thrown. So I'm executing this debug log will be open. So I'm going to click on this debug only option. So here you can see error is shown insert fail first exception on row zero first error required field missing required fields are missing name. So it is showing the whole error message. So this way whenever you perform DML operation whether it is insert update absurd delete and delete. So always use try catch block so that if any exception is there so that you can handle that and you can show the error message so that you can identify what exact error is available while you are performing DML operation. So here I explained you all the DML operations along with the exception how to handle that. So this is my website so where I placed all video links together so you can visit that and learn Salesforce. So thank you for watching this video.