function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ishan K SharmaIshan K Sharma 

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

Error is in expression '{!delAccount}' in component <apex:page> in page assignmentnew 

An unexpected error has occurred. Your development organization has been notified.

 

Can anyone help me with this code.

 

Thanks

Ishan

Best Answer chosen by Admin (Salesforce Developers) 
arjunmarjunm

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&colon;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

Cory CowgillCory Cowgill

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.

s_k_as_k_a

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

arjunmarjunm

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&colon;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

 

 

This was selected as the best answer
Ishan K SharmaIshan K Sharma
Thanks Cory.. it'll be an add on to my concepts
Ishan K SharmaIshan K Sharma

Thanks Arjunm..

its working ..is there any way to do it without using Javascript .

I mean by visualforce only....just for learning purpose..