 Hello everyone. I'm Sanjay Gupta. Welcome to Sanjay Gupta Tech School. In this module, I'm going to demonstrate how to implement batch epics with an example. Let's understand the scenario which we need to implement with the help of batch epics. So the scenario is we need to populate account billing address on related context billing address. So for that, we need to create a batch epics and we need to ensure proper code coverage as well. So let's jump into developer console. So I'm going to create new epics class where I will be writing the batch epics code. So name of this class will be update contact mailing address. So you can name it as it is required in the scenario. And after implementation, you just need to implement interfaces. So I am writing implements. Okay. So database dot batchable, then S object and database dot stateful. Right. So these two interfaces I have implemented. So whenever you want to write any batch epics, so this database dot batchable interface you need to implement. And in angular brackets, you can write S object. And if you want to maintain the state of particular variable between different batches. So for that you will be using database dot stateful. Now inside this, I'm going to write the code. Okay. So let's shift it in the beginning. So I'm going to create one integer variable, naming it as record count, then public database dot query locator. So database dot query locator will be the return type of this start method. So in batch epics, we need to implement three methods, start, execute and finish. So I'm going to implement start method first. Now inside this bracket, I need to write database dot batchable context BC. And here I'm going to write return, get query. Okay. So database dot get query locator. So this is a method. And inside this bracket, I need to provide a query. So I'm going to write a string for that. So select ID. Now here I need to write all the fields that I want to copy. So billing street, billing city, billing state, billing postal code, right? And comma, then I'm going to use a plus sign. So if you want to write this in separate lines, so you need to concatenate it with the help of this plus sign. So again, in single quotes, I'm going to write select ID. So these fields, billing street, billing city, billing state, billing postal code are related to account. Now this SOQL will be paid into child. So now inside this bracket, inner bracket, I'm going to query contact related fields. So mailing street, mailing city, mailing state and mailing postal code from context. And I'm closing it here, then putting a plus sign. And here I'm writing from account where billing country equals to USA. So now I just need to write that USA in single code, but already the string in single code. So I just need to use slash single code and then USA then slash and then closing. Right? So this is basically known as escape sequence. So if you want to use single quotes, inside single quotes, so inner single quotes will be using the slash, right? And then it is closed here. Now putting a semicolon and this start method is closed here, right? Now after this start method, I'm going to write execute method. So public void execute. And here also I just need to put it. So I'm copying it and putting it here and list of account and ACC list. So whatever data will be queried from this SOQL. So that will be automatically returned from this start method and it will be stored into this ACC list. Now I'm going to get a list of contact. So all the updated contacts will be added into this list. So I'm going to create a loop. So this loop will be treated on this ACC list. Inside this loop, I'm going to write one more loop so that I can fetch related contacts. So ACC dot contacts. So as we applied parent to child SOQL, so ACC list is providing one by one account record to ACC. Through this ACC dot contacts, each contact will be available in con. So for one contact, so sorry for one account, all the contacts will be treated and here we can copy the values. So I can write con dot mailing street equals to ACC dot mailing street then con dot mailing city equals to ACC dot mailing city. Con dot mailing state equals to ACC dot. Sorry, here I need to use billing con dot. So this way all four fields of account are copied into contact. So now I can add this con into con list. So when this loop will be iterating for one particular account, like if one account is having five contacts, so this loop will iterate five times and five contacts, mailing address will be updated and those contacts will be added into con list. Then again, another account will be available into this ACC through the loop and all the related contacts will be having related information and that contact will be added into con list. And after that, I'm going to write code count plus plus. So record count is initially zero. So once one account's billing address will be copied into one contact mailing address. So this record count variable will be incremented. Now after completion of these loops, I'm just going to write update con list. So this is my execute method. Now after that, I'm going to write one more method that is public void finish and here I'm just writing system dot debug dot count equals to and here I'm going to show this variable. So this way all three methods are implemented in this batch epics and the code is saved. Now I'm moving here. So I'm just doing a refresh. So here you can see only two processes are available. One is scheduled apex and one is future method. So when I will be executing this batch epics, you will see one more entry. Before that, I'm going to show you whether this contact is having mailing address or not. So it is having. So what I'm going to do, I'm just going to remove this and I'm going to save this. And if I go to account, so on account, you can see it is having billing address. And if I, okay, so now I'm going to, here you can see this billing country is USA. So now I'm going to execute the code and you will see related contact will be having that information populated. So I'm just opening anonymous window and here I will be executing the code. So I just need to use the class. So I'm just using it here, naming it as account process. Then ID batch ID equals to database dot execute batch. And here I need to use this ACC process. And this second parameter will be for batch size. If you don't pass this, so default batch size is 200. If you want to modify that batch size, so you can pass particular value as well. So I'm passing tennis batch size and clicking on execute. So now I'm coming here to a pick shops and clicking on refresh. So you will see one more entry. So batch epics, it is completed one batch executed. And if I come here, so why one batch? Because in ASOQL there is, there is a clear clause like billing country should be USA. So there are like less records, less than 10 where billing country is USA. And if I come here and open this contact, go to details. So here you can see this mailing information is available. Right. So we just copied four fields, street, city, state code. We didn't copy country. So that's why you can see USA is not available. Right. If you want to copy, so you can add one more line here. So this way I implemented batch epics and I executed it as well. Now the second part of this module is to implement test class for this batch epics. So for that I'm going to copy this and going to create new epics class and a pending test in the name. Now, whenever you want to create any test class, so you need to use at the rate test annotation. So at the rate is test annotation. So before the class I'm using that and before the method as well. Right. Now what I need to do, first I need to create the data. I need to create some accounts and then some contacts and then I can execute that batch. So I'm just creating method. So test address copy. So here I am creating list of account, ACC list equals to new list of account. Then one more list, list of contact, con list equals to new list of contact. Now I'm going to create accounts first. So I'm implementing a loop. So this loop will repeat five times. So five accounts will be created. So here, so this way this loop will iterate five times five account instances will be created and those will be added into this ACC list. And after this, like I need to put the address information as well. So here after comma, I'm just putting billing city as New York and billing country as USA. I'm writing ACC, insert ACC list. So this way five account will be inserted and those five account records country will be USA and billing city will be New York. Now I'm going to iterate on this account list so that I can create related contacts. So in contact, we have a required field known as last name. So I'm going to provide some value here. So I'm putting test as last name and con dot account ID equals to ACC dot ID. So this is important because we need to link the account with the contact. And I need to add these contact into con list. So this way con will be added into con list. And after completion of this loop, I'm just writing insert con list. So this way accounts are inserted, contacts are inserted. Now I can execute the batch. So for that, I'm writing test dot start test as dot stop test. So these two statements we use so that fresh governor limits will be available. So to reinitialize governor limits, you can use these two statements and these two statements you can use once in a test method. So here I'm just copying this class name and I created instance of this. Now I just need to write ID batch ID equals to database dot execute method, execute batch. And here I need to pass this ACC process. Now after this, I need to query the data. Like I just need to query the contact records so that I can verify whether a batch it takes executed properly or not. So I'm going to write list of contact con list equals to select count on contact via mailing city equals to New York. So here you can see on account billing cities New York and our batch is executing based on billing country. If billing country is USA, then we are going to copy these information. And in this information, one field is billing city. So we are just copying this billing city into mailing city. If mailing city on contact is having New York, it means your code executed properly. Okay. So instead of this list, I can use just integer. So counted contact and then I can write system dot assert equals. So expected five because we created five contacts for each account and we created five accounts. So five contacts will be updated. And here I am writing counted contact. So this way assert is also implemented. Now I can just run this test class so that we can verify the code covers. So I just clicked on run test. Test is passing assert is also passing. And if I go here, so you can see it is providing 100% code covers. So this way, with the help of batch epics, you can copy account billing information into related contact mailing information.