Find your content:

Search form

You are here

sending ids to controller and saving..?

<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

Attribution to: Mik

Possible Suggestion/Solution #1

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)

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

Attribution to: Bob Buzzard

Possible Suggestion/Solution #2

@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){
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 !!!

Attribution to: Rao
This content is remixed from stackoverflow or stackexchange. Please visit

My Block Status

My Block Content