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
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);
            //accounts.Marketing_Customer_Only__c=true;
           }
           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,

Greetings!

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.

https://salesforce.stackexchange.com/questions/21912/trying-to-create-a-trigger-to-prevent-creation-of-duplicate-accounts

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) 
Hi, 

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

trigger CasesTrigger on Case (before insert, before update) {
    
    if(Trigger.isBefore){
    if(Trigger.isInsert){
      CaseDupeChecker.caseDupeCheck(Trigger.new);
    }
    else if(Trigger.isUpdate){
      CaseDupeChecker.caseDupeCheck(Trigger.new);
    }
  }
}

Handler: 

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) {
               existingCases.add(existingCase);
          }
    }
          
          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