 Hello everyone. I'm Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. In this module, I'm going to demonstrate the difference between controller and helper and we'll be explaining the benefits of helper. So let's understand the benefits of helper first, how we can use helper in lightning or a component. So if you want to reuse your functions defined in helper. So for that purpose, you can create helper file. So normally we create functions in controller, but if you want to reuse your functions in controller or maybe in renderer JavaScript file. So you can define those functions in helper and then you can use them. So I'm just taking you to lightning or a component. So here you can see we have controller, we have helper and we have renderer. So all these are JavaScript files. So generally we prefer this client-side controller so that we can control events, system events, browser events or custom events. But if you have lots of logic or you want to reuse your code, so you can shift your logic or reusable code into helper and then you can call those methods or functions into controller. Even the methods or functions which are defined in helper, you can call them in the renderer file as well because it is also a JavaScript file. Use helper for defining data processing tasks and queuing server-side actions. So sometimes it is recommended like if you want to link your auto component with server-side controller that is your apex. So that code you can implement in helper and that helper you can call in the controller and that controller function will be further linked with your any button or any event. So next is helper functions are local to the component. So they are available with the component itself. They improve code reusability. You can move the code from controller to helper whenever possible so that you can better manage your controller and all the business logic will be available in the helper. Then helper function can be called from any JavaScript code in a components bundle such as helper method can be called from client-side controller or it can be called from renderer as well. So I will be demonstrating that as well like how we can do that. So for example, here we have this helper method implemented. So here we can receive parameters as well. So let's say I'm receiving this parameter. So now here in controller how I can call that method. So I'm just copying this name. This way I can call it from the controller. So from controller I'm calling the helper method that is defined here and here you can write your logic. Okay. Now if you want to call your method in this renderer file as well. So how you can do that. So here basically you will be writing the code and so I'm just implementing after render. Now here you can simply use this statement. So I'm just copying and pasting it here. Right. So this way, whenever this renderer file will be executed on after render event. So this helper method will be called and it will be executed. So this way you can call this method in controller as well as in renderer. Even helper functions can be called within helper itself by another function. So that is also possible. So if you have multiple functions so you can do that. So I'm going to show you that right away. So in helper. So let's say we have these two methods. So this is helper method three, which will be receiving two variables and I'm just calling this helper method and helper method two. So we can call helper method three individually as well. And this helper method three is called helper method two as well. So whenever you will be calling helper method two. So three will be executed automatically. Right. So this way you can link your helper methods with other methods. Other functions are similar to client side controller functions shape shape. So this you saw like how we implemented this function my action. Similarly, we are implementing this these helper methods. So now you might be thinking when to use controller and when to use helper. So there's these three points will clear all your doubts. You can use controllers to listen to user events and other events like component and application events, but you can delegate your business logic to helper. So for example, on your component you have used a button. If you click on that button so even will be generated so that even can be handled through controller and then the actual business logic you can define in the helper that you can call in the controller itself. So here's another delegation in all render function like render the render and so on. So here, like we have this renderer files in this the renderer file, we can define render the render after render. So in every method like you can call that helper method. Whenever you need to call one controller function from another controller function so in that case move that for two helper. So in controller it is difficult to call a method. In another matter. So in that case what you can do you can you can shift your reusable code to help her and then you can call that into separate controller methods. Right, so let me show you one more quick example so that you understand like how like if you are interacting with your apex controller so how you can reuse that code in controller and render. So I'm in this helper and in this helper. Okay, so before moving to help her, let me just implement a function in controller. So I'm going to implement a function in controller so this is called server function which is having this implementation. And after render, I'm going to define this right so this piece of code I'm calling here as well and here in this controller as well. So controller and render both are having the same line of code. So what we can do, we can jump to this helper. And in this helper we can create that method. So I'm going to create that method here so I'm just removing these and pasting it here. So in this helper I'm having this call server right and it will be receiving two parameter component comma helper. Right now what we can do we can call this method in the controller so instead of writing it here, what I can do I can write helper dot all server. And this helper is expecting two parameters so we can pass CMP and help right and same we can do here as well. So I'm just copying this statement, pasting it here. So this way this action is basically calling the method this get accounts which will be available in the apex controller. So this we implemented in the helper and this is being called in controller as well as in renderer. Now in controller if you want to call it again so what you can do you can simply implement one more function so let's say call server one, and you can call this call server method which is defined in helper once again. So this way you can reuse your code that is defined in the helper in the controller as well as in the renderer. And also remember like if you want to pass some parameter additional parameters. So let's say I am having one variable as param one. So now you can pass some value so let's say test. Here also you can pass test and from here also you can pass test. So this test value will be stored in this param one and later on you can use it in this helper for processing. So other than that, these parameters you can pass additional values to help a method as well. So this is all about controller helper and render like how you can communicate between these three JavaScript files in lightning or a component. So I hope you understood what is the importance of helper and how we can reuse the code, which is defined in helper, and we can call that code in controller as well as in grant.