Hi Friends,
I hope below post is very helpful for the learners and help them to understand the basic CRUD operations from VF page.
In the below example, I explained Insert / Edit/Delete /Clone operations on Account object.
Here are the code snippets for VF Page and Controllers.
Vfpage:-
Name :- DML Vfpage
<apex:page Controller="editdeleteCOn" >
<apex:form >
<apex:sectionHeader title="Edit/Delete" subtitle="Auditing Accounts"/>
<div align='right'>
<apex:commandButton value="Insert New" action="{!insertNew}" style="width:150px"/>
</div>
<br/>
<apex:pageBlock >
<apex:pageBlockTable value="{!lstacc}" var="acc">
<apex:column >
{!acc.name}
<apex:param name="aid" value="{!acc.id}" />
</apex:column>
<apex:column value="{!acc.Website}" />
<apex:column value="{!acc.phone}" />
<apex:column headerValue="Action" >
<apex:commandLink value="Edit" action="{!editCon}">
<apex:param name="cid" value="{!acc.id}" assignTo="{!ecid}"/>
</apex:commandlink>
/
<apex:commandLink value="Delete" action="{!deleteCon}">
<apex:param name="cid" value="{!acc.id}" assignTo="{!dcid}"/>
</apex:commandLink>
/
<apex:commandLink value="Clone" action="{!cloneCon}">
<apex:param name="cid" value="{!acc.id}" assignTo="{!ccid}"/>
</apex:commandLink>
</apex:column>
</apex:pageBlockTable>
<apex:detail subject="{!$CurrentPage.parameters.aid}"/>
</apex:pageBlock>
</apex:form>
</apex:page>
Controller:-
public class editdeleteCOn {
//global declarations
public String ecid{get;set;} // commandlink edit property
public String dcid{get;set;} // commandlink delete property
public String ccid{get;set;} // commandlink clone property
list<Account> lstacc = new list<Account>();
list<Account> lstacc1 = new list<Account>();
// Displaying accounts on vfpage
public list<Account> getlstacc () {
lstacc =[select Name,phone,website from Account];
return lstacc;
}
// To redirect to editpage.
public PageReference editCon() {
pagereference ref = new pagereference('/apex/dataedit?id='+ecid);
ref.setRedirect(False);
return ref;
}
// to delete the selected record
public pagereference deleteCon() {
lstacc1 =[Select id,Name from Account where id=:dcid];
delete lstacc1;
pagereference ref = new pagereference('/apex/editdelete');
ref.setredirect(True);
return ref;
}
// to redirect to insert page.
public PageReference insertNew() {
pagereference ref = new pagereference('/apex/accountinsert');
ref.setRedirect(True);
return ref;
}
// for cloning the records.
public PageReference cloneCon() {
account c = [Select id,Name From account where id =:ccid];
account cloneaccount = c.clone(false);
insert cloneaccount;
pagereference ref = new pagereference('/apex/editdelete');
ref.setRedirect(True);
return ref;
}
}
When you click on Edit link i am redirecting to another vfpage.
Vfpage : dataedit
<apex:page standardController="Account" extensions="savecon">
<apex:form >
<apex:sectionHeader title="Editing Demo" subtitle="Edit"/>
<apex:pageBlock tabStyle="Account" >
<apex:pageBlockButtons location="Bottom">
<apex:commandButton action="{!save}" value="Save"/>
</apex:pageBlockButtons>
<apex:pageBlockSection >
<apex:inputField value="{!account.Name}"/>
<apex:inputField value="{!account.Website}"/>
<apex:inputField value="{!account.phone}"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
Controller:savecon
public with sharing class savecon {
ApexPages.StandardController con;
public savecon(ApexPages.StandardController controller)
con=controller;
}
public pagereference save()
{
con.save();
return new pagereference(page.editdelete.getUrl());
}
}
When you click on insert ,i am displaying another page to insert records from there,
Vfpage: accountinsert
<apex:page standardController="Account" extensions="accountinsertcon">
<apex:form >
<apex:sectionHeader title="Inserting New" subtitle="Account"/>
<apex:pageBlock >
<apex:pageBlockButtons >
<apex:commandButton value="Save" action="{!save}"/>
</apex:pageBlockButtons>
<apex:pageBlockSection >
<apex:inputField value="{!account.Name}"/>
<apex:inputField value="{!account.Phone}"/>
<apex:inputField value="{!account.Website}"/>
<apex:inputField value="{!account.AnnualRevenue}"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
Controller :accountinsertCon
public class accountInsertCOn {
ApexPages.StandardController con;
public accountInsertCOn(ApexPages.StandardController controller) {
con=controller;
}
public pagereference save()
{
con.save();
pagereference ref = new pagereference('/apex/editdelete');
ref.setRedirect(TRUE);
return ref;
}
}
Thank You! I hope This will be useful for one our friends.
I hope below post is very helpful for the learners and help them to understand the basic CRUD operations from VF page.
In the below example, I explained Insert / Edit/Delete /Clone operations on Account object.
Vfpage:-
Name :- DML Vfpage
<apex:page Controller="editdeleteCOn" >
<apex:form >
<apex:sectionHeader title="Edit/Delete" subtitle="Auditing Accounts"/>
<div align='right'>
<apex:commandButton value="Insert New" action="{!insertNew}" style="width:150px"/>
</div>
<br/>
<apex:pageBlock >
<apex:pageBlockTable value="{!lstacc}" var="acc">
<apex:column >
{!acc.name}
<apex:param name="aid" value="{!acc.id}" />
</apex:column>
<apex:column value="{!acc.Website}" />
<apex:column value="{!acc.phone}" />
<apex:column headerValue="Action" >
<apex:commandLink value="Edit" action="{!editCon}">
<apex:param name="cid" value="{!acc.id}" assignTo="{!ecid}"/>
</apex:commandlink>
/
<apex:commandLink value="Delete" action="{!deleteCon}">
<apex:param name="cid" value="{!acc.id}" assignTo="{!dcid}"/>
</apex:commandLink>
/
<apex:commandLink value="Clone" action="{!cloneCon}">
<apex:param name="cid" value="{!acc.id}" assignTo="{!ccid}"/>
</apex:commandLink>
</apex:column>
</apex:pageBlockTable>
<apex:detail subject="{!$CurrentPage.parameters.aid}"/>
</apex:pageBlock>
</apex:form>
</apex:page>
Controller:-
public class editdeleteCOn {
//global declarations
public String ecid{get;set;} // commandlink edit property
public String dcid{get;set;} // commandlink delete property
public String ccid{get;set;} // commandlink clone property
list<Account> lstacc = new list<Account>();
list<Account> lstacc1 = new list<Account>();
// Displaying accounts on vfpage
public list<Account> getlstacc () {
lstacc =[select Name,phone,website from Account];
return lstacc;
}
// To redirect to editpage.
public PageReference editCon() {
pagereference ref = new pagereference('/apex/dataedit?id='+ecid);
ref.setRedirect(False);
return ref;
}
// to delete the selected record
public pagereference deleteCon() {
lstacc1 =[Select id,Name from Account where id=:dcid];
delete lstacc1;
pagereference ref = new pagereference('/apex/editdelete');
ref.setredirect(True);
return ref;
}
// to redirect to insert page.
public PageReference insertNew() {
pagereference ref = new pagereference('/apex/accountinsert');
ref.setRedirect(True);
return ref;
}
// for cloning the records.
public PageReference cloneCon() {
account c = [Select id,Name From account where id =:ccid];
account cloneaccount = c.clone(false);
insert cloneaccount;
pagereference ref = new pagereference('/apex/editdelete');
ref.setRedirect(True);
return ref;
}
}
When you click on Edit link i am redirecting to another vfpage.
Vfpage : dataedit
<apex:page standardController="Account" extensions="savecon">
<apex:form >
<apex:sectionHeader title="Editing Demo" subtitle="Edit"/>
<apex:pageBlock tabStyle="Account" >
<apex:pageBlockButtons location="Bottom">
<apex:commandButton action="{!save}" value="Save"/>
</apex:pageBlockButtons>
<apex:pageBlockSection >
<apex:inputField value="{!account.Name}"/>
<apex:inputField value="{!account.Website}"/>
<apex:inputField value="{!account.phone}"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
Controller:savecon
public with sharing class savecon {
ApexPages.StandardController con;
public savecon(ApexPages.StandardController controller)
con=controller;
}
public pagereference save()
{
con.save();
return new pagereference(page.editdelete.getUrl());
}
}
When you click on insert ,i am displaying another page to insert records from there,
Vfpage: accountinsert
<apex:page standardController="Account" extensions="accountinsertcon">
<apex:form >
<apex:sectionHeader title="Inserting New" subtitle="Account"/>
<apex:pageBlock >
<apex:pageBlockButtons >
<apex:commandButton value="Save" action="{!save}"/>
</apex:pageBlockButtons>
<apex:pageBlockSection >
<apex:inputField value="{!account.Name}"/>
<apex:inputField value="{!account.Phone}"/>
<apex:inputField value="{!account.Website}"/>
<apex:inputField value="{!account.AnnualRevenue}"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
Controller :accountinsertCon
public class accountInsertCOn {
ApexPages.StandardController con;
public accountInsertCOn(ApexPages.StandardController controller) {
con=controller;
}
public pagereference save()
{
con.save();
pagereference ref = new pagereference('/apex/editdelete');
ref.setRedirect(TRUE);
return ref;
}
}
Thank You! I hope This will be useful for one our friends.
No comments:
Post a Comment