You need to sign in to do that
Don't have an account?

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............
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............
Please find below modified code.
Let us know if that helps you.
Best Regards,
BALAJI
All Answers
you have added update dml statement inside for loop.I think thats the reason for this issue.
if(!accountsToUpdate.isEmpty())
update accountsToUpdate;
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
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.
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
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:
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.
When you open that error:
Best Regards,
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;
}
Are you still getting the same error ?
Or the functionality which you said above is not working as expected ?
Please find below modified code.
Let us know if that helps you.
Best Regards,
BALAJI