You need to sign in to do that
Don't have an account?
Delete button not deleting records from list
I have created 2 block in my VF page. one for taking input and the other for displaying list of accounts.
I want edit and delete functionality for each Account record in the list.
I've successfully created everything except the delete functionality of the delete button. The controller and Markup code are below:
Controller:
public class AccountCustom {
public List<account> accountlist = new List<account>();
public Account account{get; set;}
ApexPages.StandardSetController ssc;
public List<account> accdelete = new List<account>();
public String accid{get; set;}
//Constructor
public AccountCustom()
{
accountQuerry();
}
//Method with the querry for the list
public void accountQuerry()
{
accountlist = [SELECT Id, Name, Phone FROM Account];
ApexPages.StandardSetController ssc = new ApexPages.StandardSetController(accountlist);
}
//get Method being called from VF page to display list
public List<account> getAccountList()
{
return accountlist;
}
//Save Method
public void save()
{
account = new Account();
insert account;
accountQuerry();
}
//Method for deleting accounts from the list
public void deleteAccount()
{
accdelete = [SELECT Id, Name, Phone FROM Account WHERE Id = :accid];
if(accdelete.size() > 0)
{
if(accdelete[0].Id != '')
{
delete accdelete;
}
}
accountQuerry();
}
}
VF Page:
<apex:page controller="AccountCustom">
<apex:form >
<apex:pageBlock title="Create/Update Account">
<apex:pageBlockSection >
<apex:inputField value="{!account.Name}"/>
<apex:inputField value="{!account.Phone}"/>
</apex:pageBlockSection>
<apex:commandButton action="{!save}" value="Save" />
</apex:pageBlock>
<apex:pageBlock title="List Of Accounts">
<apex:pageBlockTable value="{!AccountList}" var="ac">
<apex:column width="40">
<apex:outputLink value="/{!ac.id}/e?retURL=%2F{!ac.id}">
Edit
</apex:outputLink>
</apex:column>
<apex:column value="{!ac.Name}"/>
<apex:column value="{!ac.Phone}"/>
<apex:column value="{!ac.id}"/>
<apex:column >
<apex:commandButton action="{!deleteAccount}" value="Delete" immediate="true">
<apex:param value="{!ac.id}" assignTo="{!accid}"/>
</apex:commandButton>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
Please reomve immediate="true" from command button and try.
Thank you for the answer,
but i have done that purposely to bypass the data validations on the page.