 Hello, everyone. I'm Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. In this video, I'm going to demonstrate a scenario how to integrate flow with code. Let's understand the requirements so that you can learn how we can integrate flow with code. So I will be demonstrating you how you can call Apex invocable method through screen flow. So the requirement is we need to create an opportunity through screen flow and that opportunity we need to link with an account. Now, through Apex code, we need to add the amount that will be populated on the newly created opportunity with accounts and will revenue. This third point will be solved with the help of Apex and the second point will be solved with the help of flow and flow will be internally calling the Apex code. So jumping to the org. So first of all, I'm going to implement the Apex class so that you understand like how it will be implemented. Then we will be creating the screen flow. So if you click on this top right corner, gear icon, you can find this developer console option where we will be implementing our Apex class. So just click on file, new, and then Apex class. So name of Apex class is account action. I'm clicking on OK. So this is our class and inside this class I'm going to implement a method. So first of all, you need to remember if you want to call your Apex class method in the flow. So you need to use an annotation at the rate invocable method. Right. So after implementation of this annotation, you can start writing your method. So this is the header of the method. So public static then list of decimal is the return type. Name of the method is update account revenue and this method will be receiving list of ID that I'm going to store in the IDs. Now inside this method, I'm going to implement the code. So here I'm trying to query the opportunity record. So opportunity ID will be available in this IDs list. So what I'm going to do, I'm just using ID zero. So the first item that is available in the list. That is the ID of particular opportunity. So basis on that opportunity ID, we are querying the opportunity information. So opportunity account ID and amount these two fields will be queried. So here I'm writing the comment fetching opportunity information and it will be stored in this OPP list. Now, with the help of this OPP list, I'm going to fetch information of account. So I'm going to fetch the account which is related to this opportunity. So I'm creating list of account ACC list equals to so I'm querying annual revenue field of account. So this opportunity list is having opportunity record that is available on the 0th index. So through that index I'm getting the account ID and through this account ID I'm matching it with the ID of the account record. So whatever account is matching, that's accounts annual revenue will be queried and the complete information will be stored in this ACC list. So this way I queried opportunity and account information. Now I'm creating one more list of type decimal. Now, I just need to perform addition operation. So what I'm going to do, I'm just using account list zero dot annual revenue equals to ACC list of zero dot annual revenue plus OPP list of zero dot amount. So opportunity amount will be added to the annual revenue of that account record and it will be stored into that account list automatically. Now, after this, we just need to update the ACC list. Right. So the account record which is related to that opportunity will be updated. I'm going to add this annual revenue into this annual revenue list. So annual revenue list dot add. And here I'm going to pass this. So this annual revenue list is of type decimal and this annual revenue that we are passing it is also of type decimal so it will be added into this list and this list I'm going to return. So this list will be available in the flow. So this way I implemented this apex invocable method. Now it's time to implement the flow so that we can process this invocable method. So jumping to the flow. So I clicked on new flow and I'm going to create a screen flow. So in this screen flow, first of all, I need to create screens. Labeling it as opportunity info. So first time taking type as text, labeling it as name, so it will be receiving opportunity name. Then I'm searching for date. So this date component will be receiving close date. So I'm marking them required because these are required fields. Then I'm having a pick list to have stays name. And for this, we need to create a new choice. So actually to have all the pick list values those are available under the stage field under opportunity object, we need to create pick list choice set. So I'm searching for opportunity object data type is pick list. Field is stays name. So it will be linked with this stays name component that we created on the screen. And it is also required because these three fields main close date and stays are required on opportunity. That's why I'm making them required. So that I'm having a number field, it will be for amount. So we are going to receive amount from screen. So it is also required because we need to add that amount into the accounts and well revenue. And if you don't want to make it require. So what you need to do in your code, you need to apply and null check for this piece like opportunity list of zero dot amount is null or not. If it is not null, then only it will be added. Right. But right now I'm making it as required because we want to add the entered amount in the accounts and well revenue. Now I'm clicking on done so this way opportunity information will be received. Now, I am going to place this flow on the account record page. So I need to link this opportunity with the account record. So I need account ID as well. So that will be available in a variable. So I'm creating a variable named as record ID. It is of type text and available for input. So this record ID variable will be receiving input from the account record page. Now after this I need to create record opportunity record so I chose create record element. So it will be creating opportunity. So you need to choose this option use separate resources and literal values. We need to create opportunity record. So opportunity name will be equals to screen component name. Then close date will be equals to screen component close date, then stays name equals to screen component stays name, then amount equals to screen component amount. And then we need to populate account ID. So this account ID is a lookup that is available on the opportunity record. So we need to populate record ID value into this. So this record ID will be having the account ID from the record page so that account ID will be assigned into this account ID lookup so that newly created opportunity will be linked to that account automatically. Now after creation of this opportunity I want to store that opportunity ID into a variable. So here I'm going to create new resource as a variable which will be storing opportunity ID data type is text and I'm enabling these checkboxes clicking on done and done. So now after implementing the screen and the create record element, it's time to call this method that we implemented here invocable method in the flow. So for that, you need to use this action and just click on type as a category. So click section and here you will find your invocable action or invocable methods. So just select that. So account action is the name of the class. And here you can see input values IDs is automatically available. So here IDs of type list of ID, but from here we just need to pass the variable single ID variable because we have created it already as a variable. So if code it will be stored as list of ID. So call a picks method, then enable this button and here you can search for OPP ID that we created as a variable in the create record element. So if you want to receive any output or let's say input for the flow. So for a picks code it will be the output like it is returning this annual revenue list which is off type decimal. So if you want to receive this as an input. So what you can do you can check this checkbox manually assign variables. And here you can create new resource. It has returned annual revenue and it will be type of number available for input and now here we have this transaction control. So if you go through with the description here so we have three options. Let the flow decide. Always start a new transaction always continue in current transaction right so if you go through with the text which is available here so the flow determines at runtime whether a new transaction is required to successfully execute the action. So it is about the action like if the action involves a call out call out means web service call out and the current transaction as pending operations. The current transaction is committed before executing the action in a new transaction. Right, so this is highly recommended. Then we have second option always start a new transaction. So before this action is executed the current transaction is committed. The action is then executed in a new transaction. If the action fails for some reason Salesforce can't roll back any operations from the previous transaction. Then always continue in current transaction so this action is always executed executed in the current transaction. If the action involves a call out with the current transaction has pending operations the flow fails. Right so this way as per the requirement you can choose any of the options I'm choosing first option and I clicked on done. So this way I called my apex class method in the flow. Now after this I'm going to pick one more screen. So I'm going to show the returned value here with the help of display text. So here I'm going to show latest annual revenue and that we stored in this variable return annual revenue so I'm going to put it here increasing the text size making it as bold and clicking on done. So this way the flow is implemented and it is calling the apex invocable method as well. So now I'm just clicking on save. I'm going to activate it. Right so this code is already saved. Now what I need to do I need to go to account record. So I'm going to pick an account record if I go to details. So here you can see annual revenue is already having some value right now if I go to related list so it is having three opportunities related which are having some amount already populated. Now, if I try to create new opportunity with that screen flow. So on that opportunity whatever amount I will be entering that should add on this annual revenue field. So first of all, I need to place that screen flow. So I need to edit this page. So I'm searching for the flow component and I'm going to place it here. So here is the flow screen flow calls apex class method. So it requires one input so I am just enabling this checkbox because it is available for input and for this opportunity ID don't enable this. So maybe like opportunity ID, we are passing into the flow as output so what you can do in the flow where you created opportunity ID. So maybe you can uncheck this right so it won't be required for input it will be like sending the output in a pic so if you uncheck this so that is also okay. So return annual revenue it will be receiving input from the apex. So we don't need to enable this as well. I just clicked on save moving back. So here you can see our screen flow is available which will be calling the apex class method and here we have three opportunities I'm creating one more opportunity and entering amount as 1000. So on account you can see annual revenue is this so 1000 will be added. So I clicked on next. So here you can see apex executed internally and on this flow it is showing latest and one revenue and it is having 1000 added. If I click on finish so you will see this annual revenue will also get updated. So you can see 1000 and add is added into this annual revenue field. If I go to related list so now opportunities are full. So with this way, like first of all you need to implement your apex code whatever you want to perform. And remember that you need to use this invocable method annotation, whatever you are passing will be stored in form of list, whatever you are returning will be returned in form of list, but through flow, you need to pass. Input, you need to create input output variable, like single variable don't create them as a collection, but in apex code it will be retrieved and returned as collection. So this way I hope you understood how we can call a picks class method with the help of screen flow.