CRT-450 Salesforce Certified Platform Developer – User Interface – 25% Part 2
- 3.16- Write Visualforce Controllers
Hey, guys. This is section three logic and process automation. And this is lecture number 16. Write visual force controllers. In this lecture, we’ll talk about how we can write custom Visual Force controllers and how we can also write Control extensions. First of all, before learning how to write custom controllers and controllers extensions, you have to complete section four. Section four introduce you to VisualForce and to also standard controllers. So it’s a prerequisite before this lecture. This is a refresher. As we have seen earlier on, Salesforce, development is based on the NBC pattern, which promotes the separation of an application into three different components. First of all, we have on the right side the Application Data Storage, which is the model. On the left side we have the Application User Interface, which is the view. And in the middle we have the Application Logic, which is the controller.
So the view is mainly the interface. This is what we see. And the model is the database. It’s like the data, the list of records and so on. And in the middle, the controller is the one responsible of displaying the data in the view and of putting data in the model from the view. And when you click on a button in the view, the controller will take action based on the code on this button.
So this is the MVC pattern. In Salesforce, we have three types of controllers. We have the standard controllers, we have the custom controllers, and we also have controller extensions. The concept of standard controller is detailed in section number four. As a refresher, they provide all the standard functionality that you need for a VF page, because they include the same logic that is used for a standard page, like the Save button, like the Edit button, and so on. Now, the drawback of standard functionality and a standard controller is that it cannot change. For example, a standard controller will return one record of one object, or a list of records and all of a single object. We cannot have more than one object by using the standard controller. On the other hand, and to overcome this, custom controllers can be used to retrieve a list of items from more than one object to be displayed in one page, make a call out for an external web service, and to validate and insert data, and much more. So custom controllers will do much more than what can be done in a standard controller. Now, what’s a custom controller? Simply speaking, it’s just an Apex class that you write and that implements all of the logic for a page without using a standard controller.
The controller name on the VF page will be the exact name of the Apex class. And with custom controllers, and because it’s just an Apex class, you can choose the sharing model. So you can choose either to type with sharing or without Sharing, which is the default one. Now, what’s inside the custom controller. What’s inside the Apex class behind the custom controller, the three method types are used primarily and custom controller. First of all we have the Get method. These methods are used to get and to show a value from the controller to the VF page. So as you can see here we have a controller, this is linked to the database and it can use many things inside. It can use so called queries and so on. And then the get method and the controller is used to show the data on the page. And then we have the set method. These are mainly used to set a value of a variable from the VF page to the controller. So it’s mainly used to get the input from the user and to save this input into a value that can be used in the controller. So on the other hand we have the set. The set is from the view from the page to the controller.
So let’s say I want to give an input to be used and the controller I will use the set method. And finally we have the action method. There is no concept of getting or of setting data here. For example, these methods will be used to move to the next page, to update data, to click on a button and to send an email. So in brief, a custom controller, which is an Apex class, can have these three method types. It can have a get method, a set method and an action method. A Get method will be used to get data from the controller to the view to the page layout. A set method will be used to get data from the page layout from the user input to the controller. And finally an action method will be used to do anything that’s not get and that is not set. This is the detail of get and set method. As we have mentioned, VF requires a getter and a setter method to reference a variable in the controller or in the extension. Without a getter or a setter method, even public or global variables cannot be referenced in the VF page. So as we have mentioned, a get method will pull data from the controller to the page and a get method and the controller should always start with the get keyword. So get and then the name of the method. And on the VF page we have to reference this method by omitting the get keyword.
So as you can see here we have a method name and we don’t have any get. On the other hand, a set method is used to submit values from the page back to the controller. To define a set method we have to start the method name by the keyword set. And in the VF page we reference this method without the keyword set. And finally, a set method should take an argument. This is an example of a custom controller and of a VF page that is using this custom controller. As we have mentioned, a custom controller is just an Apex class that you write and that implements all of the logic for a page without using a standard controller. The controller name on the VF page will be the exact name of the Apex class. And finally we can define the Apex class using the with sharing keyword or the without sharing keyword to identify the sharing model. So this is an example. On the left side we have the Apex class which is called VFC one and it has these variables and these two methods. As you can see, this is a Get method. Why? Because it starts with a Get keyword and the same for this. Now, on the VF page what we have, we have the same starting component, which of the Apex page component. But now we have the controller attribute as opposed to the standard controller attribute. So this means that we are using now a custom controller and then the value is VFC one, which refers to this Apex class. So this page will use this custom controller that’s called VFC one.
And then we have these two lines. The first line starts with My name is and then it’s calling a method one between Coley brackets and preceded by exploration. Mark, note that we don’t have any Get here. Why? Because get is only used in the Apex class. And then in the VF page we get rid of the Gat keyword. So this is mainly calling this method and the second line is mainly calling that method. So the output of this will be what will be the output of this method. So in this case, this will return a string, that is my string and my string is walleed.
So this will be my name is walleed. And then we have the result is a method two. Method two is mainly Get method two which will return a result which is of type integer and this is mainly as x plus y, which is time plus one. So the output will be here 20. This is a very simple example of a custom controller. I included this to just let you understand what is a custom controller and how we can use this in a VF page. Another example is to build a custom controller that displays records from both accounts and opportunities. As you can see here we have a class and then we have the with sharing keyword. And then we have two methods. The first one is used to get accounts. So it’s using a soccer query and it will return a list of accounts. And the second one will use a sockwell query to get opportunities and it will return a list of opportunities. Now we can use this in a VF page.
Let’s see how this is the corresponding VF page. As you can see on the first line we are using the controller attribute to refer to this. This is mainly the Apex class that we have just defined. This means that this VF page will use the methods of this class and then on the page block table we are using the value attribute to refer to the ACC. Now what’s ACC is simply the getac method. So the return of this is simply a list of accounts of this so called query and then we are using the variable MYAC. MYAC will be used on each one of the iterations of the return of ACC of the getac method. So MYAC will be used on each one of the accounts that are returned by the getac method to get the value of this account. As you can see here we are using MYAC ID, that name and that owner name. So in this case we are listing the different accounts that are here from the getact method.
The same thing goes for the OPP is mainly the get op method and then we are using this variable for each one of the iterations. So my op, ID, that name and that other name. We can also use something called properties as an alternative to a get and to a set method. This is a combination of a variable with a get and a set method with a syntax that will group the get and the set method in one line. As you can see here we have a variable that’s called mystring and then we have between curly brackets get and we also have set. Let’s understand this between the curly brackets get is basically the get method and set is basically the whole set method. Instead of using the get method and the set method we use this properties. So this is an example we have public string and then we have VAR name and then we have between cole brackets get a semicolon and then set a semicolon. This is exactly equivalent to this. So instead of having a variable called VAR name and a get method on this variable and a set method on the same variable we use the properties. So instead of having these lines of code we only have one line of code and this is exactly equivalent to this.
So by using this we are mainly having one method for get and one method for set and the variable name is the VAR name. So now on the VF page we can simply use VAR name. This is a very simple example. Let’s say you want to take some input from the user and for that you want to display a text box and the via page and then you want the user to enter something in this text box. You want to save what is in this box into a variable and the controller. How we can do that? First of all we have to define an Apex class. So let’s define this class by the name of simple get set and then in this we just define these properties. So instead of defining a get method and a set method, we define these properties. And this is mainly a get method and a set method that is using this variable name. And then on the VF page we use the component form which means that we are now ready to take input from the user. And then we use the input text component to specify that.
Now we are taking an input. So as you can see here, we are using the value of user input which is mainly these properties and exactly it’s mainly the set part of this. And then in the last line we are using the output text component and the value is the same, but this time it’s using the get point. So this is briefly using properties instead of a complete method. Now let’s talk about controller extension. A controller extension is simply a controller that extends the functionality of either a standard controller or of a custom controller. To build it, we simply build an Apex class that contains a constructor that takes a single argument of either one of these two types. So it can be an argument of Apex pages that standard controller type or of the custom controller name. And the first example, this will be used as an extension to a standard controller. And the second type is the name of a custom controller. This is mainly extending this custom controller.
This extension is associated with the VF page by using the attribute extensions and the Apex page component. And on the VF page more than one extension can be defined. So I’m not only bound to one extension, I can have more than one by using a comma separated list. And finally, when I extend a controller, the method of the main controller will be present. I can use them and I can also use the method of the extension. This is an example. I’m building an extension by building an Apex class called Ctrlext. So in this case the name of the class will be the name of the extension. And then as you can see here, I have a constructor, it has the same name of the class and it will take this argument. This will say that this is an extension and this will be used on standard controllers because I’m using this argument as opposed to a custom controller argument. As you can see here in the VF page I’m using this attribute to call the standard controller account. And on top of that, I’m using the extensions attribute to add the methods of the CTR LEXT extension to this VF page. So in this case I can use the methods of this extension which are in this case this one and this VF page.
So as you can see here I’m using Greeting. This is from this extension and it will call the Get Greeting get method. As we have mentioned, we can have more than one extension on the same VF page by using a comma separated list of extensions. This is an example. We are using the extensions attribute to call two extensions. So in this case this VF page will have the standard controller methods, it will also have X one methods and X two methods. So on the first line we are calling foo, foo is mainly from the X one extension. So it’s mainly the get foo and on the second line we are calling the bar method and this is mainly the get bar method from the x two. So this is just a simple example that shows you that we can have more than one extension on the same VF page and we can call methods from different extensions on the same page. Now let’s say that we have two extensions on the same VF page and let’s say that on these two extensions the same method is defined with the same name. So in this case we have two extensions, x one and X two. And then on X one we have get, foo and same. On X two we have also get foo, and then on the VF page we are calling foo, which means that we are calling getfo. So which one of these two will be executed? In this case the leftmost extension will be executed. So in this case the method and the x one extension will be executed.
So in this case it will be get four of the X one class. Now, the default security behavior of an extension is running in system mode, which means that an extension will have access to all of the records. But if an extension extends a standard controller, then it will run in user mode. You can also use the controller Apex class, the keywords with sharing or without sharing. So if you use with sharing you are forcing this extension to execute a user mode. And if you are using without sharing, you are forcing extension to be used in system mode. Let’s now go to salesforce and let’s see these in action.
- 3.16- Write Visualforce Controllers – Demo – Custom Controllers
So this is the developer console. First of all, let’s create a custom controller. To do that, we have to create a class. So file a new and then Apex class. Let’s call this class. CC one. And then in this class we have to create the different variables and methods. So let’s do so. So there we go. This is our Apex class, which is our custom controller. It has these variables and it has these two methods. Notice that these two methods start with Get, which means they are Getters. And each one of them will do something and it will return. This one will return a string and that one will return an integer. So now we can use these in our VF page. So let’s go to Salesforce and let’s create a VF page. So in Salesforce I will use the URL method and I will call the first page VFC One. We have to create it because it does not exist. There we go. So now let’s get rid of the body. And the first step is to specify the controller. So in this case, it will be our custom controller. So we use the attribute controller and then we specify between the quotes the name of the class, which is in this case, CC One. So CC one and then save. Now we are specifying that this VF page will take all of its orders from this custom controller, which is the Apex class. So what we’ll do, let’s create some text.
So this text is from the VF page itself. It’s a static data. Now, to call the methods from the class, to call get method one and Get method two, we get rid of the Get part and we use only method one and method two. So to call it, we have to use the curly brackets and then the explanation mark and then method one. And the same thing for the second one. So this is in fact calling the method Get method one from the class CT One. Let’s save this. And there we go. I need to change this. Control us again. There we go. So this is coming from where? From here. So it’s returning my name. And my name is what? It’s this string. And method two is returning a result, which is x plus y, which is ten plus ten, which is 20. So that’s why we can see here 20. So this is just a small example. To get you started with custom controllers, let’s create another one. This time I will create one that takes data from Salesforce, so New and Apex class. And let’s call this one CC Two. As we have mentioned, we can use the with sharing and the without sharing. So in this case, I will use the with sharing keyword to enforce the sharing rules and the permissions. And then I will start with the methods. The first one will return a list of accounts and it will be a getter. So it should start with get and let’s call this list ACC and it will have a list of accounts which is a sockwell query. So in this case this so called query will return to me the ID and the names of all of the accounts of the user.
Why? Because I’m using the with sharing keyword and it will return this list. This is the first method and I will create another one for opportunities. So in this case it will be instead of account it will be opportunities. So now we have two methods. The first one will get the list of old accounts and the second one will get the list of old opportunities. Let’s create another VF page to use these. So I will go to the salesforce and then let me copy this and I will use a new tab and I will call this VFC two. Of course I need to create it. So this is the page step one is to specify the controller. So in this case I will use the attribute controller and it will be the CC two controller and then let me remove the body of this. And the first step is to add a page block component. So I will use apex page block and I will specify a title for this control s. There you go. Now let’s fill this, let’s use a page block table component and the value of this will be what will be the method and the CC two class. So in this case if I go back to the class I can see I have this method that will return to me the list of all the accounts of all my accounts and the name is get list ATC. I need to get rid of the get keyword and my VF page so in this case I will use only list ATC. So this is what I will do here I will use list AWC and then the variable that will be used and start the body of this page a block table will be ACC.
So this variable will be used as a temporary variable for each one of the accounts of this list. It will be used inside the body of the page block table component. So to be specific it will be used in the column component. This is a selfcontained component which means that it starts and it ends on the same tag and the value will be what? It will be this variable name and then the field name. So it will be accident as you can see here in this method we are fetching both the ID and the name of the so called query so we can use these as fields in our BF page and then we’ll do the same for the name. So I will copy this and I will paste it here. Let me change this to name and let’s try to save now there you go. This is the list of all the accounts of this user.
Of course, if we are running this VF page by another user, he will only see his own list of accounts. Why? Well, because we are using the with sharing keyword which is enforcing the permissions and the sharing and the record access. Let me now do the same for opportunities. So I will copy this and I will paste it here and let me change it slightly so it will be a block for opportunities. And then the method will be listope, which is the get list op from the class itself, from the custom controller. And then I will use the variable Op and I will change here for opt name and op ID. So let’s save now. There you go. So now I have a list of all of my accounts and all of my opportunities on the same page. This is the advantage of using a custom controller. This could not be achieved by using a standard controller or even even a standard list controller.
- 3.16- Write Visualforce Controllers – Demo – Controller Extensions
Now let’s do something. Let’s create an extension. So I will go to the developer console and I will add a new class and let’s call this class Ex one. So the thing that we need to do in an extension is to provide a constructor. A constructor is just a method that uses the same name of the class. So in this case it will be public exd one. And the argument that it will take will be the argument of the custom controller. So it will be the custom controller name, it will be CC Two. So CC Two, my CC Two and then we will have an empty body. So this line will specify that this is an extension and it’s used to extend this custom controller. Now we can add any method that we want. So let’s say I want to add a simple method that will return to me a string. So I will call this exe one, m one, and this will return to me a string. So, just a very simple example to just show you how we can create custom controllers and extensions.
So this is an extension, exe one, we have specified a constructor and this is the line that specifies that this is in fact an extension. We could also use another argument here. We could use the Apex pages, that standard controller argument instead of the custom control argument. But in this case I’m using this because I want to extend CC Two. So let me save this. Now let me go back to the VF page and there let me specify this extension. So what I will do, I will just use the attribute extensions and I will specify exd one, control s. Now this VF page is using this custom controller as well as this extension, which means that I can use the method of this.
I can use this method. I forgot to do something here, I forgot to use the get keyword. So CTRL s, and then I will use this method in there. So let me specify this if you want here. So I will use a paragraph tag and then I will call this method. So what will happen here? I’m using this method from where? From Exe one, just because I have added this, if I did not add this, I could not use this. So let me try to save and let’s see what will happen. There you go. So this is in fact from this extension I can also add another extension. So file new and Apex class, and let’s call this Exd Two. So I will change it slightly to make it for EXP two, and I will call it now in the VF page.
But before that I need to specify it here. How do I do that? I just added as ex two and the extensions attribute, it will be a comma separated list control s. So let me save this. Now let me try to save this again. And then what I will do, I will just copy this and I will call exd two m one CTRL s. There you go. So in this lecture you have learned about custom VF controller syntax. How we can create them, how we can build them, how we can use them. In the VF pages we have talked about the get and the set method. We have talked about the properties and finally we have talked about the controller extensions and then we have jumped into salesforce and we have shown you all of that in action. And finally, as usual, thanks for watching.