 Hello everyone, I'm Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. StudySalesForce.com is my website or video library to learn sales force. In this video, I'm going to demonstrate how you can implement the scenario with the help of Apex. So we need to query opportunities. Those are created in last week. If opportunity stays is closed one, then we need to update description as opportunity is closed one. So description is a field available on opportunity. In case of closed lost, we need to update description as opportunities closed lost. So with this demonstration, you will get to know how we can use SOQL, how we can apply a loop on list, and how we can perform DML operations. I'm moving to the org. So here, first of all, I'm going to implement a query in query editor so that we can check whether any opportunities available in last week or not. Otherwise, we will be using this filter criteria as this week and I will be creating some new opportunities. So ID, name, then stays name, close date from opportunity. We have created date equals to last week. So I'm clicking on execute. So here you can see there are various opportunities available, which are created last week and stays name for all is prospecting. So I'm going to change stays name of some of the opportunities. So let's say this is closed one, this is closed lost, and I'm going to click on save rows. If I execute this query again, so here you can see this opportunity is modified as closed one, but this is not. So let me go to setup and let me copy the exact pick list value so that it can be saved. So jumping to object manager, and here I need to search for opportunities. And so here it is. Now click on fields and relationships, then search for stage. Under this stays pick list field, you will be finding the pick list value. So you just need to copy and use that. So here it is closed lost. I'm clicking on save. So it is not updated. It's updated now and I'm just closing this and querying the data again. Yeah, here you can see. So we have one opportunity as closed lost, one as closed one. So now I'm going to write the code so that we can use this SOQL and we can update the description, right? So now you have two different ways, like you can use SOQL and then store the result into a list and then you can iterate on the list. Second is you can directly use SOQL in the for each loop. So I'm just implementing it in both the ways. So first I'm going to implement list of opportunity. So OPP list. So here also like you can put it like this or maybe instead of using this, you can paste this SOQL directly. So let's do that so that we can reduce number of lines. So I'm pasting it here. Sorry. Okay, so let me do that again. Yep, everything is fine. So mistakenly I just logged out. So yeah, I just forgot this thing as well, like before writing the code, I need to define name of method as well. So public static void update opportunities. Now inside this, I'm going to create list of opportunity for this which is available here. Now I'm applying null check. So if OPP list.is empty. So if it is not empty, that I'm going to iterate a loop on opportunity. So now here I'm using OPP list, right? So if this OPP list is not empty, then it will be iterated. Now inside this for each loop, I just need to check the stage. So if OPP dot stage name equals to closed one, right? So the opportunities which we carried using this ASOQL, those are available in this OPP list. And we are iterating on this OPP list one by one. So each opportunity will be available in this OPP instance and we are checking its stage name if it is closed one, then OPP dot description equals to opportunity is closed one. Right, so this way I updated the description then else if opportunity dot stage name equals to close lost. So in that case, you can simply copy and paste it and opportunity dot description equals to opportunity is closed lost. So this way you can implement your code, right? So your opportunity list will be modified with the help of this for each loop, right? And if you're modifying this opportunity list directly in this loop, so you don't need to create any additional list where you need to add those instances, right? So what you can do just after completion of this for loop and this if block here, you can check if not opportunity list dot is empty so if this list is not empty, you can apply update OPP list, right? So this is basically DML. So initially I told you like with this example, with the help of this example, you will be able to understand how we can apply SOQL to query the data, how we can apply a loop on a list and how we can perform DML operation. So I hope all the things are covered with this example. So now I'm saving this code. Right? So if I execute this query once again, so here you can see two records are available. So let me pull description as well. So initially descriptions are blank for every opportunity. Now I'm going to execute this code. So I'm just calling this method, clicking on execute. Now I'm just executing this SOQL. And here you can see for this closed one, description is updated as opportunity is closed one. And for this closed lost opportunity, description is updated as opportunity is closed lost. So this way, if you query some data using SOQL and then you apply the loop and then perform DML operation. So you will be able to modify the data of existing records, right? But later on, like, if you are implementing these things, like whenever you are creating any opportunity or updating, so in that case, you can just have trigger as well. So in this code, I have not used trigger. So this is just for demonstrating how you can use APIX without the help of trigger. Now one more thing, if you want to remove these if conditions, so what you can do, you can simply copy this. And you can paste it here, right? So no need to create this list. You can just have SOQL written over here, whatever data that will be fast, that will be available one by one in this OPP instance and it will be like executed. Now what you need to do, this in this case, you need to create one list where you can add this opportunity and then you can use that here, right? So what you can do, simply remove this. Here after this, you need to write OPP list.add OPP. So opportunity will be added into this list and after completion of this loop, you can apply this and update the opportunity list. And here it is showing some error. Yeah, I need to remove this semicolon. Now it is all sorted and implemented correctly. So if you execute this code, so same operations will be performed. So this way I just demonstrated you how you can implement your APIX code with SOQL for each loop and DML operation. So I hope you understood this use case and if you want to see more use cases or videos related to Salesforce, so just visit studysalesforce.com. Thank you for watching this video.