+ Start a Discussion
mamatha devarajmamatha devaraj 

"1. The system should identify the duplicate tickets for the same policy and for the same QRC. 2. The system should merge the duplicate tickets and make a Parent - child ticket relation". here ticket is a case object.

I wrote this code but it is not working, help me for this

Thanks in advance

trigger FindDuplicatecases on Case (after insert) {
    system.debug('tset');
    
    String policyVariable;
    String typevalue;
    Datetime createdDateValue;
    List<Case> caseList = New List<Case>();
    List<Case> cadup = New List<Case>();
    Set<Case> caseSet = New Set<Case>();
    
    for(Case c:trigger.new){
        if(c.policy__c != null && c.Type == 'QRC'){
            policyVariable = c.policy__c;
            typevalue = c.Type;
            createdDateValue = c.CreatedDate;
        }
    }
    system.debug('policyVariable>>> '+policyVariable);
    system.debug('typevalue>>> '+typevalue);
    
    if(policyVariable != null && typevalue == 'QRC'){
        system.debug('in>>>> ');
        cadup =[select id, policy__c, Type, ParentId from case where policy__c =: policyVariable  AND Type = 'QRC' ];
    }
    system.debug('cadup>>>> '+cadup);
    
    if(cadup.size() > 0){
        for(case cs : trigger.new){
            for(case cse : cadup){
     system.debug('oldcaseid'+cse);
                Case ce = New Case();
                ce.Id = cs.Id;
                ce.ParentId = cse.Id;
                caseSet.add(ce);
            }
        }
    }
    system.debug('caseSet>>>> '+caseSet);
    
    for(Case csSet : caseSet){
        caseList.add(csSet);
    }
    
  
        Update caseList;
    
}
Best Answer chosen by mamatha devaraj
Suraj Tripathi 47Suraj Tripathi 47
// trigger file

trigger FindDuplicatecases on Case (after insert) {
    Switch on Trigger.operationType{
        when AFTER_INSERT{
            caseTriggerHandler.findDuplicateCase(trigger.new);
        }
    }
}


// case trigger handler file 

public class caseTriggerHandler {
    public static void findDuplicateCase(List<Case> newReacords){
        String policyVariable;
        string typevalue;
        Datetime createdDateValue;
        
        List<Case> caseList = new List<Case>();
        List<Case> cadup = new List<Case>();
        set<Case> caseSet = new set<Case>();
        set<Id> caseId = new set<Id>();
        for( Case each : newReacords){
            if(each.policy__c != null && each.Type == 'QRC'){
                policyVariable = each.Policy__c;
                typevalue = each.Type;
                createdDateValue = each.CreatedDate;
                caseId.add(each.Id);
            }
        }
        
        system.debug('policyVariable>>> '+policyVariable);
        system.debug('typevalue>>> '+typevalue);
        
        if(policyVariable != null && typevalue == 'QRC'){
            system.debug('in>>>> ');
            cadup =[select id, policy__c, Type, ParentId from case 
                    where policy__c =: policyVariable  AND Type = 'QRC' AND ID != :caseId ];
        }
        system.debug('cadup>>>> '+cadup);
        if(cadup.size() > 0){
            for(Id csId : caseId){
                for(case cse : cadup){
                    system.debug('oldcaseid'+cse);
                    Case ce = New Case();
                    ce.Id = csId;
                    ce.ParentId = cse.Id;
                    caseSet.add(ce);
                }
            }
        }
        system.debug('caseSet>>>> '+caseSet);
        
        for(Case csSet : caseSet){
            caseList.add(csSet);
        }
        system.debug('caseList : '+ caseList);
        Update caseList;
    }
}

I hope you find the above solution helpful.
 If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Suraj

All Answers

Suraj Tripathi 47Suraj Tripathi 47
// trigger file

trigger FindDuplicatecases on Case (after insert) {
    Switch on Trigger.operationType{
        when AFTER_INSERT{
            caseTriggerHandler.findDuplicateCase(trigger.new);
        }
    }
}


// case trigger handler file 

public class caseTriggerHandler {
    public static void findDuplicateCase(List<Case> newReacords){
        String policyVariable;
        string typevalue;
        Datetime createdDateValue;
        
        List<Case> caseList = new List<Case>();
        List<Case> cadup = new List<Case>();
        set<Case> caseSet = new set<Case>();
        set<Id> caseId = new set<Id>();
        for( Case each : newReacords){
            if(each.policy__c != null && each.Type == 'QRC'){
                policyVariable = each.Policy__c;
                typevalue = each.Type;
                createdDateValue = each.CreatedDate;
                caseId.add(each.Id);
            }
        }
        
        system.debug('policyVariable>>> '+policyVariable);
        system.debug('typevalue>>> '+typevalue);
        
        if(policyVariable != null && typevalue == 'QRC'){
            system.debug('in>>>> ');
            cadup =[select id, policy__c, Type, ParentId from case 
                    where policy__c =: policyVariable  AND Type = 'QRC' AND ID != :caseId ];
        }
        system.debug('cadup>>>> '+cadup);
        if(cadup.size() > 0){
            for(Id csId : caseId){
                for(case cse : cadup){
                    system.debug('oldcaseid'+cse);
                    Case ce = New Case();
                    ce.Id = csId;
                    ce.ParentId = cse.Id;
                    caseSet.add(ce);
                }
            }
        }
        system.debug('caseSet>>>> '+caseSet);
        
        for(Case csSet : caseSet){
            caseList.add(csSet);
        }
        system.debug('caseList : '+ caseList);
        Update caseList;
    }
}

I hope you find the above solution helpful.
 If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Suraj
This was selected as the best answer
mamatha devarajmamatha devaraj
HI  Suraj Tripathi 47
Thank you so much for your respons.

It is working fine for only one duplicate , If it is more Than one duplicate it's showning error Massage 

''FindDuplicatecases: execution of AfterInsert caused by: System.ListException: Duplicate id in list: 5007F00001GMjY6QAL Class.caseTriggerHandler234.findDuplicateCase: line 46, column 1 Trigger.FindDuplicatecases: line 4, column 1''.