1

I have a queue setup that users can add cases to before submitting them. I currently have it so that they can type the row number of the case they want to delete; however, I need to make it so that they can press a button beside the row and have it delete that specific row.

I already set up the button to generate on each row. I just cannot figure out how to correlate that button with that row. This is the code I have so far:

CLASS-

public class MassAddCaseNotes {


    public Case_Note__c Case_Notes                    {get; set;}
    public List<Case> cases                           {get; private set;}
    public Map<Id, Boolean> selectedCases             {get; set;}
    public Case filterCase                            {get; set;}    
    public String sortField                           {get; set;}
    public boolean direction                          {get; set;}
    public List<Case_Note__c> queue                   {get; set;}
    private ApexPages.StandardSetController allCases  {get; set;}
    public Integer rowToRemove                        {get; set;}




    private final String selectCasesQuery = 'SELECT Id, CaseNumber, RecordType.Name, Contact.Name, Account.Name, Opened_Date__c, ClosedDate, Program_Location_Assignment__c, Referring_Agency__r.Name ' +
                                            'FROM Case ' +
                                            'WHERE Status=\'Open\' AND RecordTypeId IN (SELECT Id FROM RecordType WHERE SObjectType = \'Case\' AND IsActive = true)';

    public MassAddCaseNotes (ApexPages.StandardSetController controller) 
   {
           queue = new List<case_Note__c>();
           Case_Notes = new Case_Note__c(); 
           filterCase = new Case();
           direction = true;
         //selectCases();
           direction = false;

   }

    public void selectCases() 
    {
        if (sortField == null) sortField = 'CaseNumber';
        String filter = '';
        if (filterCase.RecordTypeId != null) filter += ' AND RecordTypeId = \'' + filterCase.RecordTypeId + '\'';
        if (filterCase.ContactId != null) filter += ' AND ContactId = \'' + filterCase.ContactId + '\'';
        if (filterCase.AccountId != null) filter += ' AND AccountId = \'' + filterCase.AccountId + '\'';

        allCases = new ApexPages.StandardSetController(Database.getQueryLocator(selectCasesQuery + filter +  ' ORDER BY ' + sortField + (direction ? ' ASC' : ' DESC')));
        allCases.setPageSize(1);
        updateCases();

    }


    public void sortCases() 
    {
        direction = !direction;
        selectCases();

    }

    public Boolean hasPrevious {get {return allCases.getHasPrevious();}}
    public Boolean hasNext {get {return allCases.getHasNext();}}

    public void previous() 
    {
        allCases.previous();
        updateCases();
    }

    public void next() 
    {
        allCases.next();
        updateCases();
    }

    private void updateCases() 
    {
        cases = allCases.getRecords();
        selectedCases = new Map<Id, Boolean>();
        for (Case c : cases) 
        {
            selectedCases.put(c.Id, false);
        }
    }


public void addtoqueue()
{
integer counter = 0;
        for (Case c : cases) 
        {
            if (selectedCases.get(c.Id)) 
            {

                Case_Note__c items = new Case_Note__c(
                    Case__c = c.Id,
                    Date__c = Case_Notes.Date__c,
                    Contact_Name__c = filtercase.contactid,
                    Note__c = Case_Notes.Note__c 
                );

                 for(integer i = 0; i<= queue.size()-1; ++i)
                {
                    if(items == queue[i])
                    {
                         counter+=1;
                    }

                }                                
                    if(counter <= 0)
                    {
                         queue.add(items); 
                                            }
                    if(counter > 0 )
                    {                           
                    Apexpages.addMessage(new ApexPages.Message(ApexPages.Severity.Warning,''+' You are attempting to add a duplicate case to the queue.'));

                         }
                    }

            }
}




public void removelast()
{
queue.remove(queue.size()-1);
}

public void removeall()
{
queue.clear();
}



public void removeDesiredRow() 
{
    if(queue.size()>=rowToRemove&& rowToRemove> 0) 
    {
        queue.remove(rowToRemove-1);
        rowToRemove= null;
    }
  /*  if(rowToRemove == 999 || rowToRemove == 9999 || rowToRemove == 99999 || rowToRemove == 999999 || rowToRemove == 9999999)
    {
        queue.clear();
        rowToRemove= null;
    }
    if(queue.size()< rowToRemove && rowToRemove != 999 && rowToRemove != 9999 && rowToRemove != 99999 && rowToRemove != 999999 && rowToRemove != 9999999 || rowToRemove < 1)
    {  
        Apexpages.addMessage(new ApexPages.Message(ApexPages.Severity.error,''+'Please Enter A Valid Row To Delete'));
        rowToRemove= null;
    }*/
}




public PageReference save() 
{       
     addtoqueue();
     return null;
}


public PageReference submitCases()
{
if(queue.size()==0){
Apexpages.addMessage(new ApexPages.Message(ApexPages.Severity.info,''+' Your queue is currently empty.'));

}
if(queue.size()>0){
  integer i = 0;
  while(i<queue.size())
  {
     insert queue[i];
     i++;
  }

  queue.clear();


  }
    PageReference pg = new PageReference('/apex/MassAddCaseNotes');
  pg.setRedirect(true);
  return pg;
}

/*
public PageReference saveandclose() 
{
        List<Case_Note__c> note = new List<Case_Note__c>();
        for (Case c : cases) 
        {
            if (selectedCases.get(c.Id)) 
            {
                Case_Note__c s = new Case_Note__c(
                    Case__c = c.Id,
                    Date__c = Case_Notes.Date__c,
                    Note__c = Case_Notes.Note__c
                );               

                note.add(s);
            }
        }
        insert note;  

     PageReference pg = new PageReference('/500/o');
        pg.setRedirect(true);
        return pg;
}
*/
}

PAGE-

<apex:page standardController="Case_Note__c" recordSetVar="MassAddCaseNotes" tabStyle="Case_Note__c" extensions="MassAddCaseNotes">







<apex:sectionHeader title="Mass Add Case Notes" />  
<apex:panelGrid columns="3" cellpadding="5" style="margin-bottom: 15px;">
<apex:form style="width:450px" > 

    <apex:pageBlock title="Case Notes" id="block1">

        <apex:pageBlockSection title="Enter Your Notes " columns="2" id="section1">
            <script> twistSection(document.getElementById('{!$Component.block1.section1}').getElementsByTagName('img')[0]) </script>
            <apex:panelGrid columns="1" cellpadding="5" style="margin-bottom: 15px;">
                <apex:outputLabel for="CaseNote">Notes:  </apex:outputLabel>
                <apex:inputField style="width:300px;height:100px;" value="{!Case_Notes.Note__c}" id="CaseNote"  />
                <apex:outputLabel for="CaseNote">Date:  </apex:outputLabel>
                <apex:inputField value="{!Case_Notes.Date__c}" id="Date" />
            </apex:panelGrid>
        </apex:pageBlockSection>

        <apex:pageBlockSection title="Find Cases" columns="1">
            <apex:panelGrid columns="1" cellpadding="5" style="margin-bottom: 5px;"> 
                <apex:outputLabel for="contactFilter">Contact Search: </apex:outputLabel>
                <apex:inputField value="{!filtercase.contactid}" required="true" id="contactFilter" />
                <apex:commandButton action="{!selectCases}"   value="Filter" />                  
            </apex:panelGrid>

            <apex:panelGrid columns="1" cellpadding="5" style="margin-bottom: 5px;">          
               <apex:PageBlockTable value="{!cases}"  var="case"  id="case">
                <apex:column >
                    <apex:inputCheckbox value="{!selectedCases[case.Id]}" />
                </apex:column>
                <apex:column >
                    <apex:facet name="header">
                        <apex:commandLink value="Case Number" action="{!sortCases}">
                            <apex:param name="productSortField" value="CaseNumber" assignTo="{!sortField}"/>
                        </apex:commandLink>
                    </apex:facet>
                    <apex:outputLabel value="{!case.CaseNumber}" />
                </apex:column>
                <apex:column >
                    <apex:facet name="header">
                        <apex:commandLink value="Record Type" action="{!sortCases}">
                            <apex:param name="productSortField" value="RecordType.Name" assignTo="{!sortField}"/>
                        </apex:commandLink>
                    </apex:facet>
                    <apex:outputLabel value="{!case.RecordType.Name}" />
                </apex:column>
                <apex:column >
                    <apex:facet name="header">
                        <apex:commandLink value="Contact" action="{!sortCases}">
                            <apex:param name="productSortField" value="Contact.Name" assignTo="{!sortField}"/>
                        </apex:commandLink>
                    </apex:facet>
                    <apex:outputLabel value="{!case.Contact.Name}" />
                </apex:column>
                <apex:column >
                    <apex:facet name="header">
                        <apex:commandLink value="Account" action="{!sortCases}">
                            <apex:param name="productSortField" value="Account.Name" assignTo="{!sortField}"/>
                        </apex:commandLink>
                    </apex:facet>
                    <apex:outputLabel value="{!case.Account.Name}" />
                </apex:column>
                <apex:column >
                    <apex:facet name="header">
                        <apex:commandLink value="Date Opened" action="{!sortCases}">
                            <apex:param name="productSortField" value="Opened_Date__c" assignTo="{!sortField}"/>
                        </apex:commandLink>
                    </apex:facet>
                    <apex:outputLabel value="{!case.Opened_Date__c}" />
                </apex:column>
                           </apex:PageBlockTable> 
                           <apex:commandButton action="{!save}" value="Save To Case Queue" status="retrieveSaveStatus" id="btnSave" immediate="false" />
            </apex:panelGrid>
         </apex:pageBlockSection>
     </apex:pageBlock>
</apex:form> 


<apex:variable var="rowcount" value="{!1}" />

<apex:form style="width:400px" > 
     <apex:pageBlock title="Case Queue" >
        <apex:pageBlockButtons location="top" >





                      <apex:panelGrid columns="4" cellpadding="1" style="margin-top: 10px;"> 
                      <apex:commandButton action="{!submitCases}" value="Submit Notes"  status="retrieveSaveStatus" id="btnSubmitCases" immediate="false" />
                     </apex:panelGrid>
        </apex:pageBlockButtons> 

        <apex:actionStatus layout="block" startText="Saving notes. . . " stopText="" id="retrieveSaveStatus" startStyle="color:green; font-style:italic" stopStyle="color:black;"/>
        <apex:pageMessages showDetail="false" />
                <br/>      

                <apex:pageBlockSection title="Case Queue" columns="1">
                    <apex:panelGrid columns="3" cellpadding="1" style="margin-top: 10px;">
                     <apex:commandButton value="Delete Row #: " action="{!removeDesiredRow}"/>
                     <apex:inputText value="{!rowToRemove}" style="width:30px" /> 
                     <apex:commandButton value="Delete all " action="{!removeall}"/>
                    </apex:panelGrid>

                        <apex:pageblocktable value="{!queue}" var="que">
                         <apex:column >
                          {!rowcount} <apex:variable var="rowcount" value="{!rowcount+1}" />
                          <apex:commandButton value="Delete: "   action="{!removeDesiredRow}"  />
                         </apex:column>
                         <apex:column headervalue="Contact Name" value="{!que.Contact_Name__c}"/>
                         <apex:column headervalue="Case Number" value="{!que.Case__c}"/>
                         <apex:column headervalue="Date" value="{!que.Date__c}"/>
                     </apex:pageblocktable>  



                </apex:pageBlockSection>

    </apex:pageBlock>
</apex:form>  


</apex:panelGrid>
</apex:page>

This is a screen shot of the queue Case Queue

This is the block of code that specifically deals with the queue

<apex:variable var="rowcount" value="{!1}" />

<apex:form style="width:400px" > 
     <apex:pageBlock title="Case Queue" >
        <apex:pageBlockButtons location="top" >





                      <apex:panelGrid columns="4" cellpadding="1" style="margin-top: 10px;"> 
                      <apex:commandButton action="{!submitCases}" value="Submit Notes"  status="retrieveSaveStatus" id="btnSubmitCases" immediate="false" />
                     </apex:panelGrid>
        </apex:pageBlockButtons> 

        <apex:actionStatus layout="block" startText="Saving notes. . . " stopText="" id="retrieveSaveStatus" startStyle="color:green; font-style:italic" stopStyle="color:black;"/>
        <apex:pageMessages showDetail="false" />
                <br/>      

                <apex:pageBlockSection title="Case Queue" columns="1">
                    <apex:panelGrid columns="3" cellpadding="1" style="margin-top: 10px;">
                     <apex:commandButton value="Delete Row #: " action="{!removeDesiredRow}"/>
                     <apex:inputText value="{!rowToRemove}" style="width:30px" /> 
                     <apex:commandButton value="Delete all " action="{!removeall}"/>
                    </apex:panelGrid>

                        <apex:pageblocktable value="{!queue}" var="que">
                         <apex:column >
                          {!rowcount} <apex:variable var="rowcount" value="{!rowcount+1}" />
                          <apex:commandButton value="Delete: "   action="{!removeDesiredRow}"  />
                         </apex:column>
                         <apex:column headervalue="Contact Name" value="{!que.Contact_Name__c}"/>
                         <apex:column headervalue="Case Number" value="{!que.Case__c}"/>
                         <apex:column headervalue="Date" value="{!que.Date__c}"/>
                     </apex:pageblocktable>  



                </apex:pageBlockSection>

    </apex:pageBlock>
</apex:form>  

And this specifically deals with the removal of individual rows.

public void removeDesiredRow() 
{
    if(queue.size()>=rowToRemove&& rowToRemove> 0) 
    {
        queue.remove(rowToRemove-1);
        rowToRemove= null;
    }
}

2 Answers 2

2

I would suggest using Javascript Remoting for this which gives you the benefit of a more light-weight solution, and conforming your <apex:pageBlockTable> to Salesforce's list views. For example:

enter image description here

Here's a sample code:

Controller

public class MassAddCaseNotes {

    @RemoteAction
    public static void deleteCase(Id caseNoteId) {
        delete new Case_Note__c(Id = caseNoteId);
    }

}

Visualforce

<script type="text/javascript">
    function deleteCaseNote(caseNoteId) {
        Visualforce.remoting.Manager.invokeAction(
            '{!$RemoteAction.MassAddCaseNotes.deleteCase}',
            caseNoteId, 
            function (result, event) {
                if (event.status) {
                    // Refresh the page after deleting the record.
                    location.reload();
                }
            }
        );
    }
</script>

...

<apex:pageblocktable value="{!queue}" var="que">
    <apex:column>
        <a onclick="deleteCaseNote('{!que.id}')">Delete</a>
    </apex:column>
</apex:pageBlockTable>
5
  • I have put this into my code and it looks great, the "delete button" shows up and when I mouse over it, it turns blue with an underline but I cannot click on it. My cursor is just a text cursor when I scroll over it. Commented Oct 8, 2014 at 23:19
  • Write this instead: <a href="javascript:void(0);" onclick="deleteCaseNote('{!que.id}')">Delete</a>
    – Jan Julian
    Commented Oct 8, 2014 at 23:25
  • It certainly is close. It has the hand cursor and lets me click it. But nothing happens. Commented Oct 8, 2014 at 23:33
  • Strange. First, open your dev console to see if you're getting any javascript errors. Then check if the javascript function deleteCaseNote(caseNoteId) gets invoked when you click the "Delete" link. If it does, check if you get a debug log from SFDC (this will be due to the @RemoteAction being invoked).
    – Jan Julian
    Commented Oct 8, 2014 at 23:42
  • I don't see any javascript errors and I am unsure how to go about checking to see if the function deleteCaseNote(caseNoteId) gets invoked when 'Delete' is clicked. I have not used the dev console very much. Commented Oct 9, 2014 at 20:18
2

You can pass parameters inside apex:CommandButton. So, as you emit the rows in your table, you can have a child element for your apex:commandButton, which is an apex:param, which will call a setter on your controller prior to processing the action of your apex:commandButton.

For example, you could use the id value of your iterator, que, as the identifier, instead of trying to do the rowCount thing. Modify removeDesiredRow() to expect que.id in a rowToRemove variable, instead of the rowCount.

<apex:commandButton value="Delete: " action="{!removeDesiredRow}">
  <apex:param name="rowToRemove" value="{!que.id}" assignTo={!rowToRemove}"/>
</apex:commandButton>

Simply add some rerender behavior to the apex:commandButton, to keep your data table up to date. You can also get fancy and use <apex:actionSupport> and <apex:actionRegion> if you want an AJAX solution without messing with JavaScript manually.

1
  • if we want confirm dialog box before delete then how you will process. Commented Sep 13, 2017 at 5:22

You must log in to answer this question.

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