Tuesday, 14 February 2017
Hands on Salesforce Training Part I
Sunday, 16 February 2014
SalesForce Online Training
Hi All,
We are providing SalesForce Online Training with with real time scenarios.
If you are interested please contact us.
Duration: 30Hrs
Email: AvaramTechnologies@gmail.com
We are providing SalesForce Online Training with with real time scenarios.
If you are interested please contact us.
Duration: 30Hrs
Email: AvaramTechnologies@gmail.com
Friday, 7 September 2012
Add an Action Method to a Controller - Visualforce
In this lesson you add an action method, a method that is invoked when the user of your Visualforce page clicks on a button or link. You previously used such a method,save(), on the standard controller—now you’ll write your own. The goal is to modify the page to dynamically display a selected account record’s list of contacts.
- Click MyController to edit the controller for your Visualforce page.
- Below the line public class MyController { add the following two lines:
public Id selectedAccount { get; set; } public List<Contact> contactsInformation { get; set; }
This creates two properties. The first, selectedAccount, is an identifier, while the second, contactsInformation, is a list of Contact records. - Click AccountWithContacts to edit the Visualforce page, and add the following snippet just below the </apex:form> line:
<apex:outputPanel id="ContactDetail"> <apex:repeat value="{! contactsInformation}" var="contact"> <p>{! contact.FirstName & ' ' & contact.LastName}</p> </apex:repeat> </apex:outputPanel>
This iterates over the list of contacts (remember, contactsInformation is defined to return List<Contact>), displaying their first and last names.At this point, you can save the page and you’ll notice no change. What you need to do now is ensure that when an account name is clicked, thecontactsInformation property is populated with that account record’s contacts. - Modify your controller by clicking MyController and adding the following before the final brace:
public void accountClicked() { contactsInformation = [SELECT FirstName, LastName FROM Contact WHERE AccountID = :selectedAccount]; }
- Replace the line <apex:outputTextvalue="{! acct.name}"/> in your Visualforce page with the following:
<apex:commandlink action="{! accountClicked}" rerender="ContactDetail"> <apex:outputText value="{! acct.name}"/> <apex:param name="id" value="{! acct.Id}" assignTo="{!selectedAccount}"/> </apex:commandLink>
This uses the rerender attribute to ensure that the output panel is refreshed using an Ajax update. - Click Save.Now when you select an account record with related contacts, the contacts for that record are dynamically displayed below the list of accounts.
A lot happens when you click the name of an account:
- Because an <apex:param> component is supplied, the ID of the current account is first assigned to the property selectedAccount in the controller. So now the controller knows which account you selected.
- Because the account name is wrapped in an <apex:commandLink> component, the method indicated in the action attribute, accountClicked(), is invoked.
- When the accountClicked() method runs, it performs a simple query, using the selectedAccount identifier, and assigns the result to thecontactsInformation property.
- Because the <apex:commandLink> component has a rerender attribute, the output panel dynamically renders, which in turn iterates over thecontactsInformation and displays the contacts’ names.
That’s a lot to digest! Take your time, experimenting by removing attributes, modifying the methods, and noting what changes occur. Also, refer back to Tutorial #11: UpdatingVisualforce Pages with Ajax to learn more about the Ajax effect.
Add a Method to Retrieve Account Records - Visualforce
You’re now ready to write getMyAccounts(), which simply returns the list of 10 accounts most recently modified.
- Click the MyController tab.
- Modify the getMyAccounts() method to read as follows:
public List<Account> getMyAccounts() { return [SELECT Id, Name, AccountNumber FROM Account ORDER BY LastModifiedDate DESC LIMIT 10]; }
- Click Save.
Your page now displays the names of the 10 most recently modified accounts. The Visualforce expression {! myaccounts} causes Visualforce to execute yourgetMyAccounts() method in the MyController class. That method returns a list of account records, which is what the <apex:dataList> component iterates over.
Create a Page with a Controller - Visualforce
You create controllers the same way you created extensions in the previous tutorial, by navigating to Setup | Develop | Apex Classes | New. You can also have the Visualforceeditor create them for you.
- Create a new Visualforce page named AccountWithContacts.
- Enter the following as the body of the page:
<apex:page controller="MyController"> <apex:form> <apex:dataList value="{! myaccounts}" var="acct"> <apex:outputText value="{! acct.name}"/> </apex:dataList> </apex:form> </apex:page>
The contents of the page will be very familiar to you. The primary component iterates over a list of accounts, displaying their names. Where is myaccounts defined? It’s not defined yet, but it will reside in the controller that you specified, MyController. - Click Save.
- Visualforce notes that the class MyController doesn’t exist. Click Create Apex class 'public class MyController'.
- Visualforce notes that there’s an unknown property myaccounts and offers to create it for you. Click Create Apex method 'MyController.getMyAccounts'.
You will notice two things in your Visualforce editor. First, there’s another error message: Unknown property 'String.name'. This happens because you haven’t quite fully defined the getMyAccounts() method yet. Visualforce doesn’t know the type that will be returned by that method. You’ll also notice a new tab has appeared next to Page Editor titled Controller. This lets you modify the controller’s Apex class.
Creating and Using Custom Controllers - Visualforce
Tutorial #4: Using Standard Controllers introduced how Visualforce supports the Model-View-Controller (MVC) style of user interface creation. Controllers typically retrieve the data to be displayed in a Visualforce page, and contain code that executes in response to page actions, such as a command button being clicked. Up until this point, you’ve been using a standard controller—a set of logic provided for you out of the box by the platform. In this tutorial you create your own controller by writing Apex code.
Custom controllers contain custom logic and data manipulation that can be used by a Visualforce page. For example, a custom controller can retrieve a list of items to be displayed, make a callout to an external web service, validate and insert data, and more—and all of these operations will be available to the Visualforce page that uses it as a controller.
Add an Extension to a Visualforce Page
Tutorial #8: Inputting Data with Forms shows how to create a very simple form. In this lesson you will duplicate that page, adding the extension functionality:
- Create a new Visualforce page called MyAccountWithExtension.
- Use the following as the body of the page:
<apex:page standardController="Account" extensions="MyExtension"> <p>{!title}</p> <apex:form> <apex:inputField value="{!account.name}"/> <apex:commandButton action="{!save}" value="Save!"/> </apex:form> </apex:page>
- Now access your page with a valid Account identifier passed in as a parameter. Your URL will look something like:https://na6.visual.force.com/apex/MyAccountWithExtension?id=0018000000MDfn1
What you’ll find is that your form pre-populates the input field with the name of the identified account. Great! And it also shows your fancy title with the account name and identifier.
When you save the page, you’ll notice that a new line appears above the form. Here’s what’s happening in your page:
- The attribute extensions="MyExtension" ensures that Visualforce instantiates a copy of your Apex class MyExtension, passing in a reference to the current controller (you’re using the standard controller for Account).
- The syntax {! title} instructs Visualforce to look for a method called getTitle() and insert the result of calling that method. Because there is such a method in your extension, Visualforce executes it and replaces the statement with the result.
Subscribe to:
Posts (Atom)