function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
BeautifulDrifterBeautifulDrifter 

Trouble Deleting Record from PageBlockTable

Need help here.  I have a record within a PageBlockTable and added a button to delete the record.  I am doing this on another tab of the same page and it is working fine.  For some reason I cannot get this to work.

Pulling the primary data for the table
 
Private void setAppDetail(){
        AppDetails = [select Id,Name,PSQ_Application__c,Instances__c from PSQ_App_Detail__c where PSQ_Header__c = :Header.Id];
        
        
    }

Here is the VF:
 
<apex:pageBlock title="Line Items" id="AppLineItems">
                        <apex:pageBlockTable value="{!AppDetails}" id="appDet" var="ad">
                        <apex:column title="Name" value="{!ad.Name}"/>
                        <apex:column title="Socket" value="{!ad.PSQ_Application__c}"/>
                        <apex:column title="Instances" value="{!ad.Instances__c}"/>
                        <apex:column >
                        <apex:commandButton value="Remove App" action="{!removeApp}" id="removeApp" reRender="AppLineItems" >
                            <apex:param name="AppId" value="{!ad.id}" assignTo="{!AppId}"/>
                        </apex:commandButton>
                        </apex:column>
                    </apex:pageBlockTable>
                </apex:pageBlock>

Below is the Apex for the action.  You can also see all of the different ways I have tried to delete this record (marked out).  
 
//public PSQ_App_Detail__c AppId {get; set;}
    public string AppId {get; set;}
    public PageReference removeApp() 
    {
        //PSQ_App_Detail__c deleteApp = AppId;
        PSQ_App_Detail__c deleteApp = new PSQ_App_Detail__c(Id = AppId);
        //PSQ_App_Detail__c DeleteApp = [select Id from PSQ_App_Detail__c where Id = :AppId];
        //delete AppId;
                
        delete DeleteApp;
                
        setAppDetail();
        
        return null;
    }

Any help would be much appreciated.  Thanks in advance!
 
Best Answer chosen by BeautifulDrifter
BeautifulDrifterBeautifulDrifter
OK...I figured it out.  I had a required field in a different portion of the page which was throwning an error when I was pressing the button.  Basically, I have an SOQL statement within my getter and the error was throwing on the required field before the controller was executing and populating it.  At least that's what I believe was happening.  The rerender was actually rerendering the page to I wasn't seeing it.  There are a bunch of questions out there about rerendering and using buttons in the visualforce page.  

For simplicities sake, I removed the required field and now it is working.  

All Answers

Alain CabonAlain Cabon
Hi,

" I am doing this on another tab of the same page and it is working fine..." exactly the same or with <apex:commandLink> ?

http://blog.jeffdouglas.com/2010/03/04/passing-parameters-with-a-commandbutton/ (https://th3silverlining.com/2009/06/12/salesforce-bugs-you/)

https://th3silverlining.com/2009/06/12/salesforce-bugs-you/


 
Alain CabonAlain Cabon
http://salesforce.stackexchange.com/questions/56720/passing-parameter-through-commandbutton-is-null-value
BeautifulDrifterBeautifulDrifter
Below is the code for the other Tab and as I said, this one is working fine.  

VF
<apex:tab label="Categories2"  id="tabCategories2">
            <apex:form id="CatForm">
                <apex:pageBlock title="Categories">
                    <apex:pageBlockTable id="list" value="{!catsList}" var="rc">
                        <apex:column value="{!rc.PSQ_Category__c}"/>
                        <apex:column value="{!rc.Need__c}"/>
                        <apex:column >
                            <apex:commandButton value="Add" action="{!addRecord}" id="add" rerender="list, catqTab">
                                <apex:param name="RecToAdd" value="{!rc.id}" assignTo="{!addRec}"/>
                            </apex:commandButton>
                        </apex:column>
                        <apex:column >
                            <apex:commandButton value="Remove" action="{!removeRecord}" id="remove" reRender="list, catqTab">
                                <apex:param name="RecToRemove" value="{!rc.id}" assignTo="{!removeRec}"/>
                            </apex:commandButton>
                        </apex:column>
                        <apex:inlineEditSupport event="ondblClick" showOnEdit="saveButton,cancelButton" hideOnEdit="editButton" />
                    </apex:pageBlockTable>
                </apex:pageBlock>
                <apex:pageBlock >
                </apex:pageBlock>
            </apex:form>
        </apex:tab>
APEX
private void setCatsList()
    {
        catslist=[select id,PSQ_Category__c, Name, Need__c from PSQ_Record_Category__c where PSQ_Header__c=:stdCtrl.getId()];
        
       
        
    }
    
    
    public string addRec {get; set;}
    public PageReference addRecord()
    {
        //PSQ_Record_Category__c toAdd = [select Id from PSQ_Record_Category__c where Id = :addRec];
        PSQ_Record_Category__c toAdd = new PSQ_Record_Category__c(Id = addRec);
        //PSQ_Record_Category__c toAdd = ApexPages.currentPage().getParameters().get('addRec');
        toAdd.Need__c = True;
        update toAdd;
        setCatsList();
        return null;
        
        
    }
    public string removeRec {get; set;}
    public PageReference removeRecord()
    {
        
        PSQ_Record_Category__c toRemove = new PSQ_Record_Category__c(Id = removeRec);
        toRemove.Need__c = False;
        update toRemove;
        setCatsList();
        return null;
    }

So is this a bug?
Alain CabonAlain Cabon
I have myself problems with the command button and the parameters. I thought there was a bug but the articles are sometimes very old about this issue (2009) and there are work-arounds.

This only difference for your code is the rerender target.

<apex:pageBlock title="Line Items" id="AppLineItems">  ( KO )

<apex:pageBlockTable id="list" value="{!catsList}" var="rc">  ( OK )

The solution is perhaps around that. People have raised suspicions around the target ids of the rerender (the only difference in their code too).

I am interested in the solution of your problem because many people have this issue (including me).



 
BeautifulDrifterBeautifulDrifter
OK...I figured it out.  I had a required field in a different portion of the page which was throwning an error when I was pressing the button.  Basically, I have an SOQL statement within my getter and the error was throwing on the required field before the controller was executing and populating it.  At least that's what I believe was happening.  The rerender was actually rerendering the page to I wasn't seeing it.  There are a bunch of questions out there about rerendering and using buttons in the visualforce page.  

For simplicities sake, I removed the required field and now it is working.  
This was selected as the best answer
Alain CabonAlain Cabon
Interesting and good news for you because of the following detail from Visualforce Developer Guide (v39): 

apex:param  A parameter for the parent component. The <apex:param> component can only be a child of the following components:
<apex:actionFunction>
<apex:actionSupport>
<apex:commandLink>
<apex:outputLink>
<apex:outputText>
<flow:interview>
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_param.htm

There is not <apex:commandButton> which seems an exception for Salesforce or just a typo here since a recent fix (v39)?
Many people have tried apex:commandButton searching a hack or a work-around and that seems to work with your exact code which can interest other people who want a button on every row of a list (instead of the classic checkboxes with a global action buttton)

Your problem is solved so mark your own answer as the best one.
BeautifulDrifterBeautifulDrifter
Well it definitely works for commandButton.  Thanks for your help.