You need to sign in to do that
Don't have an account?
Nicholas Mirams
System.DmlException: Update failed. Duplicate id in list: Error
Hi,
I am getting an error when testing and trying to deploy the below trigger
How do I change the list to a map as I think thats the error. I am not an apex coder and am trying to learn
first trigger which updates a contact record to customer upon the opportunity setting to won (contact role): -
trigger UpdateContactToCustomer on Opportunity (after insert, after update) {
OpportunityContactRole ocr;
Contact contact;
Opportunity opp = Trigger.new[0];
list<Contact> listToUpdate = new list<Contact>();
if(opp.StageName == 'Complete - Live'){
for(OpportunityContactRole iterating_oppConRole : [SELECT o.Role,
o.OpportunityId,
o.IsPrimary,
o.Id,
o.ContactId,
o.Contact.Marketing_Prospect_Level__c
FROM OpportunityContactRole o where o.OpportunityId =: opp.id])
{
Contact tempContact = new Contact(id=iterating_oppConRole.ContactId, Marketing_Prospect_Level__c = 'Customer');
listToUpdate.add(tempContact);
}
}
if(!listToUpdate.isEmpty())
update listToUpdate;
}
I am getting an error when testing and trying to deploy the below trigger
How do I change the list to a map as I think thats the error. I am not an apex coder and am trying to learn
first trigger which updates a contact record to customer upon the opportunity setting to won (contact role): -
trigger UpdateContactToCustomer on Opportunity (after insert, after update) {
OpportunityContactRole ocr;
Contact contact;
Opportunity opp = Trigger.new[0];
list<Contact> listToUpdate = new list<Contact>();
if(opp.StageName == 'Complete - Live'){
for(OpportunityContactRole iterating_oppConRole : [SELECT o.Role,
o.OpportunityId,
o.IsPrimary,
o.Id,
o.ContactId,
o.Contact.Marketing_Prospect_Level__c
FROM OpportunityContactRole o where o.OpportunityId =: opp.id])
{
Contact tempContact = new Contact(id=iterating_oppConRole.ContactId, Marketing_Prospect_Level__c = 'Customer');
listToUpdate.add(tempContact);
}
}
if(!listToUpdate.isEmpty())
update listToUpdate;
}
The above isn't bulkified. Please find the updated and bulkified code..
Please mark as best answer if the above helps...!!!
I am still getting the same duplicate error when testing this trigger, but the error is pointing to another trigger on opportunity which updates some checkboxes on the same contact record when the opportunity is closed won. This error is: -
System.DmlException: Update failed. First exception on row 0 with id 0069E000007aUMmQAM; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Updateproductsbought: execution of AfterUpdate
caused by: System.ListException: Duplicate id in list: 0039E00000Yv4kyQAB
Trigger.Updateproductsbought: line 81, column 1: []
Stack TraceClass.testUpdateContactToCustomer.ValidateContactAsCustomer: line 65, column 1
the other trigger is: -
trigger Updateproductsbought on Opportunity (after update) {
Set<Id> st_OppId = new Set<Id>();
List<Contact> con_List;
for(Opportunity opp :Trigger.new)
{
if(opp.StageName=='Complete - Live' )
st_OppId.add(opp.Id);
}
if(st_OppId!=null && !st_Oppid.isEmpty())
{
for(OpportunityContactRole iterating_oppConRole : [SELECT Role,
OpportunityId,
IsPrimary,
Id,
ContactId,
Contact.Order_Routing__c,
Contact.Settlements__c,
Contact.Dividends__c,
Contact.Transfers__c,
Contact.Payments__c,
Contact.Re_registration__c,
Contact.Reporting_Reconciliations__c,
Opportunity.CTN_Service__c
FROM OpportunityContactRole where OpportunityId IN : st_OppId])
{
if(iterating_oppConRole.Opportunity.CTN_Service__c == 'Order Routing')
{
iterating_oppConRole.Contact.Order_Routing__c = true;
}
if(iterating_oppConRole.Opportunity.CTN_Service__c == 'Settlements')
{
iterating_oppConRole.Contact.Settlements__c = true;
}
if(iterating_oppConRole.Opportunity.CTN_Service__c == 'Dividends')
{
iterating_oppConRole.Contact.Dividends__c = true;
}
if(iterating_oppConRole.Opportunity.CTN_Service__c == 'Transfers')
{
iterating_oppConRole.Contact.Transfers__c = true;
}
if(iterating_oppConRole.Opportunity.CTN_Service__c == 'Payments')
{
iterating_oppConRole.Contact.Payments__c = true;
}
if(iterating_oppConRole.Opportunity.CTN_Service__c == 'Re-registration')
{
iterating_oppConRole.Contact.Re_registration__c = true;
}
if(iterating_oppConRole.Opportunity.CTN_Service__c == 'Reporting_Reconciliations__c')
{
iterating_oppConRole.Contact.Reporting_Reconciliations__c = true;
}
if(iterating_oppConRole.Opportunity.CTN_Service__c == 'Data Services Platform')
{
iterating_oppConRole.Contact.Data_Services_Platform__c = true;
}
if(con_List == null)
con_List = new List<Contact>();
con_List.add(iterating_oppConRole.Contact);
}
}
if(con_List!=null && !con_List.isEmpty())
update con_List;
}
research tells me I need to use map instead of list to prevent any duplicates but I am stumped how to ressolve this one or the other one with this error