Account field and integration__c fields combination of record is have already in case object don't allows duplication if sftype__C checkbox is true otherwise allows if sftype__c is false allows for duplication.

This is my trigger but this is also allows duplicates please check it once

trigger AvoidDuplicateInsertions on Case(before insert,before update){

         Set<string> accountname= new Set<string>();
         Set<string> integrationto= new Set<string>();

         for(Case cas: Trigger.new){



       List<Case> duplicatecaseList = [Select id,Case.Account.Name,IntegrationTo__c,SFType__c From Case where Case.Account.Name= :accountname AND IntegrationTo__c =:integrationto];

       Set<string > duplicatecaseIds = new Set<string >();

       for(Case dup: duplicatecaseList ){

          duplicatecaseIds .add(dup.Account.Name);
          duplicatecaseIds .add(dup.IntegrationTo__c);


       for(Case s : Trigger.new){

            if(s.Account.Name!=null && s.IntegrationTo__c!=null){

               if(duplicatecaseIds.contains(s.Account.Name) && duplicatecaseIds.contains(s.IntegrationTo__c)){

                  if(s.SFType__c = true){

                      s.addError('Record already exist with same Service');



but this allows duplication please anyone check it once

Thanks In advance


Instead of case.Account.Name, do it using case.AccountId, because :

  1. Account Name could be duplicate
  2. You can not access Account Name using reference in trigger without using a Query. so case.Account.Name is null every time in trigger.

Not the answer you're looking for? Browse other questions tagged or ask your own question.