 Hello everyone. I am Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. StudySalesforce.com. This is my website. You can visit it to learn Salesforce. So in this video, I'm going to demonstrate you the scenario which is particularly related to Apex. So after understanding basic features of Apex, you might need some use cases. Those are demonstrated with the help of various features. Those are available in the Apex. So for that purpose, I am creating this video. So the scenario says like we need to query account records. Those are created this year. So there may be different scenarios like this year or maybe last year, this week, last week, next week. So whatever scenario you have accordingly, you need to apply SOQL first. Then we need to insert one opportunity record for each account. So based on the query, whatever our account records we are querying. So we need to associate or we need to create one opportunity record under that account record. So this is our requirement. So I hope you understood this thing. Now I am moving to Salesforce org and developer console so that I can show you how we can write the code for this. So first of all, let's implement SOQL so that we can use that SOQL in our method so that all the account records will be available. So those are created in this week. So for that, you can write select ID name from account where created date equals to this year. So basically select from where these are the keywords. So best practice says like you can use these in capital, but Apex is case sensitive. So if you write in small, so that also works. So if I click on execute, so here you can see in this year, total 13 records are created so their IDs and names are listed over here. So it means our query is working fine. So we can copy this query and use it here in particular method. So now I'm going to create a method. So this will be the name of method create opportunity on account then I'm creating list of account. ACC list equals to and then in square records you can paste your SOQL. Right. So right now I have used SOQL here and the result will be available into account list. So I hope you know, whenever we apply SOQL so result will be always in form of list. So you need to create a list so that you can store the result. But if you are using this SOQL result into a list, like if you are using ACC list in a loop for iteration. So in that scenario you can directly use SOQL in the loop as well. So right now I'm going to use this ACC list in the loop then after execution I will show you like how you can replace it with SOQL directly inside the loop. Then I will be requiring one opportunity list. So this is the name right now after creating these two variables so ACC list is having some records of account object opportunity to be created this instance is blank. So before applying loop I just need to check ACC list. So here we can use two things like either we can check the size and then we can apply the loop or you can use is empty method and accordingly you can use it. Right if you want to use is empty method so I'm just giving you brief demo. So you need to remove this. So this way you can apply is empty. So if ACC list is empty then this condition will be true but if it is not empty then we want to go inside dive condition so you need to apply not here. So it depends on your choice like which if you want to use both are correct. So I'm just raising this using this one. Now here I'm going to apply for each so it will iterate on this account list so one by one each account report will be available into this ACC and inside this for loop you can create opportunities and you can add those opportunities into list. So I hope you remember if you saw my earlier videos those I created based on some features those are available in a pic so there I told you like as part of best practice don't use SOQL and DML inside the loop. So if you are implementing a loop so you can create a list and after completion of loop you can use that list to insert delete update records. So by watching this example you will get to know like how we can do that. So now I'm creating one instance of opportunity. Now I need to fill the required fields of opportunity so here I'm putting name so opportunity name will be similar to account name. Then next is close date so here I'm putting system dot today and opportunity dot stays name equals to prospecting. So these are three required fields which are required on opportunity so whenever you try and create any opportunity record and if you don't fill anything so here you can see these three close date opportunity name and stays are required. So if you are creating any record through code then also you need to take care of those. Now the requirement is we need to link all these opportunities with particular account. So here you can see ACC list is iterating so one by one account will be available in this ACC instance. So now what you can do you can use this account ID lookup field that is available on opportunity and this you can fill with this ACC dot ID. So whatever account record will be available in this ACC that records ID will be assigned into this account ID so that newly created opportunity will be linked with that account. Now you can copy this and you can add opportunity into this list so this is part of best practice like you can't use DML inside loop because of governor limits so you can add opportunities into a list then after completion of this loop and if block you can check size of this list if it is greater than zero so you can write insert opportunities to be created. So this list will be inserted and opportunity records will be created. So this way our code is complete now we are all set to execute this so to test this code basically what we can do we right now we have not implemented any trigger so this code won't be executing automatically right so what we need to do we need to click on debug and open execute an inverse window and here you can call this method so name of classes my demo and this is the method create opportunities on account so I need to modify this right so now if I click on execute so this method okay so there's a mistake line one column 26 okay so we need to check it so here this is our closed this is also closed this is closed why to show me so actually I just forgot to put semi colon after this statement so now if I try to execute this it will work fine so code executed now it's time to move to opportunities first so you will see lots of opportunities created so here you can see all these opportunities are created today all 13 having same account name so opportunity name and account name are same this is today's date right so you can check it here 27 October so here you can see the dates stages prospecting and close date is today's date right so everything is fine and if you move to account so here also if you open any account records so you will see related opportunity with that name so here you can see united oil and gas gas corporation so this is the name of opportunity and account name is also same so this way if you implement this code so you will be able to create one opportunity under each account now I'm going to show you how you can remove this so you can copy this now here we can paste it this way so you don't need this if condition anymore so maybe we can comment this as well and we can comment this as well right so what what is happening here we applied as a cure directly here right and the result which will be returning from this as a cure will be available automatically in a list and one by one those account reports will be available in this ACC and rest of the things will remain same now if this query let's say you are trying to fat some records but there is no record available in the org so null check will be automatically applied here and if this is the case this for each loop won't be executed and here also we applied this check so if it is null and don't have any records so this insert operation won't be executed so that's why we need to apply these null checks always so that unnecessary statements we can avoid like at the time of execution so this is all about the scenario that I demonstrated you so we queried account records those are created this year then we inserted one opportunity record for each account so study salesforce.com or video library to learn salesforce you can visit and find lots of video links there thank you for watching this video