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
ccracyccracy 

Duplicate check (via Trigger) when bulk inserting using API

I have writen a 'before insert' trigger to check for duplicates while creating new Accounts. When I do a bulk insert operation for Accounts using API, duplicates within this batch are not caught.

I beleive the reson for this is that the trigger is a bulk trigger. It checks for duplicates for the entire batch against Accounts already in the db. Only after having done the duplicate check (for the entire batch), it inserts (creates) the entire batch. Therefore, it misses any duplicate within this batch of Accounts.

Is there a way, where in I can catch duplicates within the batch also.

Thanks,
Rohit M.

Message Edited by ccracy on 08-27-2007 02:02 AM

mtbclimbermtbclimber
Sure, you just need to add this logic to your trigger.  You can do the same check against the batch in the request as you are doing against the server. For example, if you were de-duping on AccountNumber you could do something like this:

Code:
public class dupeAccount {
 public void checkDupes(List<Account> accountList) { 
  Map<String, Account> numberMap = new Map<String,Account>();
  for(Account a:accountList) {
   if(numberMap.containsKey(a.accountNumber)) a.accountNumber.addError('Duplicate found in batch');
   else numberMap.put(a.accountNumber,a);
  }
 }
}

Of course this logic may not be what you want but you should get the idea.

Regards,