官术网_书友最值得收藏!

Editing a record in Visualforce

The standard record edit page does not allow customization outside the layout of fields. Editing records with Visualforce pages allows customization of all aspects of the page, including styling, content, and displayed buttons.

In this recipe, we will create a Visualforce page that provides contact edit capability, but does not allow a contact to be reparented to a different account. The account lookup field is editable until the record is saved with the lookup populated, after which it becomes read-only. This page will also render a different section heading depending on whether the contact is being created or edited.

Getting ready

This recipe makes use of a standard controller, so we only need to create the Visualforce page.

How to do it…

  1. Navigate to the Visualforce setup page by clicking on Your Name | Setup | Develop | Pages.
  2. Click on the New button.
  3. Enter ContactCreateEdit in the Label field.
  4. Accept the default ContactCreateEdit that is automatically generated for the Name field.
  5. Paste the contents of the ContactCreateEdit.page file from the code download into the Visualforce Markup area and click on the Save button.
  6. Navigate to the Visualforce setup page by clicking on Your Name | Setup | Develop | Pages.
  7. Locate the entry for the ContactCreateEdit page and click on the Security link.
  8. On the resulting page, select which profiles should have access and click on the Save button.

How it works…

Opening the following URL in your browser displays the ContactCreateEdit page to create a new record: https://<instance>/apex/ContactCreateEdit.

Here, <instance> is the Salesforce instance specific to your organization, for example, na6.salesforce.com.

Note that the section heading for the page is Create Contact, and the Account Name field is editable. Save this record and edit it again via the same page using the following URL: https://<instance>/apex/ContactCreateEdit?id=<contact_id>.

Here, <contact_id> is the record ID of the newly created contact, and it displays the edit form of the page with a different section heading, and the Account Name field is changed to read-only.

The Visualforce page utilizes a standard controller and conditionally renders the section heading, and input/output variants of the Account Name field based on the ID of the record.

<apex:sectionHeader 
    title="{!IF (Contact.id==null, 'Create', 'Edit')} Contact" />

<apex:inputField value="{!Contact.AccountId}"
        rendered="{!null==Contact.AccountId}" />
<apex:outputField value="{!Contact.AccountId}" 
        rendered="{!null!=Contact.AccountId}" />

See also

  • The Using field sets recipe in this chapter shows how an administrator can control the editable fields on a Visualforce page.
主站蜘蛛池模板: 和田县| 西乌| 玛纳斯县| 墨脱县| 曲阳县| 黄冈市| 武川县| 永登县| 进贤县| 兴和县| 班戈县| 乐平市| 固始县| 深泽县| 宜阳县| 莫力| 浮山县| 阜新市| 武清区| 湖口县| 夹江县| 遵义县| 洪雅县| 蓝山县| 宜都市| 界首市| SHOW| 旬邑县| 仁寿县| 德安县| 互助| 塔城市| 沙雅县| 馆陶县| 淮南市| 汉川市| 保靖县| 贵南县| 泾源县| 交城县| 开化县|