+ Start a Discussion
Ryan McNeelyRyan McNeely 

When user clicks the Delete Button, the product is not getting deleted from the visualforce page

Hi All,
I'm having a hard time with the controller code for my visualforce Delete Button. When the user clicks the 'X', that entire row should disappear from the view, and the object should be deleted. I've been through so many wikis on the topic. You're help would be appreciated.
(I'd also like to know how to replace the 'X' with a nifty x button .png or similar)

User-added image
<apex:page controller="SockWarehouseController" tabStyle="Sock__c">
  <h1>Ocean Socks</h1>
      <apex:form >
          <apex:pageBlock title="Inventory"> 
              <apex:pageBlockSection columns="1">
                  <apex:pageBlockTable value="{!products}" var="pitem">
                        <apex:column headerValue="" >
                            <apex:commandButton action="{!deleteProduct}" value="X"/>
                        </apex:column>
                        <apex:column headerValue="Product" >                         
                            <apex:outputText value="{!pitem.Name}"/>
                        </apex:column>
                        <apex:column headerValue="Quantity">
                            <apex:outputText value="{!pitem.Quantity__c}"/>
                        </apex:column>
                         <apex:column headerValue="Price">
                            <apex:outputText value="{!pitem.Price__c}"/>
                        </apex:column>
                  </apex:pageBlockTable>
              </apex:pageBlockSection>
          </apex:pageBlock>
      </apex:form>
</apex:page>
 
public class SockWarehouseController {

    List<Sock__c> products;
    
    public List<Sock__c> getProducts() {
        if(products == null) {
           products = [SELECT id, Name, Quantity__c, Price__c FROM Sock__c];
        }
        return products;
    }
    
    public PageReference deleteProduct() {
        String socksId = ApexPages.currentPage().getParameters().get('id');
        System.debug(socksId);
        Sock__c sockObject = [Select id from Sock__c where id=:socksId];
        delete sockObject ;
        return null;
    }
}

 
Best Answer chosen by Ryan McNeely
Chris  ByromChris Byrom
You need to add a parameter to your command button. Here is an older thread that shows how to do it. The problem now is that your deleteProduct method does not know which item you want to delete. Passing the id of the product you want to delete to the method will allow your to delete the correct item.

https://developer.salesforce.com/forums/?id=906F000000095uXIAQ

All Answers

Chris  ByromChris Byrom
You need to add a parameter to your command button. Here is an older thread that shows how to do it. The problem now is that your deleteProduct method does not know which item you want to delete. Passing the id of the product you want to delete to the method will allow your to delete the correct item.

https://developer.salesforce.com/forums/?id=906F000000095uXIAQ
This was selected as the best answer
Ryan McNeelyRyan McNeely
Params did it for me. Here's the final code.
public class SockWarehouseController {

    List<Sock__c> products;
    List<Sock__c> productsAfterDelete {get; set;}
    List<Sock__c> productsToDelete {get; set;}
    public String userinput{get; set;}
    String pitemId;
    
    public String getpitemId(){
        return pitemId;
    }

    public void setpitemId(String pitemId){
        this.pitemId = pitemId;
    }

    public List<Sock__c> getProducts() {
        products = [SELECT id, Name, Quantity__c, Price__c FROM Sock__c];
        return products;
    }
    
    public PageReference save() {
        update products;
        return null;
    }
    
    public void deleteProduct(){
        productsToDelete = [Select id FROM Sock__c where id = :pitemId];
        delete productsToDelete;
        getProducts();
    }
}
 
<apex:page controller="SockWarehouseController" tabStyle="Inventory__tab">
      <head>
          <style>
              .odd {
                  background-color: #f0f0f5;
              }
              .even {
                  background-color: #e6faff;
              }
          </style>
      </head>
      <apex:form >
          <apex:pageBlock title="Socks Inventory"> 
              <apex:pageBlockSection columns="1">
                  <apex:pageBlockTable value="{!products}" var="pitem" id="panel">
                        <apex:column >                         
                            <apex:commandLink action="{!deleteProduct}" reRender="panel">Del
                                <apex:param value="{!pitem.Id}" name="idToDel" assignTo="{!pitemId}"/>
                            </apex:commandLink>
                        </apex:column>
                        <apex:column headerValue="Product" >                         
                            <apex:outputText value="{!pitem.Name}"/>
                        </apex:column>
                        <apex:column headerValue="Quantity">
                               <apex:inputField value="{!pitem.Quantity__c}" onkeypress="{!userinput}"/>  
                        </apex:column>
                         <apex:column headerValue="Price">
                            <apex:outputText value="{!pitem.Price__c}"/>
                        </apex:column>
                  </apex:pageBlockTable>
                  <apex:commandButton action="{!save}" value="Save"/>
              </apex:pageBlockSection>
          </apex:pageBlock>
      </apex:form>
</apex:page>