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
PRIYAN NEERUDUPRIYAN NEERUDU 

error in trigger : duplicate value found: <unknown> duplicates value on record with id: <unknown>

trigger CONUP on Contact (after update,after insert) {
   
    Map<Id, Decimal> acctIdToAmount = new Map<Id, Decimal>();
    
    List<Account> accountsToUpdate = new List<Account>();
   
    Map<ID,RecordType> typeMap = New Map<ID,RecordType>([SELECT Id,name from RecordType where SobjectType='Contact' and Name LIKE'%CONTACT%']);
    
    for (CONTACT CON : trigger.new) {
          // If the Record Type = Intake Form
            if (CON.RecordTypeId =='01228000000Qjh3AAC' || CON.RecordTypeId =='01228000000QjgyAAC'){
            //    Get the Contact Amount into a temp variable
            Decimal sum = CON.PAID_AMOUNT__c == null ? 0 : CON.PAID_AMOUNT__c;

            //    Sum it up with the already collected Amount    
            if(acctIdToAmount.containsKey(CON.AccountId))
                sum += acctIdToAmount.get(CON.AccountId); 
            
            //    Make a map with AccountId as Key and Amount as value     
             acctIdToAmount.put(CON.AccountId, sum);
          }
  for(Id accId : acctIdToAmount.keyset()) {
        Account acc = new Account(Id = accId, TOTAL_AMOUNT__c = acctIdToAmount.get(accId));
        accountsToUpdate.add(acc);
    }

    if(!accountsToUpdate.isEmpty())
        update accountsToUpdate;
}
}

can anyone help to resolve the above error............
Best Answer chosen by PRIYAN NEERUDU
BALAJI CHBALAJI CH
Hi Priyan,

Please find below modified code.
 
trigger record on Contact (after update,after insert) 
{
    Map<Id, Decimal> acctIdToAmount = new Map<Id, Decimal>();
    
    for(Account ac : [select id, Total_Amount__c from Account])
    {
        Decimal s = ac.Total_Amount__c  == null ? 0 : ac.Total_Amount__c;
        acctIdToAmount.put(ac.id, s);
    }
    List<Account> accountsToUpdate = new List<Account>();
    
    
    Map<ID,RecordType> typeMap = New Map<ID,RecordType>([SELECT Id,name from RecordType where SobjectType='Contact' and Name LIKE'%CONTACT%']);
    for (CONTACT CON : trigger.new) 
    {
        // If the Record Type = Intake Form
        if (CON.RecordTypeId == '01228000000QjgyAAC' || CON.RecordTypeId == '01228000000Qjh3AAC') 
        {
            //    Get the Contact Amount into a temp variable
            Decimal sum = CON.PAID_AMOUNT__c  == null ? 0 : CON.PAID_AMOUNT__c;
            
            //    Sum it up with the already collected Amount    
            if(acctIdToAmount.containsKey(CON.AccountId))
                sum += acctIdToAmount.get(CON.AccountId); 
            
            //    Make a map with AccountId as Key and Amount as value     
            acctIdToAmount.put(CON.AccountId, sum);
        }
        
        else if(CON.RecordTypeId == '01228000000QjgtAAC') 
        {
            Decimal sum = 0;
            
            if(acctIdToAmount.containsKey(CON.AccountId))
                sum = acctIdToAmount.get(CON.AccountId);
            
            sum -= CON.PAID_AMOUNT__c == null ? 0 : CON.PAID_AMOUNT__c; 
            
            acctIdToAmount.put(CON.AccountId, sum);    
        }      
    }
    
    for(Id accId : acctIdToAmount.keyset()) 
    {
        Account acc;
        if(acctIdToAmount.get(accId) >= 0)
            acc = new Account(Id = accId, Total_Amount__c = acctIdToAmount.get(accId));
        
        else
            acc = new Account(Id = accId, Pending_Amount__c = acctIdToAmount.get(accId));
        accountsToUpdate.add(acc);
    }
    
    if(!accountsToUpdate.isEmpty())
        update accountsToUpdate;
}

Let us know if that helps you.

Best Regards,
BALAJI

All Answers

veda shriveda shri
Hi Please try below code
 
trigger CONUP on Contact (after update,after insert) {
   
    Map<Id, Decimal> acctIdToAmount = new Map<Id, Decimal>();
    
    List<Account> accountsToUpdate = new List<Account>();
   
    Map<ID,RecordType> typeMap = New Map<ID,RecordType>([SELECT Id,name from RecordType where SobjectType='Contact' and Name LIKE'%CONTACT%']);
    
    for (CONTACT CON : trigger.new) {
          // If the Record Type = Intake Form
            if (CON.RecordTypeId =='01228000000Qjh3AAC' || CON.RecordTypeId =='01228000000QjgyAAC'){
            //    Get the Contact Amount into a temp variable
            Decimal sum = CON.PAID_AMOUNT__c == null ? 0 : CON.PAID_AMOUNT__c;

            //    Sum it up with the already collected Amount    
            if(acctIdToAmount.containsKey(CON.AccountId))
                sum += acctIdToAmount.get(CON.AccountId); 
            
            //    Make a map with AccountId as Key and Amount as value     
             acctIdToAmount.put(CON.AccountId, sum);
          }
  for(Id accId : acctIdToAmount.keyset()) {
        Account acc = new Account(Id = accId, TOTAL_AMOUNT__c = acctIdToAmount.get(accId));
        accountsToUpdate.add(acc);
    }

   
}

 if(!accountsToUpdate.isEmpty())
        update accountsToUpdate;
}

you have added  update dml statement inside for loop.I think thats the reason for this issue.
if(!accountsToUpdate.isEmpty())
        update accountsToUpdate;


 
PRIYAN NEERUDUPRIYAN NEERUDU
no the error is not due to that ...i guess ...aftr using above code also i have same error
BALAJI CHBALAJI CH
Hi Priyan,

As a Best Practice, DML statements should not be inside FOR loop as above said by @Veda Shri.

But that would not be the reason for the Error:  "duplicate value found: <unknown> duplicates value on record with id: <unknown>" which you are getting.  I think you already have a Trigger with that name CONUO, try changing the name of your trigger from CONUP to something else like CONUP1.

Let us know if that helps you.

Best Regards,
BALAJI
veda shriveda shri
please share on which line it is showing error?
veda shriveda shri
Hi BalaJi,

It is not because of trigger name. it is because of DML statement. If the list contains same ID twice and ifwe try to update it. it will give this error.

 
veda shriveda shri
Hi Priyan, please check you have any trigger on Account which will update contact which makes recursion in trigger. This may be the cause for issue. 
PRIYAN NEERUDUPRIYAN NEERUDU
trigger recoerdtypeup on Contact (before insert) {
 Map<Id, Decimal> acctIdToAmount = new Map<Id, Decimal>();
    
    Map<ID,RecordType> Mapvar = New Map<ID,RecordType>([SELECT Id from RecordType where SobjectType='Contact' and Name LIKE'%CONTACT%']);
  List<Account> ACUP = new List<Account>();
    for(CONTACT CON : TRIGGER.NEW){
        if ( CON.RecordTypeId=='01228000000QjgyAAC' || CON.RecordTypeId=='01228000000Qjh3AAC'){
           Decimal sum = CON.PAID_AMOUNT__c == null ? 0 : CON.PAID_AMOUNT__c;
            if(acctIdToAmount.containsKey(CON.AccountId))
                sum += acctIdToAmount.get(CON.AccountId);
                acctIdToAmount.put(CON.AccountId, sum);
    
        }else if(CON.RecordTypeId=='01228000000QjgtAAC') {
            Decimal sum = 0;
                if(acctIdToAmount.containsKey(CON.AccountId))
                sum = acctIdToAmount.get(CON.AccountId);
              sum -= CON.PAID_AMOUNT__c == null ? 0 : CON.PAID_AMOUNT__c;
            system.debug(sum);    
       if(sum>0)
       {      
             acctIdToAmount.put(CON.AccountId, sum);        
            for(Id accId : acctIdToAmount.keyset()) {
            Account acc = new Account(Id = accId, TOTAL_AMOUNT__c = acctIdToAmount.get(accId));
            ACUP.add(acc);
                 system.debug(acc.TOTAL_AMOUNT__c); 
            }  
            } else  if(sum<0){
                acctIdToAmount.put(CON.AccountId, sum);    
    
    for(Id accId : acctIdToAmount.keyset()) {
        Account acc = new Account(Id = accId, PENDING_AMOUNT__c = acctIdToAmount.get(accId));
        ACUP.add(acc);
    } 
            }
           
           
 }
       


}
     if(!ACUP.isEmpty())
        update ACUP;
}



this trigger has to update account amount field when v hv contacts of first 2 record types thier paidamonts should sumup and when i choose contact with 3recordtype den already choose contactrecordtype paid amount should get deducted with 3recordtype contact paid amount
BALAJI CHBALAJI CH
Hello Veda Shri,

The error might be also due to duplicate Trigger name. To make sure, you can try to check it practically.
Try creating two triggers named CONUP and CONUP1 in your Dev Org like below:
User-added image

Now try changing the name of one trigger to other. For example, change name of CONUP to CONUP1 and see what is the error we are getting.
User-added image

When you open that error:
User-added image

Best Regards,
BALAJI
PRIYAN NEERUDUPRIYAN NEERUDU
hi balaji

i hv to update account fields with diff recordtype contacts paidamount field  as below 

[contactwithrecordtypeid1 (01228000000QjgyAAC)] paidamount +(plus) [contactwithrecordtypeid2 (01228000000QjgyAAC)] paidamount - (minus) [contactwithrecordtypeid3 (01228000000QjgyAAC)] paidamount=sum

if sum is +ve value update account totalamountfield else pendingamountfield

can u plz check the beloow code


trigger record on Contact (after update,after insert) {

    Map<Id, Decimal> acctIdToAmount = new Map<Id, Decimal>();
    List<Account> accountsToUpdate = new List<Account>();
    Map<ID,RecordType> typeMap = New Map<ID,RecordType>([SELECT Id,name from RecordType where SobjectType='Contact' and Name LIKE'%CONTACT%']);
    for (CONTACT CON : trigger.new) {
          // If the Record Type = Intake Form
          if (CON.RecordTypeId == '01228000000QjgyAAC' || CON.RecordTypeId == '01228000000Qjh3AAC') {
            //    Get the Contact Amount into a temp variable
            Decimal sum = CON.PAID_AMOUNT__c  == null ? 0 : CON.PAID_AMOUNT__c;

            //    Sum it up with the already collected Amount    
            if(acctIdToAmount.containsKey(CON.AccountId))
                sum += acctIdToAmount.get(CON.AccountId); 
            system.debug(sum);
            //    Make a map with AccountId as Key and Amount as value     
             acctIdToAmount.put(CON.AccountId, sum);
          }
          else if(CON.RecordTypeId == '01228000000QjgtAAC') {
              Decimal sum = 0;

            if(acctIdToAmount.containsKey(CON.AccountId))
                sum = acctIdToAmount.get(CON.AccountId);

            sum -= CON.PAID_AMOUNT__c == null ? 0 : CON.PAID_AMOUNT__c; 
                 
             acctIdToAmount.put(CON.AccountId, sum);    
          }      
    }

    for(Id accId : acctIdToAmount.keyset()) {
        Account acc;
        if(acctIdToAmount.get(accId) >= 0)
               acc = new Account(Id = accId, Total_Amount__c = acctIdToAmount.get(accId));
        else
               acc = new Account(Id = accId, Pending_Amount__c = acctIdToAmount.get(accId));
        accountsToUpdate.add(acc);
    }

    if(!accountsToUpdate.isEmpty())
        update accountsToUpdate;
}
 
BALAJI CHBALAJI CH
Hi Priyan,

Are you still getting the same error ?
Or the functionality which you said above is not working as expected ?
PRIYAN NEERUDUPRIYAN NEERUDU
Thanks,No m ant facing dat error ..........but trigger is not working as expected
BALAJI CHBALAJI CH
Hi Priyan,

Please find below modified code.
 
trigger record on Contact (after update,after insert) 
{
    Map<Id, Decimal> acctIdToAmount = new Map<Id, Decimal>();
    
    for(Account ac : [select id, Total_Amount__c from Account])
    {
        Decimal s = ac.Total_Amount__c  == null ? 0 : ac.Total_Amount__c;
        acctIdToAmount.put(ac.id, s);
    }
    List<Account> accountsToUpdate = new List<Account>();
    
    
    Map<ID,RecordType> typeMap = New Map<ID,RecordType>([SELECT Id,name from RecordType where SobjectType='Contact' and Name LIKE'%CONTACT%']);
    for (CONTACT CON : trigger.new) 
    {
        // If the Record Type = Intake Form
        if (CON.RecordTypeId == '01228000000QjgyAAC' || CON.RecordTypeId == '01228000000Qjh3AAC') 
        {
            //    Get the Contact Amount into a temp variable
            Decimal sum = CON.PAID_AMOUNT__c  == null ? 0 : CON.PAID_AMOUNT__c;
            
            //    Sum it up with the already collected Amount    
            if(acctIdToAmount.containsKey(CON.AccountId))
                sum += acctIdToAmount.get(CON.AccountId); 
            
            //    Make a map with AccountId as Key and Amount as value     
            acctIdToAmount.put(CON.AccountId, sum);
        }
        
        else if(CON.RecordTypeId == '01228000000QjgtAAC') 
        {
            Decimal sum = 0;
            
            if(acctIdToAmount.containsKey(CON.AccountId))
                sum = acctIdToAmount.get(CON.AccountId);
            
            sum -= CON.PAID_AMOUNT__c == null ? 0 : CON.PAID_AMOUNT__c; 
            
            acctIdToAmount.put(CON.AccountId, sum);    
        }      
    }
    
    for(Id accId : acctIdToAmount.keyset()) 
    {
        Account acc;
        if(acctIdToAmount.get(accId) >= 0)
            acc = new Account(Id = accId, Total_Amount__c = acctIdToAmount.get(accId));
        
        else
            acc = new Account(Id = accId, Pending_Amount__c = acctIdToAmount.get(accId));
        accountsToUpdate.add(acc);
    }
    
    if(!accountsToUpdate.isEmpty())
        update accountsToUpdate;
}

Let us know if that helps you.

Best Regards,
BALAJI
This was selected as the best answer
PRIYAN NEERUDUPRIYAN NEERUDU
thnk u ....