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
AnsliesdadAnsliesdad 

Need Help Adding Button to VF Page that Merges Cases

I've created a custom detail page button on the Case object that calls a VF page.  The VF page renders a page that displays two sections:  Master Case and Merge Cases.  Under Master Case, it displays the case from which the button was pushed.  Under Merge Cases, it displays all cases of a certain type (i.e. Type = X) and that are associated with the same Account and the same Contact (i.e. All cases that have John Smith as the Contact and Salesforce as the Company and X as the Type).  Also, under Merge Cases, each case is displayed with a checkbox in the left column like in a list view so the user can select them.  It also displays a button called "Merge Cases".  This is where I'm stuck.

What I want to happen is when a user checks a box for one or more cases under Merge Cases and then clicks the Merge Cases button, I want to update the Master Case with the data from the Merge Cases.  Most of the fields I'd like to update are text fields (so concatenate the text values) but there are a few picklists.  Also, I don't want to delete the Merge Cases but I do want to set their status field to a different value, say "Duplicate".

I'm pretty new to coding so I would appreciate any help or if anyone can point me in the right direction from here.  I've included my VF page and Controller code below.  Thanks for any help.

Here is my VF Page:
<apex:page standardController="Case" extensions="SCM_Controller">

    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockButtons >
                <apex:commandButton value="Merge Cases" action="{!processSelected}" rerender="table"/>
            </apex:pageBlockButtons>
            <apex:pageBlockSection title="Master Case" columns="1">
                <!-- In our table we are displaying the cCase records -->
                <apex:pageBlockTable value="{!Case}" var="c" id="table">
                    <!-- This is how we access the case values within our cCase container/wrapper -->
                    <apex:column value="{!c.CaseNumber}" />
                    <apex:column value="{!c.AccountId}" />
                    <apex:column value="{!c.ContactId}" />
                    <apex:column value="{!c.type}" />
                    <apex:column value="{!c.reason}" />
                    <apex:column value="{!c.origin}" />
                    <apex:column value="{!c.description}" />
                    <apex:column value="{!c.Care_Management_Required__c}" />
                </apex:pageBlockTable>
            </apex:pageBlockSection>
            <apex:pageblockSection title="Merge Cases" columns="1">
                <!-- In our table we are displaying the cCase records -->
                <apex:pageBlockTable value="{!cases}" var="c" id="table">
                    <apex:column >
                        <!-- This is our selected Boolean property in our wrapper class -->
                        <apex:inputCheckbox value="{!c.selected}"/>
                    </apex:column>
                    <!-- This is how we access the case values within our cCase container/wrapper -->
                    <apex:column value="{!c.cas.CaseNumber}" />
                    <apex:column value="{!c.cas.AccountId}" />
                    <apex:column value="{!c.cas.ContactId}" />
                    <apex:column value="{!c.cas.type}" />
                    <apex:column value="{!c.cas.reason}" />
                    <apex:column value="{!c.cas.origin}" />
                    <apex:column value="{!c.cas.description}" />
                    <apex:column value="{!c.cas.Care_Management_Required__c}" />
                </apex:pageBlockTable>
            </apex:pageblockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>
And here is my controller:
public with sharing class SCM_Controller
{
    
    private ApexPages.StandardController controller;
   
    public Case tempCase {get; set;}

    public SCM_Controller(ApexPages.StandardController controller)
    {
    
        this.controller = controller;
        this.tempCase = (Case)controller.getRecord();
        
    }

    //add wrapper with checkbox so user can select cases to be merged
    //collection of the class/wrapper objects cCase
    public List<cCase> caseList {get; set;}
 
    //This method uses a simple SOQL query to return a List of Cases
    public List<cCase> getCases()
    {
        if(caseList == null)
        {
            caseList = new List<cCase>();
            System.debug(this.tempCase);
            for(Case c: [select Id, CaseNumber, AccountId, ContactId, type, reason, origin, description, care_management_required__c from Case where ContactId =:this.tempCase.ContactId and CaseNumber!=:this.tempCase.CaseNumber and type='Medical Incident'])
            {
                // As each case is processed we create a new cCase object and add it to the caseList
                caseList.add(new cCase(c));
            }
        }
        return caseList;
    }
 
    public PageReference processSelected()
    {
 
        //create a new list of Cases to be populated only with Cases if they are selected
        List<Case> selectedCases = new List<Case>();
 
        //cycle through list of cCases and check to see if the selected property is set to true, if it is, add the Case to the selectedCases list
        for(cCase cCas: getCases())
        {
            if(cCas.selected == true)
            {
                selectedCases.add(cCas.cas);
            }
        }
 
        //Now we have our list of selected cases and can perform any type of logic we want
        System.debug('These are the selected Cases...');
        for(Case cas: selectedCases)
        {
            system.debug(cas);
        }
        caseList=null; //we need this line if we performed a write operation because getCases gets a fresh list now
        return null;
    }


    //This is our wrapper/container class
    public class cCase
    {
        public Case cas {get; set;}
        public Boolean selected {get; set;}
 
        //This is the contructor method
        public cCase(Case c)
        {
            cas = c;
            selected = false;
        }
    }
}
Thanks again for any help!
AnsliesdadAnsliesdad
I forgot to mention that I'd like to bring over related lists records from the Merge Cases to the Master Case.