I have taken three fields from case object called AccountName
, IntegrationTo__c
and SfType__c
(checkbox).
I have created one case record with a specific combination of three fields.
For the second time I create a record with that combination, then display error message only if SfType__c is true in first record otherwise allow for record creation.
trigger AvoidDuplicate on Case(before insert)
{
set<string> newNameSet = new set<string>();
newNameSet = new set<string>();
set<string> newEmailSet = new set<string>();
set<string> dbNameSet = new set<string>();
set<string> dbEmailSet = new set<string>();
for(Case newLead : trigger.new){
newNameSet.add(newLead.Account.Name);
newEmailSet.add(newLead.IntegrationTo__c);
}
for(Case dbLead : [select id, Case.Account.Name, IntegrationTo__c from Case where IntegrationTo__c IN: newEmailSet OR Case.Account.Name IN: newNameSet]){
dbNameSet.add(dbLead.Account.Name);
dbEmailSet.add(dbLead.IntegrationTo__c);
}
for(Case newLead : trigger.new){
if(dbNameSet.contains(newLead.Account.Name) && dbEmailSet.Contains(newLead.IntegrationTo__c))
newLead.addError('You are inserting Duplicate Case');
}
}
i wrote this this not checking the account field name and integration__c values only with combination of account and integration__c shows error