I'm playing with a trigger exercise, that consists of creating a trigger to create 10 Opportunities whenever a account with more than 100 employees are created.

I made the following:

trigger AccountTrigger on Account (after insert) {
    List<Contact> Contacts = new List<Contact>();
    for (Account acct : Trigger.new){       
        for(integer i = 0; i < 2; i++){
            Contact cont    = new Contact();
            cont.LastName   = acct.Name+ i;
            cont.AccountId  = acct.Id;
        insert Contacts;

    if (acct.NumberOfEmployees > 99){
        List<Opportunity> Opportunities = new List<Opportunity>();
        for(integer i = 0; i < 10; i++){
            Opportunity opp = new Opportunity();
            opp.Name        = acct.Name + i;
            opp.CloseDate   = system.today()+ 30;
            opp.StageName   = 'Open';
            opp.AccountId   = acct.Id;
        insert Opportunities;


And I'm getting the following error:

execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATES_DETECTED, Use one of these records?:

I didn't created any, or changed the dupe rule of account neither Opportuinities.

Can you help me understand what kind of error is this? And how can I change the trigger to workaround it?

Thanks in advance!!


As per the error message, this is coming from an active duplicate rule, which may have been created by another admin user.

The part "Use one of these records?" in the error message suggests that "Alert" option is selected on the duplicate rule.

Try to look for the duplicate rule under Setup, Duplicate Rules or Matching Rules and review the criteria causing the error message.

Further reference

  • Thanks for the quick answer!! How can I change the trigger to avoid hit this error? I don't want to deactivate the dupe rule. – RedMan Sep 27 '20 at 13:59
  • Normally you have to adapt the trigger logic to match the duplicate rule criteria but just realised that duplicate rule is not supported yet on opportunity but the error is coming a duplicate rule somewhere. Were you able to find the duplicate rule, is it on account ? could you also post the complete trigger code ? – Shamina Sep 27 '20 at 14:17
  • Apparently, is the account one. Edited the op with it! – RedMan Sep 27 '20 at 14:31
  • Based on the full code in your question, there is a DML insert on contact so if there is an active duplicate rule on contact, the error may be coming from here, check debug log to confirm. Also, be careful, the DML insert statements are currently inside the for loop of Trigger.new, this is prone to governor limit errors. Cross check best practice on triggers and review your code – Shamina Sep 27 '20 at 14:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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