0
<apex:commandlink action="{!removeline}" onclick="if(confirm('Are you sure?')) "   reRender="thePB" target="_top" > <img src="{!$Resource.Red_Cross}" alt="Remove" title="Remove" />
<apex:param name="deleteid" value="{!op.Id}" assignTo="{!deleteid}"/>
</apex:commandlink >

I have cross image in front of every record.on click of that i am sending id to the controller.but the problem is when i click on cross 2 records and then click on delete button only 1 record is deleted.Can anyone suggest me how to save ids(more than 1 record) in controller and delete records on delete click.I am learning salesforce

2 Answers 2

2

I would approach this a different way: to allow it to scale to a large number of records I would have each record contained in a wrapper class with an additional boolean property to indicate if it should be deleted. This boolean property is exposed in the page as a checkbox. When the delete method is called, the controller can iterate the wrapper classes and delete all of those that have the boolean property set to true.

I believe the above approach is the better way, as you aren't carrying out a postback whenever the user selects an item to delete, rather you have a single postback when the user clicks the delete button, so the user will spend a lot less time waiting for round trips. However, if you have to go this route, it should be as simple as changing your setDeleteId method to append to a list of ids rather than overwriting, as the assignTo attribute simply executes the setter for the property. Something like:

List<Id> idsToDelete=new List<Id>();

public void setDeleteId(String theId)
{
   idsToDelete.add(theId);
}

and then in your delete action method, pass the list of ids into the delete method, e.g.

public PageReference delete()
{
    delete idsToDelete;

    return null; // or wherever you are taking them
}
2
  • thanks bob for answering..but is there any way i save save my deleteids in some list or something and then i can delete list on delete button...
    – Mik
    Nov 14, 2012 at 17:40
  • I've updated my answer to show how that could be done - I still think its not a great user experience though. Nov 15, 2012 at 8:26
0

@user1785489 Taking a try : If you want to build a list every time you click the X mark are you getting the ID related to the row you are clicking?

If this is true in your controller build a string something like

string Ids_todelete ='' ; 
Ids_todelete = Ids_todelete+deleteid ;


database.deleteresult[] DL = database.delete(Ids_todelete,true);

for(database.deleteresult  D : DL){
if(D.Issuccess(){
Ids_todelete = '';
}
}

this way when you click on the X mark the controller's deleteid variable will be getting new values and the Ids_to_delete will be appended with the new Id's from the page. Finally you will have a list of comma seperated Id's and then you can use a In clause and get the records to delete

Hope this helps !!!

2
  • 1
    In your code all deletes will be succesful or all will fail ;) Did you mean Database.delete(alist, true)?
    – eyescream
    Nov 15, 2012 at 9:35
  • yes it should have true attribute atleast to allow partial processing,thanks for correction@eyescream :). I think as bob pointed I do not even need the query to get the record just a database.delete should do :/
    – Rao
    Nov 15, 2012 at 9:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .