You need to sign in to do that
Don't have an account?
Pankaj Yadav 16
How to delete selected records and all records at a time in visual force page.
Please let me know the logic and appriciate your help.
Apex code:
public class ActiveAccAss2
{
private integer totalRecs = 0;
private integer OffsetSize = 0;
private integer LimitSize= 10;
public ActiveAccAss2()
{
totalRecs = [select count() from account];
}
public List<account> getacclist()
{
List<account> acc = Database.Query('SELECT Name, website, AnnualRevenue, description, Type FROM account LIMIT :LimitSize OFFSET :OffsetSize');
System.debug('Values are ' + acc);
return acc;
}
public void FirstPage()
{
OffsetSize = 0;
}
public void previous()
{
OffsetSize = OffsetSize-LimitSize;
}
public void next()
{
OffsetSize = OffsetSize + LimitSize;
}public void LastPage()
{
OffsetSize = totalrecs-math.mod(totalRecs,LimitSize);
}
public boolean getprev()
{
if(OffsetSize == 0)
return true;
else
return false;
}
public boolean getnxt()
{
if((OffsetSize + LimitSize) > totalRecs)
return true;
else
return false;
}
}
Visual force code
<apex:page controller="ActiveAccAss2" >
<apex:form >
<apex:pageBlock id="details">
<apex:pageblockTable value="{!acclist}" var="acc">
<apex:column >
<apex:inputCheckbox />
</apex:column>
<apex:column value="{!acc.Name}"/>
<apex:column value="{!acc.website}"/>
<apex:column value="{!acc.AnnualRevenue}"/>
<apex:column value="{!acc.Description}"/>
<apex:column value="{!acc.Type}"/>
</apex:pageblockTable>
<apex:pageblockButtons >
<apex:commandButton value="Activate/Inactivate"/>
<apex:commandButton value="Delete"/>
</apex:pageblockButtons>
<apex:pageblockButtons >
<apex:commandButton value="First Page" rerender="details" action="{!FirstPage}" disabled="{!prev}"/>
<apex:commandButton value="Previous" rerender="details" action="{!previous}" disabled="{!prev}"/>
<apex:commandButton value="Next" rerender="details" action="{!next}" disabled="{!nxt}"/>
<apex:commandButton value="Last Page" rerender="details" action="{!LastPage}" disabled="{!nxt}"/>"
</apex:pageblockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>
Apex code:
public class ActiveAccAss2
{
private integer totalRecs = 0;
private integer OffsetSize = 0;
private integer LimitSize= 10;
public ActiveAccAss2()
{
totalRecs = [select count() from account];
}
public List<account> getacclist()
{
List<account> acc = Database.Query('SELECT Name, website, AnnualRevenue, description, Type FROM account LIMIT :LimitSize OFFSET :OffsetSize');
System.debug('Values are ' + acc);
return acc;
}
public void FirstPage()
{
OffsetSize = 0;
}
public void previous()
{
OffsetSize = OffsetSize-LimitSize;
}
public void next()
{
OffsetSize = OffsetSize + LimitSize;
}public void LastPage()
{
OffsetSize = totalrecs-math.mod(totalRecs,LimitSize);
}
public boolean getprev()
{
if(OffsetSize == 0)
return true;
else
return false;
}
public boolean getnxt()
{
if((OffsetSize + LimitSize) > totalRecs)
return true;
else
return false;
}
}
Visual force code
<apex:page controller="ActiveAccAss2" >
<apex:form >
<apex:pageBlock id="details">
<apex:pageblockTable value="{!acclist}" var="acc">
<apex:column >
<apex:inputCheckbox />
</apex:column>
<apex:column value="{!acc.Name}"/>
<apex:column value="{!acc.website}"/>
<apex:column value="{!acc.AnnualRevenue}"/>
<apex:column value="{!acc.Description}"/>
<apex:column value="{!acc.Type}"/>
</apex:pageblockTable>
<apex:pageblockButtons >
<apex:commandButton value="Activate/Inactivate"/>
<apex:commandButton value="Delete"/>
</apex:pageblockButtons>
<apex:pageblockButtons >
<apex:commandButton value="First Page" rerender="details" action="{!FirstPage}" disabled="{!prev}"/>
<apex:commandButton value="Previous" rerender="details" action="{!previous}" disabled="{!prev}"/>
<apex:commandButton value="Next" rerender="details" action="{!next}" disabled="{!nxt}"/>
<apex:commandButton value="Last Page" rerender="details" action="{!LastPage}" disabled="{!nxt}"/>"
</apex:pageblockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>
I trust you are doing very well.
Below is the sample code to delete selected and all records, which I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
Custom Object : Course__c
Visualforce:
Controller:
I hope it helps you.
Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in future.
Thanks and Regards,
Khan Anas
http://www.infallibletechie.com/2014/08/deleting-checked-rows-using-apex-in.html
http://www.sandeeparora.org/2014/11/insert-delete-and-update-multiple-records-using-a-custom-controller-in-visualforce/
https://salesforce.stackexchange.com/questions/117058/how-to-delete-the-multiple-record-with-checkbox
https://developer.salesforce.com/page/Wrapper_Class
public class wrapperClassController {
public class GroupDeleteController9 {
public List<WrapperClass> sub {get;set;}
public String key {get;set;}
public Boolean check_box {get;set;}
public List<Account> del {get;set;}
public GroupDeleteController9(){
sub = new List<WrapperClass>();
}
public PageReference search_now(){
for(Course__c cr : [SELECT Name FROM Account WHERE name LIKE :('%'+key+'%')]){
sub.add(new WrapperClass (cr, false));
}
return null;
}
public PageReference delete_now(){
del=new list<Account>();
for(WrapperClass cc: sub){
if(cc.check_box){
del.add(cc.cs);
}
}
delete del;
PageReference pr = new PageReference(System.currentPageReference().getURL());
pr.setRedirect(true);
return pr;
}
public PageReference delete_all(){
del=new list<Account>();
for(WrapperClass cc: sub){
del.add(cc.cs);
}
delete del;
PageReference pr = new PageReference(System.currentPageReference().getURL());
pr.setRedirect(true);
return pr;
}
public class WrapperClass {
public Account cs {get; set;}
public Boolean check_box {get; set;}
public WrapperClass(Account cs, Boolean check_box){
this.cs = cs;
this.check_box = check_box;
}
}
}
}
error screen shot
Yes, you can use it with Account.
Visualforce:
Controller:
I hope it helps you.
Kindly mark this as solved if the information was helpful.
Thanks and Regards,
Khan Anas
Please check your code. You are using 2 classes.
public class wrapperClassController {
public class GroupDeleteController9 {
Remove one class. After that, check the constructor name properly.
Visualforce:
Controller:
I hope it helps you.
Kindly mark this as solved if the information was helpful.
Thanks and Regards,
Khan Anas
Thanks
Thank you so much...!!
I have added above code in My final code.
error i am getting this error please - <apex:pageBlock> must be the direct parent of <apex:pageBlockButtons>
Visualforce code.
Apex code.
You are using <apex:pageBlockButtons> at wrong place on line 14 in visualforce page. Refer below code:
Regards,
Khan Anas
In the constructor, add below line of code:
Like this:
I hope it helps you.
Kindly mark this as solved if it's resolved so that it gets removed from the unanswered queue which results in helping others who are encountering a similar issue.
Regards,
Khan Anas
But unable to the records
https://developer.salesforce.com/forums/ForumsMain?id=9060G0000005oGFQAY