1

I'm trying to run a cleanup job on the master case record after the merge is complete. I want to update and potentially delete duplicate junction records on the master record. What I'm finding is when you merge cases through the Lightning UI that the merges do not happen in bulk (debugging the after update on case). They run once per case being merged. We are not deleting the losing records in our case. If there any way to identify when the final merge takes place? I don't see that we could, but maybe I'm missing something.

The issue I run into is that I will receive an error that the record I'm trying to merge has been deleted. Which makes sense if they are not in bulk.

1 Answer 1

0

We have a similar Scenario where we need to update a deleted tickets numbers on Master Case Record in a Text Field and need to update ParentCaseId__c field from deleted ticket to Master Ticket when user forgot to select the ParentCaseId__c (Only when on Master Record It's empty and on deleted record It's present)

1. Create a Text field with length 255 on Case Object (Merged Ticket).

2. On Case_Trigger call Apex Class.

Trigger Case_Trigger on Case (after Delete) {
    //Calling UpdateCaseFieldsAfterMerge class in after delete by passing  oldmap data
    If(Trigger.isAfter && Trigger.isDelete) {
        UpdateCaseFieldsAfterMerge.AfterChildCaseDelete(Trigger.oldMap);
    }
}

3. UpdateCaseFieldsAfterMerge Class is used to query the deleted Tickets also need to get MasterRecordId from the deleted Ticket. Salesforce will update MasterRecordId when ever we merged the cases, Before deleting the record It will update

/*Class Purpose : To update the Case fields on Master Case after Merging from Merged Case
* NewCaseMap : New Map of deleted Cases after Merge
* OldCaseMap : OldMap of Deeleted Cases Afte Merge
*/
public class UpdateCaseFieldsAfterMerge {
    public static set<Id> setTestMaster;
    public Static List<Case> listTestmergedCases;
    public static void AfterChildCaseDelete( Map<Id,Case>OldCaseMap) {
    // deletedCases is a set of OldCaseMap Ids to extract the deleted Ticket Fields
        set<Id> deletedCases =new set<Id> ();
        deletedCases=OldCaseMap.keyset();
        List<Case> mergedCases = [SELECT CaseNumber, MasterRecordId, 
                                         ParentCaseId__c, Merged_Ticket__c 
                                  FROM Case 
                                  WHERE MasterRecordId != null 
                                        AND Id in : deletedCases ALL ROWS];
        // Map is to store MasterRecordId and Case Numbers from mergedCases
        Map<Id,String>MasterTicket=new Map<Id,String>();
        for (Case CasTicket: mergedCases {
            MasterTicket.put(CasTicket.MasterRecordId,CasTicket.CaseNumber);
        }

        // setMaster is the set of Ids which contains Master Records. We use this set to extract Master Records
        set<Id>setmaster=new set<Id>();
        setmaster=MasterTicket.keyset();
        if(test.isRunningTest()) {
            setmaster=setTestMaster;
            mergedCases=listTestmergedCases;
        }
                    
        //Already we are updating Master Record in same Trigger Instance so we shouldn't update directly here.
        // We use Queable Async to avoid recursion on Case Trigger and to pass list of cases of merged cases and set of Master Record Ids
        if(setmaster.size()>0){
            Id jobId= System.enqueueJob(new MergeCaseQueueable (mergedCases, setmaster));
        }         
    }    
}

4. Queable Class which we use to updated Master Records

public class MergeCaseQueueable implements Queueable {
    List<Case> mergedCases =new List<Case> ();
    set<Id>setmaster=new set<Id>();
         
    public MergeCaseQueueable (List<Case> mergedCases , set<Id>setmaster) {
        //Assign to local variables
        this.mergedCases=mergedCases;
        this.setmaster=setmaster;
    }
            
    public void execute (Queueablecontext Context) {
        List<Case> MasterList=new List<Case>(); 
        List<Case>MasterCase=new List<Case> ();        
                
        if (setmaster.size()>0) {
            MasterCase=[SELECT Id,Merged_Ticket__c,ParentCaseId__c FROM Case WHERE Id in : setmaster];
        }
        if(MasterCase.size()>0){
            for(case cas : MasterCase){
                for (Case cas1 : mergedCases) {
                    // check for MasterRecordId on merge ticket to Id of Master Ticket
                    if(cas1.MasterRecordId==cas.Id || Test.isRunningTest()) {
                        //updating Merged_Ticket__c on diffrent Scenraios so that we won't lose old merged ticket data
                        if (cas.Merged_Ticket__c !=null && cas1.Merged_Ticket__c !=null) {
                            cas.Merged_Ticket__c=cas1.CaseNumber+ ', ' + cas.Merged_Ticket__c+ ', ' +cas1.Merged_Ticket__c;
                        }
                        if (cas.Merged_Ticket__c ==null && cas1.Merged_Ticket__c !=null) {
                            cas.Merged_Ticket__c=cas1.CaseNumber+ ', ' + cas1.Merged_Ticket__c;
                        }
                        if (cas.Merged_Ticket__c !=null && cas1.Merged_Ticket__c ==null) {
                            cas.Merged_Ticket__c=cas1.CaseNumber+ ', ' + cas.Merged_Ticket__c;
                        }
                        if (cas.Merged_Ticket__c ==null && cas1.Merged_Ticket__c ==null) {
                            cas.Merged_Ticket__c=cas1.CaseNumber;
                        }
                        //updating related ticket Id from merged ticket to Master Ticket
                        if(cas.ParentCaseId__c ==null && cas1.ParentCaseId__c !=null) {
                            cas.ParentCaseId__c =cas1.ParentCaseId__c ;
                        }
                        MasterList.add(cas);
                    }
                }
            }
            update MasterList;
        }
    }
}

5. Test Class:

@IsTest
public class TestUpdateCaseFieldsAfterMerge {
    @isTest
    Public static void AfterChildTicketDelete () {
        test.startTest();
                
        List<Case> caseList = new List<Case>();
        Case caseObj1 = new Case( Subject = 'Hello1', SuppliedEmail = '[email protected]', Origin = 'Email');
        insert caseObj1;
        List<Case> MasterList = [select Id,CaseNumber,ParentCaseId__c ,MasterRecordId,Merged_Ticket__c from Case where Id = : caseObj1.Id];
        Case caseObj2 = new Case( Subject = 'Hello2', SuppliedEmail = '[email protected]', Origin = 'Email');
        Case caseObj3 = new Case( Subject = 'Hello3', SuppliedEmail = '[email protected]', Origin = 'Email',ParentCaseId__c = MasterList[0].Id );
        Case caseObj4 = new Case( Subject = 'Hello4', SuppliedEmail = '[email protected]', Origin = 'Email', Merged_Ticket__c=MasterList[0].CaseNumber );
        caseList.add(caseObj2);
        caseList.add(caseObj3);
        caseList.add(caseObj4);
        Insert caseList;
                
        Map<Id,Case> OldCaseMap1=new Map<Id,Case> ();
        oldCaseMap1.put(caseObj1.Id,caseObj1);
        set<Id>MasterIds=new set<Id>();
        For (Case cas : caseList) {
            MasterIds.add (cas.Id);
        }
        UpdateCaseFieldsAfterMerge.setTestMaster= MasterIds; 
        UpdateCaseFieldsAfterMerge.listTestmergedCases= MasterList;
        UpdateCaseFieldsAfterMerge.AfterChildCaseDelete(OldCaseMap1);
        test.stopTest();
    }
}
1
  • 4
    Welcome to Salesforce Stack Exchange (SFSE)! Is this an answer or a question? If this is an answer, the code is appreciated but - to quote @DerekF: "This site thrives on details, and the explanation of how a solution works is as important as the solution itself." So please edit your answer to add details on how this answers the question. If this is a question, rather than asking a question in an answer space, please ask a new question.
    – Moonpie
    Commented Feb 2, 2022 at 15:19

You must log in to answer this question.

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