0

I create delete-button to remove records from my list. First I choose records and add them to this list. Then I can delete some records, but it removes just last one. How to delete only selected record from the list?

public void deleteObject() {
    for (Integer i=0; i < someList.size(); i++){
        Job_Advertisement__c j = someList[i];
        if(j.Id == objId){
            someList.remove(i);
        }
    }
}

VF:

 <apex:repeat value="{!someList}" var="list">
    <div class="div-smallpanel-label">
        <apex:outputText value="{!list.Name}"/>
        <apex:commandButton value="X" action="{!deleteObject}" reRender="selectedObjs"> 
            <apex:param value="{!list.Id}" assignTo="{!objId}"/>
        </apex:commandButton>
     </div>
</apex:repeat>
1

apex:commandButton does not support apex:param, and will have unexpected behavior. You would need to use one of the supported elements instead. Please read the documentation for apex:param. The supported elements are:

<apex:actionFunction>
<apex:actionSupport>
<apex:commandLink>
<apex:outputLink>
<apex:outputText>
<flow:interview>
|improve this answer|||||
  • Even if I use <apex:commandLink>, I can delete with it just last one element from the list (the last one I added). – MichaelLev19 Nov 6 '18 at 14:36
0

You assign param value of selected record id to just one {!objId} value, so you take only last chosen element.

You have to use list of "wrapped" custom records with Boolean param that will get its' value on user check.

|improve this answer|||||
0

Apart from all other issues as mentioned above, the reason only why your last item from the list is getting removed seems to be because you are assigning the id of the last item in the list in the variable objId.

Note, that you are assigning the value to the variable as:

<apex:param value="{!list.Id}" assignTo="{!objId}"/>

and that this code itself is in an iteration within <apex:repeat>. So when the last element is being rendered, list.Id will be always have the id of the last element within your iteration and will get assigned to the objId. So whenever you call delete with that value, it will always delete the last item.

You need to make sure you set the objId specifically to the value that you need to delete.

|improve this answer|||||
  • this is true for apex:variable where the last invocation of the tag will be the value, but not for apex:param tags where the value that's passed to the back-end will be based on the parent that was clicked (ignoring the fact apex:commandbutton doesn't support parameters as sfdc fox pointed out) – Ralph Callaway Nov 6 '18 at 22:37
  • Haven’t really tried this out but at first instance that seemed to be the issue. – Jayant Das Nov 6 '18 at 23:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.