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
BrandiTBrandiT 

Trigger Error: DUPLICATE_VALUE error message

I have a trigger on contract that is supposed to update values on the related opportunity.  It has worked fine for a while now, but it suddenly starting throwing duplicate value errors for about half the contracts we update.  I haven't made any recent development changes so I'm not sure what's causing it, or even how to research it.  I would appreciate any insight you can give me.

 

Here's the error I'm getting: 

Validation Errors While Saving Record(s)
There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger ContractSumOppTrigger caused an unexpected exception, contact your administrator: ContractSumOppTrigger: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 0067000000OOHZkAAP; first error: DUPLICATE_VALUE, duplicate value found: unknown duplicates value on record with id: 0017000000oL0gj: []: Trigger.ContractSumOppTrigger: line 88, column 1".
 

The interesting thing about the error is that a) the id  0017000000oL0gj is an account record, and I don't do anything with accounts in my trigger, and b) the account is not related to the contract that's being updated in any way.

 

Here's the trigger:

 

trigger ContractSumOppTrigger on Contract
 (after delete, after insert, after undelete, after update) {   
 
 Contract[] cons;
     if (Trigger.isDelete)
          cons = Trigger.old;
              else
                      cons = Trigger.new;   
 // get list of opptys
     Set<ID> oppIds = new Set<ID>();   
     for (Contract con : cons) {
                 oppIds.add(con.Opportunity__c);
                     }
     Map<ID, Contract> contractsForOpps = new Map<ID, Contract>([select Id                                                           
     ,Opportunity__c                                                          
     from Contract                                                           

where Opportunity__c in :oppIds
                       ]);
               Map<ID, Opportunity> oppsToUpdate = new Map<ID, Opportunity>([select Id                                                                
               ,Total_Contracts__c                                                                 
               from Opportunity                                                                 
               where Id in :oppIds]);    

                     for (Opportunity opp : oppsToUpdate.values()) {
                             Set<ID> conIds = new Set<ID>();
                                     for (Contract con : contractsForOpps.values()
                                     ) {
                                                 if (con.Opportunity__c == opp.Id)
                        conIds.add(con.Id);
                                }
               
                if (opp.Total_Contracts__c != conIds.size())
                    opp.Total_Contracts__c = conIds.size();
                               }
                                   update oppsToUpdate.values();


//Create a set of Oppty IDs and a Map of Opptys to Update
     Set<id> opptyIds = new Set<id>(); Map<Id,Opportunity> opptysToUpdate = new Map<Id,Opportunity>();

//Add Oppty ID's to the set
     if(Trigger.isInsert || Trigger.isUpdate){

          for (Contract c : Trigger.new) {

               opptyIds.add(c.Opportunity__c); } }

     if (Trigger.isUpdate || Trigger.isDelete) {

          for (Contract c : Trigger.old) {

               opptyIds.add(c.Opportunity__c); } }

//Populate the opptysToUpdate map with fields from the Oppty and related Contracts:
     for (Opportunity opptys :

          [SELECT Id, Signed_Contracts__c,

               (SELECT Id, RPM_Signed_Contract__c

               FROM Contracts__r)

          FROM Opportunity WHERE Id in :opptyIds]){

         opptysToUpdate.put(opptys.id,opptys);

     }


//For every oppty...
     for (Opportunity oppty : opptysToUpdate.values()) {

//Initialize all sums to '0':
     double sumSigned = 0;

//... loop through associated contracts and calculate the signed ones:
     for (Contract cnt: oppty.Contracts__r){
          sumSigned += cnt.RPM_Signed_Contract__c;

     }

//Set the rollup sum on the current oppty:
     oppty.Signed_Contracts__c = sumSigned;
     }

//Finally update all affected goals:
update opptysToUpdate.values();


//Create a set of Oppty IDs and a Map of Opptys to Update
     Set<id> opptyIds2 = new Set<id>(); Map<Id,Opportunity> opptysToUpdate2 = new Map<Id,Opportunity>();

//Add Oppty ID's to the set
     if(Trigger.isInsert || Trigger.isUpdate){

          for (Contract c : Trigger.new) {

               opptyIds2.add(c.Opportunity__c); } }

     if (Trigger.isUpdate || Trigger.isDelete) {

          for (Contract c : Trigger.old) {

               opptyIds2.add(c.Opportunity__c); } }

//Populate the opptysToUpdate map with fields from the Oppty and related Contracts:
     for (Opportunity opptys2 :

          [SELECT Id, Cancelled_Recalled_Contracts__c,

               (SELECT Id, Cancelled_Recalled_Ind__c

               FROM Contracts__r)

          FROM Opportunity WHERE Id in :opptyIds2]){

         opptysToUpdate2.put(opptys2.id,opptys2);

     }


//For every oppty...
     for (Opportunity oppty2 : opptysToUpdate2.values()) {

//Initialize all sums to '0':
     double sumSigned = 0;

//... loop through associated contracts and calculate the signed ones:
     for (Contract cnt: oppty2.Contracts__r){
          sumSigned += cnt.Cancelled_Recalled_Ind__c;

     }

//Set the rollup sum on the current oppty:
     oppty2.Cancelled_Recalled_Contracts__c = sumSigned;
     }

//Finally update all affected goals:
update opptysToUpdate2.values();

}

 

 

Thanks!