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
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;
}
}