You need to sign in to do that
Don't have an account?
how to delete records using custom controller
VF page
<apex:page Controller="Assignmentnew">
<apex:panelGrid columns="2">
<apex:form id="all" >
<apex:pageBlock >
<apex:pageBlockButtons location="top" >
<apex:commandButton value="new" action="{!doNewrecord}"/>
</apex:pageBlockButtons>
<apex:pageBlockTable value="{!accList}" var="a">
<apex:column headerValue="Name">
<apex:outputLink value="/{!a.id}/d?retURL=/apex/AssignmentNew">
{!a.name}
</apex:outputLink>
</apex:column>
<apex:column headerValue="Buttons">
<apex:outputLink value="/{!a.id}/e?retURL=/apex/{!a.Name}">
Edit
</apex:outputLink>
<apex:commandLink action="{!delAccount}" reRender="all">Del
<apex:param value="{!a.Id}" assignTo="{!SelectedAccountId} "/>
</apex:commandLink >
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:panelGrid>
</apex:page>
Class ::
public class Assignmentnew
{
public List<Account> accList {get; set;}
public string SelectedAccountId {get; set;}
public Assignmentnew()
{
setupAccount();
}
public void setupAccount()
{
accList = [Select name, id from Account ];
}
public Pagereference doNewrecord()
{
Pagereference newRecord= new Pagereference ('/001/e?retURL=/001/');
return newRecord;
}
public PageReference delAccount()
{
accList = [select id,name from account where id=:SelectedAccountId];
if(accList .size() > 0 || accList[0].Id != ''){
delete accList;
}
setupAccount();
return null;
}
}
Error:
List index out of bounds: 0
An unexpected error has occurred. Your development organization has been notified.
Can anyone help me with this code.
Thanks
Ishan
hi
page
<apex:page Controller="Assignmentnew">
<apex:panelGrid columns="2">
<apex:form id="all" >
<apex:pageBlock >
<apex:pageBlockButtons location="top" >
<apex:commandButton value="new" action="{!doNewrecord}"/>
</apex:pageBlockButtons>
<apex:pageBlockTable value="{!accList}" var="a">
<apex:column headerValue="Name">
<apex:outputLink value="/{!a.id}/d?retURL=/apex/AssignmentNew">
{!a.name}
</apex:outputLink>
</apex:column>
<apex:column headerValue="Buttons">
<apex:outputLink value="/{!a.id}/e?retURL=/apex/{!a.Name}">
Edit
</apex:outputLink>
<a href="javascript:if (window.confirm('Are you sure?')) DeleteAccount('{!a.Id}');" style="font-weight:bold">Del</a>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
<apex:actionFunction action="{!DeleteAccount}" name="DeleteAccount" reRender="all" >
<apex:param name="accountid" value="" assignTo="{!SelectedAccountId}"/>
</apex:actionFunction>
</apex:form>
</apex:panelGrid>
</apex:page>
controller
public class Assignmentnew
{
public List<Account> accList {get; set;}
public string SelectedAccountId {get; set;}
public Assignmentnew()
{
setupAccount();
}
public void setupAccount()
{
accList = [Select name, id from Account ];
}
public Pagereference doNewrecord()
{
Pagereference newRecord= new Pagereference ('/001/e?retURL=/001/');
return newRecord;
}
public void DeleteAccount()
{
Account del = new Account(ID=SelectedAccountId);
delete del;
setupAccount();
}
}
Thanks,
arjun
All Answers
Change This:
if(accList .size() > 0 || accList[0].Id != ''){
to this:
if(accList .size() > 0){
Why expection:
Size is 0, but you are trying to acces the first element accList[0] in the if condition. Since it is size 0 it throws out of bounds exception.
Hi,
See the below blog this is more similar to your requirement
http://salesforcesource.blogspot.com/2009/09/edit-and-delete-command-for-your.html
hi
page
<apex:page Controller="Assignmentnew">
<apex:panelGrid columns="2">
<apex:form id="all" >
<apex:pageBlock >
<apex:pageBlockButtons location="top" >
<apex:commandButton value="new" action="{!doNewrecord}"/>
</apex:pageBlockButtons>
<apex:pageBlockTable value="{!accList}" var="a">
<apex:column headerValue="Name">
<apex:outputLink value="/{!a.id}/d?retURL=/apex/AssignmentNew">
{!a.name}
</apex:outputLink>
</apex:column>
<apex:column headerValue="Buttons">
<apex:outputLink value="/{!a.id}/e?retURL=/apex/{!a.Name}">
Edit
</apex:outputLink>
<a href="javascript:if (window.confirm('Are you sure?')) DeleteAccount('{!a.Id}');" style="font-weight:bold">Del</a>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
<apex:actionFunction action="{!DeleteAccount}" name="DeleteAccount" reRender="all" >
<apex:param name="accountid" value="" assignTo="{!SelectedAccountId}"/>
</apex:actionFunction>
</apex:form>
</apex:panelGrid>
</apex:page>
controller
public class Assignmentnew
{
public List<Account> accList {get; set;}
public string SelectedAccountId {get; set;}
public Assignmentnew()
{
setupAccount();
}
public void setupAccount()
{
accList = [Select name, id from Account ];
}
public Pagereference doNewrecord()
{
Pagereference newRecord= new Pagereference ('/001/e?retURL=/001/');
return newRecord;
}
public void DeleteAccount()
{
Account del = new Account(ID=SelectedAccountId);
delete del;
setupAccount();
}
}
Thanks,
arjun
Thanks Arjunm..
its working ..is there any way to do it without using Javascript .
I mean by visualforce only....just for learning purpose..