+ Start a Discussion
Rafael MottaRafael Motta 

Cases Mtching Rule by apex class

Good morning Trailblazers.
I would appreciate your help to clear my mind. I would like to create a duplication rule for cases, so the only option I think i need to create a class, the problem is that I am not a developer and I want to help my team because I no longer have pending.
he criteria are as follows.
For the same Customer (Person Account)
Case Status does not equal to Resolved or Closed
Order Number = Exact match
Case Reason = Exact Match
Error Message: Duplicate Case [Case Number (link possible?)].
A case is already opened against this exact Customer, Order Number, and Case Reason.

I could crate  the select of fields but when i try to ser If sentence, i can save the field 

public class LP_CheckMarketingCustomerOnly{
    Set<Id> CasesIds = new Set<Id>();
       public static void GetAllCases() {
      // Esta Consulta funciona,  la tomare como base
         Account[] AllAccounts = [SELECT Id,(SELECT Id,Order_Number__c,Reason,Status FROM Cases) FROM Account LIMIT 3]; 
        for (Account accounts : AllAccounts) {

            //for(Case cases:accounts.Cases)
             //        System.debug('Account Id:'+accounts.Id+'   Case Id:'+cases.Id);
           System.debug('This is a update'+ AllAccounts);
           //update AllAccounts;
             //System.debug('This is a testing debug cases'+ AllAccounts);
if you can guide me i apriciate 
ShirishaShirisha (Salesforce Developers) 
Hi Rafael,


Are you getting any errors with the above code.I would suggest you to refer the sample code in the below thread for trigger to prevent the duplicate case record creation.


Kindly mark it as best answer if it helps so that it can help others in the future.

Warm Regards,
Shirisha Pathuri
AnudeepAnudeep (Salesforce Developers) 

As far as I know, you need a trigger and a handler for this

trigger CasesTrigger on Case (before insert, before update) {
    else if(Trigger.isUpdate){


public class CaseDupeChecker {

  public static void caseDupeCheck(List<Case> cases){

  List<Account> AllAccounts = new List<Account>([SELECT Id,(SELECT Id,Order_Number__c,Reason,Status FROM Cases) FROM Account LIMIT 3]); 

  List<Case> existingCases = new List<Case>();

    for(Account acc: AllAccounts) {

          for(Case cs: acc.cases) {
          for (Case existingCase : existingCases) {
    for (Case newCase : cases) {

        if (newCase.status!= Resolved || newCase.status!=Closed) {
              if(newCase.Order_Number__c==existingCase.Order_Number__c && newCase.Reason==existingCase.Reason) {
                   newCase.addError('Duplicate Case' + newCase.Case Number + 'A case is already opened against this exact Customer, Order Number, and Case Reason.');




NOTE: The code provided is an example. You'll need to review and make modifications for your organization.

Let me know if this helps, if it does, please mark this answer as best so that others facing the same issue will find this information useful. Thank you