SlideShare a Scribd company logo
Demo how to create Visualforce and Apex Controller to
update, delete custom object
Description: Demo how to create visualforce and Apex Controller to load, create and update custom
object.
Author: Tuan Vo (tuanv2t@gmail.com)
Version: 1.0
Contents
Create a custom object Task.........................................................................................................................1
Create a classic Visualforce Page..................................................................................................................7
Show page on a tab.......................................................................................................................................8
Create controller to get Tasks.....................................................................................................................13
Modify Visualforce page to show tasks ......................................................................................................16
Create new visualforce page to add new Task ...........................................................................................19
Create button New Task on the list ............................................................................................................24
Create and page to update existing Task....................................................................................................28
Create a custom object Task
Log in as a Developer Salesforce account.
Create new custom object Task
Click Save
Add some fields for the new custom object Task.
Add Text field Title
Add new Date field Due Date
Add new number field Remaining Hours
Demo how to create visualforce and apex controller to update, delete custom object
Add new number field Priority
Finally, there are some custom fields as below
Create a classic Visualforce Page
Open the Developer Console under Your Name or the quick access menu (Setup gear icon).
The Developer Console opens in a new window.
Click File | New | Visualforce Page.
Enter Task for the name of the new page, and click OK.
A new, blank Visualforce page opens in the Developer Console.
Modify the content
<apex:page>
<h1>This is Task page</h1>
</apex:page>
The content will be updated later.
Show page on a tab
Go to Setup, Type Tab -> Create Tabs
Note: Select the Visualforce tab
Demo how to create visualforce and apex controller to update, delete custom object
Demo how to create visualforce and apex controller to update, delete custom object
Demo how to create visualforce and apex controller to update, delete custom object
Now we need to create controller to retrieve tasks from database and show tasks in page .
Create controller to get Tasks
File > New > Apex Class
Type: TaskController
Copy and paste this code segment into the TaskController
public class TaskController {
public List<Task__c> getTasks(){
List<Task__c> results = Database.query(
'SELECT Id, Title__c, Remaing_Hours__c,Priority__c,Due_Date__c ' +
'FROM Task__c ' +
'ORDER BY Title__c'
);
return results;
}
}
Now the TaskController looks like this
Note: According to the rule of Apex, the method getTasks will allow the code on Visualforce page to use
variable Tasks (See more at https://www.developerforce.com/guides/Visualforce_in_Practice.pdf )
Modify Visualforce page to show tasks
Back to the page Task ( Developer Console -> File > Open Resource )
Find Task.vfp
Modify the source of Task.vfp as below
<apex:page controller="TaskController">
<apex:pageBlock title="Tasks">
<apex:pageBlockTable value="{!Tasks}" var="task">
<apex:column value="{!task.Title__c}"/>
<apex:column value="{!task.Remaing_Hours__c}"/>
<apex:column value="{!task.Priority__c}"/>
<apex:column value="{!task.Due_Date__c}"/>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:page>
Now the Task.vfp looks like this
Now login salesforce, reopen the tab Task page and see how it works
There’s no data now. We will create form to input data.
Create new visualforce page to add new Task
First, it might need to create a new controller that will take responsibility to create new Task
In this case,it’s able to reuse controller TaskController to make it simple.
Because this is a custom controller, it’s necessary to add new public property to make it become a
model
public Task__c newTask {get;set;}
This model must be instanced in the constructor
public TaskController(){
newTask = new Task__c();
}
Create a new public method to receive model and create new Task
public PageReference createNewTask() {
insert newTask;
return new PageReference('/apex/Task?id=' + newTask.Id);
}
The return statement will bring user back to the list whenever the new task has been created.
Nex, create a new Visualforce page from Developer Console, this page will be used as a form to create
new Task.
The content of the page looks like this
<apex:page controller="TaskController ">
<h1>Add Task</h1>
<apex:form>
<apex:pageBlock>
<apex:pageBlockSection columns="1">
<apex:inputField value="{! newTask.Title__c }"/>
<apex:inputField value="{!newTask.Remaing_Hours__c}" />
<apex:inputField value="{!newTask.Priority__c}" />
<apex:inputField value="{!newTask.Due_Date__c}" />
</apex:pageBlockSection>
<apex:pageBlockButtons>
<apex:commandButton action="{!createNewTask}" value="Create" />
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>
It’s able to access to page directly from url such as
https://c.ap4.visual.force.com/apex/TaskNew
And here’s how it looks like
Enter some values and try to create new task
After clicking button Create
Go to the url of the Task like this
https://c.ap4.visual.force.com/apex/Task
(c.ap4 is my domain of my developer account, your domain might be different.
The new created task will be shown on the list.
Until now, the controller TaskController looks like this
public class TaskController {
public TaskController(){
newTask = new Task__c();
}
public List<Task__c> getTasks(){
List<Task__c> results = Database.query(
'SELECT Id, Title__c, Remaing_Hours__c,Priority__c,Due_Date__c ' +
'FROM Task__c ' +
'ORDER BY Title__c'
);
return results;
}
public Task__c newTask {get;set;}
public PageReference createNewTask() {
insert newTask;
return new PageReference('/apex/Task?id=' + newTask.Id);
}
}
There’s no change on the visualforce page Task.vfp
The next step is creating a new button on the list, clicking on that button will bring user to the page to
create Task above
Create button New Task on the list
Modify controller TaskController to add new public method
public PageReference redirectCreateTask()
{
PageReference pr = new PageReference('/apex/TaskNew');
return pr;
}
Open Developer console and open the visualforce page of the Task.vfp
Add new script below
<apex:form >
<apex:commandButton action="{!redirectCreateTask}" value="New Task"/>
</apex:form>
Note, the apex:commandButton must be located inside a apex:form
Now the visualforce page Task.vfp will look like
<apex:page controller="TaskController">
<apex:pageBlock title="Tasks">
<apex:form >
<apex:commandButton action="{!redirectCreateTask}" value="New Task"/>
</apex:form>
<apex:pageBlockTable value="{!Tasks}" var="task">
<apex:column value="{!task.Title__c}"/>
<apex:column value="{!task.Remaing_Hours__c}"/>
<apex:column value="{!task.Priority__c}"/>
<apex:column value="{!task.Due_Date__c}"/>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:page>
Open the tab Task on salesforce to check
The button New Task has appeared.
Clicking on that button will bring user to page to add new task.
Until now, the content of the controller look like this
public class TaskController {
public TaskController(){
newTask = new Task__c();
}
public List<Task__c> getTasks(){
List<Task__c> results = Database.query(
'SELECT Id, Title__c, Remaing_Hours__c,Priority__c,Due_Date__c ' +
'FROM Task__c ' +
'ORDER BY Title__c'
);
return results;
}
public Task__c newTask {get;set;}
public PageReference createNewTask() {
insert newTask;
return new PageReference('/apex/Task?id=' + newTask.Id);
}
public PageReference redirectCreateTask()
{
PageReference pr = new PageReference('/apex/TaskNew');
return pr;
}
}
The next step is creating a new page to Update an existing task.
Create and page to update existing Task
First, it’s able to reuse the controller TaskController ( there’s no need to create a new custom controller
such as TaskUpdatingControler ).
The constructor of the controller will receive the Id of task.
Modify TaskController , add new public Task__c updatedTask to act as a model in the edit page ( the edit
page will be created soon )
public Task__c updatedTask
public TaskController(){
newTask = new Task__c();
//https://www.developerforce.com/guides/Visualforce_in_Practice.pdf
if(ApexPages.currentPage().getParameters().get('id')!=null){
updatedTask = [select Id, Title__c, Remaing_Hours__c,Priority__c,Due_Date__c from Task__c
where id=:ApexPages.currentPage().getParameters().get('id')];
}
}
Add new public method to update the task
public PageReference updateTask () {
update updatedTask;
return new PageReference('/apex/Task');
}
Here’s how the controller looks like
public class TaskController {
public TaskController(){
newTask = new Task__c();
//https://www.developerforce.com/guides/Visualforce_in_Practice.pdf
if(ApexPages.currentPage().getParameters().get('id')!=null){
updatedTask = [select Id, Title__c, Remaing_Hours__c,Priority__c,Due_Date__c from Task__c
where id=:ApexPages.currentPage().getParameters().get('id')];
}
}
public List<Task__c> getTasks(){
List<Task__c> results = Database.query(
'SELECT Id, Title__c, Remaing_Hours__c,Priority__c,Due_Date__c ' +
'FROM Task__c ' +
'ORDER BY Title__c'
);
return results;
}
public Task__c newTask {get;set;}
public PageReference createNewTask() {
insert newTask;
return new PageReference('/apex/Task?id=' + newTask.Id);
}
public PageReference redirectCreateTask()
{
PageReference pr = new PageReference('/apex/TaskNew');
return pr;
}
public Task__c updatedTask {get;set;}
public PageReference updateTask () {
update updatedTask;
return new PageReference('/apex/Task');
}
}
Now create a new visualforce page that will be used to update
Create a new visualforce page , name it TaskUpdate and its content will be
<apex:page controller="TaskController ">
<h1>Edit Task</h1>
<apex:form>
<apex:pageBlock>
<apex:pageBlockSection columns="1">
<apex:inputField value="{! updatedTask.Title__c }"/>
<apex:inputField value="{!updatedTask.Remaing_Hours__c}" />
<apex:inputField value="{!updatedTask.Priority__c}" />
<apex:inputField value="{!updatedTask.Due_Date__c}" />
</apex:pageBlockSection>
<apex:pageBlockButtons>
<apex:commandButton action="{!updateTask}" value="Save" />
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>
Now modify the home Task.vfp page, add new link Edit, clicking on that link will bring user to the edit
page
<apex:page controller="TaskController">
<apex:pageBlock title="Tasks">
<apex:form >
<apex:commandButton action="{!redirectCreateTask}" value="New Task"/>
</apex:form>
<apex:pageBlockTable value="{!Tasks}" var="task">
<apex:column>
<apex:outputLink
value="TaskUpdate?id={!task.Id}">
Edit
</apex:outputLink>
</apex:column>
<apex:column value="{!task.Title__c}"/>
<apex:column value="{!task.Remaing_Hours__c}"/>
<apex:column value="{!task.Priority__c}"/>
<apex:column value="{!task.Due_Date__c}"/>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:page>
Run it
Click on Edit of an record
Make change something for this task and click Save, user will be brought back to the list
Demo how to create visualforce and apex controller to update, delete custom object

More Related Content

PPTX
Achieving quality contraints
PDF
Android Mobile App Development Proposal PowerPoint Presentation Slides
PPTX
Designing applications with web access capabilities
PDF
Introduction to ReactJS
PDF
Call Center Process Flow Charts: Templates For All Types From Time Doctor
PDF
Delitos sexuales jennifer galindo
PDF
Creo Color Management Settings
PPTX
Achieving quality contraints
Android Mobile App Development Proposal PowerPoint Presentation Slides
Designing applications with web access capabilities
Introduction to ReactJS
Call Center Process Flow Charts: Templates For All Types From Time Doctor
Delitos sexuales jennifer galindo
Creo Color Management Settings

Similar to Demo how to create visualforce and apex controller to update, delete custom object (20)

PDF
Demo how to create visualforce page and custom controller via developer console
PDF
Visualforce Workbook
PDF
Intro to Apex Programmers
PDF
Workbook vf
PPTX
Getting a Quick Start with Visualforce
PPTX
SFDC UI - Introduction to Visualforce
PPTX
Coding Apps in the Cloud with Force.com - Part I
PPTX
Hands-On Workshop: Introduction to Coding for on Force.com for Admins and Non...
PPTX
Coding Apps in the Cloud with Force.com - Part 2
PPTX
Advanced Apex Webinar
PPTX
ELEVATE Paris
PPTX
Dive Deep into Apex: Advanced Apex!
PPT
Salesforce1 Platform for programmers
PDF
Salesforce Developer Online Training.pdf
PPTX
Hands-On Workshop: Introduction to Development on Force.com for Developers
PDF
Introduction to Visualforce
DOC
Salesforce crm training
PDF
Programming Building Blocks for Admins
PPTX
#CNX14 - Intro to Force
PPTX
Salesforce online training -GoLogica
Demo how to create visualforce page and custom controller via developer console
Visualforce Workbook
Intro to Apex Programmers
Workbook vf
Getting a Quick Start with Visualforce
SFDC UI - Introduction to Visualforce
Coding Apps in the Cloud with Force.com - Part I
Hands-On Workshop: Introduction to Coding for on Force.com for Admins and Non...
Coding Apps in the Cloud with Force.com - Part 2
Advanced Apex Webinar
ELEVATE Paris
Dive Deep into Apex: Advanced Apex!
Salesforce1 Platform for programmers
Salesforce Developer Online Training.pdf
Hands-On Workshop: Introduction to Development on Force.com for Developers
Introduction to Visualforce
Salesforce crm training
Programming Building Blocks for Admins
#CNX14 - Intro to Force
Salesforce online training -GoLogica
Ad

Recently uploaded (20)

PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Empathic Computing: Creating Shared Understanding
PDF
Modernizing your data center with Dell and AMD
PDF
Sensors and Actuators in IoT Systems using pdf
PPTX
Spectroscopy.pptx food analysis technology
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPT
Teaching material agriculture food technology
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Dropbox Q2 2025 Financial Results & Investor Presentation
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
20250228 LYD VKU AI Blended-Learning.pptx
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Review of recent advances in non-invasive hemoglobin estimation
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Empathic Computing: Creating Shared Understanding
Modernizing your data center with Dell and AMD
Sensors and Actuators in IoT Systems using pdf
Spectroscopy.pptx food analysis technology
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Network Security Unit 5.pdf for BCA BBA.
Diabetes mellitus diagnosis method based random forest with bat algorithm
The AUB Centre for AI in Media Proposal.docx
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Teaching material agriculture food technology
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Ad

Demo how to create visualforce and apex controller to update, delete custom object

  • 1. Demo how to create Visualforce and Apex Controller to update, delete custom object Description: Demo how to create visualforce and Apex Controller to load, create and update custom object. Author: Tuan Vo ([email protected]) Version: 1.0 Contents Create a custom object Task.........................................................................................................................1 Create a classic Visualforce Page..................................................................................................................7 Show page on a tab.......................................................................................................................................8 Create controller to get Tasks.....................................................................................................................13 Modify Visualforce page to show tasks ......................................................................................................16 Create new visualforce page to add new Task ...........................................................................................19 Create button New Task on the list ............................................................................................................24 Create and page to update existing Task....................................................................................................28 Create a custom object Task Log in as a Developer Salesforce account. Create new custom object Task
  • 3. Add some fields for the new custom object Task. Add Text field Title Add new Date field Due Date
  • 4. Add new number field Remaining Hours
  • 6. Add new number field Priority Finally, there are some custom fields as below
  • 7. Create a classic Visualforce Page Open the Developer Console under Your Name or the quick access menu (Setup gear icon). The Developer Console opens in a new window. Click File | New | Visualforce Page. Enter Task for the name of the new page, and click OK. A new, blank Visualforce page opens in the Developer Console.
  • 8. Modify the content <apex:page> <h1>This is Task page</h1> </apex:page> The content will be updated later. Show page on a tab Go to Setup, Type Tab -> Create Tabs
  • 9. Note: Select the Visualforce tab
  • 13. Now we need to create controller to retrieve tasks from database and show tasks in page . Create controller to get Tasks File > New > Apex Class
  • 15. Copy and paste this code segment into the TaskController public class TaskController { public List<Task__c> getTasks(){ List<Task__c> results = Database.query( 'SELECT Id, Title__c, Remaing_Hours__c,Priority__c,Due_Date__c ' + 'FROM Task__c ' + 'ORDER BY Title__c' ); return results; } } Now the TaskController looks like this
  • 16. Note: According to the rule of Apex, the method getTasks will allow the code on Visualforce page to use variable Tasks (See more at https://www.developerforce.com/guides/Visualforce_in_Practice.pdf ) Modify Visualforce page to show tasks Back to the page Task ( Developer Console -> File > Open Resource ) Find Task.vfp
  • 17. Modify the source of Task.vfp as below <apex:page controller="TaskController"> <apex:pageBlock title="Tasks"> <apex:pageBlockTable value="{!Tasks}" var="task"> <apex:column value="{!task.Title__c}"/> <apex:column value="{!task.Remaing_Hours__c}"/> <apex:column value="{!task.Priority__c}"/> <apex:column value="{!task.Due_Date__c}"/> </apex:pageBlockTable> </apex:pageBlock> </apex:page> Now the Task.vfp looks like this
  • 18. Now login salesforce, reopen the tab Task page and see how it works
  • 19. There’s no data now. We will create form to input data. Create new visualforce page to add new Task First, it might need to create a new controller that will take responsibility to create new Task In this case,it’s able to reuse controller TaskController to make it simple. Because this is a custom controller, it’s necessary to add new public property to make it become a model public Task__c newTask {get;set;} This model must be instanced in the constructor public TaskController(){ newTask = new Task__c(); } Create a new public method to receive model and create new Task public PageReference createNewTask() { insert newTask; return new PageReference('/apex/Task?id=' + newTask.Id); } The return statement will bring user back to the list whenever the new task has been created. Nex, create a new Visualforce page from Developer Console, this page will be used as a form to create new Task.
  • 20. The content of the page looks like this <apex:page controller="TaskController "> <h1>Add Task</h1> <apex:form> <apex:pageBlock> <apex:pageBlockSection columns="1"> <apex:inputField value="{! newTask.Title__c }"/> <apex:inputField value="{!newTask.Remaing_Hours__c}" /> <apex:inputField value="{!newTask.Priority__c}" /> <apex:inputField value="{!newTask.Due_Date__c}" /> </apex:pageBlockSection> <apex:pageBlockButtons>
  • 21. <apex:commandButton action="{!createNewTask}" value="Create" /> </apex:pageBlockButtons> </apex:pageBlock> </apex:form> </apex:page> It’s able to access to page directly from url such as https://c.ap4.visual.force.com/apex/TaskNew And here’s how it looks like
  • 22. Enter some values and try to create new task
  • 23. After clicking button Create Go to the url of the Task like this https://c.ap4.visual.force.com/apex/Task (c.ap4 is my domain of my developer account, your domain might be different. The new created task will be shown on the list. Until now, the controller TaskController looks like this public class TaskController { public TaskController(){ newTask = new Task__c(); }
  • 24. public List<Task__c> getTasks(){ List<Task__c> results = Database.query( 'SELECT Id, Title__c, Remaing_Hours__c,Priority__c,Due_Date__c ' + 'FROM Task__c ' + 'ORDER BY Title__c' ); return results; } public Task__c newTask {get;set;} public PageReference createNewTask() { insert newTask; return new PageReference('/apex/Task?id=' + newTask.Id); } } There’s no change on the visualforce page Task.vfp The next step is creating a new button on the list, clicking on that button will bring user to the page to create Task above Create button New Task on the list Modify controller TaskController to add new public method public PageReference redirectCreateTask() { PageReference pr = new PageReference('/apex/TaskNew');
  • 25. return pr; } Open Developer console and open the visualforce page of the Task.vfp Add new script below <apex:form > <apex:commandButton action="{!redirectCreateTask}" value="New Task"/> </apex:form> Note, the apex:commandButton must be located inside a apex:form Now the visualforce page Task.vfp will look like <apex:page controller="TaskController"> <apex:pageBlock title="Tasks"> <apex:form > <apex:commandButton action="{!redirectCreateTask}" value="New Task"/> </apex:form> <apex:pageBlockTable value="{!Tasks}" var="task"> <apex:column value="{!task.Title__c}"/> <apex:column value="{!task.Remaing_Hours__c}"/> <apex:column value="{!task.Priority__c}"/> <apex:column value="{!task.Due_Date__c}"/> </apex:pageBlockTable> </apex:pageBlock> </apex:page>
  • 26. Open the tab Task on salesforce to check
  • 27. The button New Task has appeared. Clicking on that button will bring user to page to add new task. Until now, the content of the controller look like this public class TaskController { public TaskController(){ newTask = new Task__c(); } public List<Task__c> getTasks(){ List<Task__c> results = Database.query( 'SELECT Id, Title__c, Remaing_Hours__c,Priority__c,Due_Date__c ' +
  • 28. 'FROM Task__c ' + 'ORDER BY Title__c' ); return results; } public Task__c newTask {get;set;} public PageReference createNewTask() { insert newTask; return new PageReference('/apex/Task?id=' + newTask.Id); } public PageReference redirectCreateTask() { PageReference pr = new PageReference('/apex/TaskNew'); return pr; } } The next step is creating a new page to Update an existing task. Create and page to update existing Task First, it’s able to reuse the controller TaskController ( there’s no need to create a new custom controller such as TaskUpdatingControler ). The constructor of the controller will receive the Id of task.
  • 29. Modify TaskController , add new public Task__c updatedTask to act as a model in the edit page ( the edit page will be created soon ) public Task__c updatedTask public TaskController(){ newTask = new Task__c(); //https://www.developerforce.com/guides/Visualforce_in_Practice.pdf if(ApexPages.currentPage().getParameters().get('id')!=null){ updatedTask = [select Id, Title__c, Remaing_Hours__c,Priority__c,Due_Date__c from Task__c where id=:ApexPages.currentPage().getParameters().get('id')]; } } Add new public method to update the task public PageReference updateTask () { update updatedTask; return new PageReference('/apex/Task'); } Here’s how the controller looks like public class TaskController { public TaskController(){ newTask = new Task__c(); //https://www.developerforce.com/guides/Visualforce_in_Practice.pdf if(ApexPages.currentPage().getParameters().get('id')!=null){ updatedTask = [select Id, Title__c, Remaing_Hours__c,Priority__c,Due_Date__c from Task__c where id=:ApexPages.currentPage().getParameters().get('id')]; }
  • 30. } public List<Task__c> getTasks(){ List<Task__c> results = Database.query( 'SELECT Id, Title__c, Remaing_Hours__c,Priority__c,Due_Date__c ' + 'FROM Task__c ' + 'ORDER BY Title__c' ); return results; } public Task__c newTask {get;set;} public PageReference createNewTask() { insert newTask; return new PageReference('/apex/Task?id=' + newTask.Id); } public PageReference redirectCreateTask() { PageReference pr = new PageReference('/apex/TaskNew'); return pr; } public Task__c updatedTask {get;set;} public PageReference updateTask () { update updatedTask;
  • 31. return new PageReference('/apex/Task'); } } Now create a new visualforce page that will be used to update Create a new visualforce page , name it TaskUpdate and its content will be <apex:page controller="TaskController "> <h1>Edit Task</h1> <apex:form> <apex:pageBlock> <apex:pageBlockSection columns="1"> <apex:inputField value="{! updatedTask.Title__c }"/> <apex:inputField value="{!updatedTask.Remaing_Hours__c}" /> <apex:inputField value="{!updatedTask.Priority__c}" /> <apex:inputField value="{!updatedTask.Due_Date__c}" /> </apex:pageBlockSection> <apex:pageBlockButtons> <apex:commandButton action="{!updateTask}" value="Save" /> </apex:pageBlockButtons> </apex:pageBlock> </apex:form> </apex:page>
  • 32. Now modify the home Task.vfp page, add new link Edit, clicking on that link will bring user to the edit page
  • 33. <apex:page controller="TaskController"> <apex:pageBlock title="Tasks"> <apex:form > <apex:commandButton action="{!redirectCreateTask}" value="New Task"/> </apex:form> <apex:pageBlockTable value="{!Tasks}" var="task"> <apex:column> <apex:outputLink value="TaskUpdate?id={!task.Id}"> Edit </apex:outputLink> </apex:column> <apex:column value="{!task.Title__c}"/> <apex:column value="{!task.Remaing_Hours__c}"/> <apex:column value="{!task.Priority__c}"/> <apex:column value="{!task.Due_Date__c}"/> </apex:pageBlockTable> </apex:pageBlock> </apex:page> Run it
  • 34. Click on Edit of an record
  • 35. Make change something for this task and click Save, user will be brought back to the list