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

System.DmlException:Insert failed First exception on row 0; CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, ClosedOpportunityTrigger: execution of AfterInsert caused by: System.ListException: Before Insert or Upsert list must not have two identically equal elements
Hi,
Need help in this Closed Opportunity -Trigger Bulk Apex trigger trail head challenge.The following error was received when trying to check the challenge eventhough the code worked and desired result was achieved. Could not check the challenge.
Challenge Not yet complete... here's what's wrong:
There was an unexpected error in your org which is preventing this assessment check from completing: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, ClosedOpportunityTrigger: execution of AfterInsert caused by: System.ListException: Before Insert or Upsert list must not have two identically equal elements Trigger.ClosedOpportunityTrigger: line 19, column 1: []
The code is given below.
trigger ClosedOpportunityTrigger on Opportunity (after insert, after update) {
try {
List<Opportunity> Opps = [SELECT Id, StageName FROM Opportunity
WHERE StageName =:'Closed Won' AND Id IN :Trigger.New];
List<Task> tasks = new List<Task>();
Task t = new Task();
for(Opportunity opp : Opps) {
if(Trigger.isInsert || Trigger.isUpdate) {
t.Subject = 'Follow Up Test Task';
t.WhatId = opp.Id;
tasks.add(t);
}
}
System.debug(tasks.size() + ' tasks size');
if(tasks.size() > 0) {
upsert tasks;
}
}catch (DmlException e) {
System.debug('A DML exception has occurred: ' +
e.getMessage());
}
}
Thanks,
Chithra
Need help in this Closed Opportunity -Trigger Bulk Apex trigger trail head challenge.The following error was received when trying to check the challenge eventhough the code worked and desired result was achieved. Could not check the challenge.
Challenge Not yet complete... here's what's wrong:
There was an unexpected error in your org which is preventing this assessment check from completing: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, ClosedOpportunityTrigger: execution of AfterInsert caused by: System.ListException: Before Insert or Upsert list must not have two identically equal elements Trigger.ClosedOpportunityTrigger: line 19, column 1: []
The code is given below.
trigger ClosedOpportunityTrigger on Opportunity (after insert, after update) {
try {
List<Opportunity> Opps = [SELECT Id, StageName FROM Opportunity
WHERE StageName =:'Closed Won' AND Id IN :Trigger.New];
List<Task> tasks = new List<Task>();
Task t = new Task();
for(Opportunity opp : Opps) {
if(Trigger.isInsert || Trigger.isUpdate) {
t.Subject = 'Follow Up Test Task';
t.WhatId = opp.Id;
tasks.add(t);
}
}
System.debug(tasks.size() + ' tasks size');
if(tasks.size() > 0) {
upsert tasks;
}
}catch (DmlException e) {
System.debug('A DML exception has occurred: ' +
e.getMessage());
}
}
Thanks,
Chithra
Please mark this as Best Answer if it helps you!
P.S.
Solution was here "Trigger.isInsert || Trigger.isUpdate"
Saurabh B and
Grisha Pishchyk you both have solved my problem within seconds!!
But can you please provide its brief explanation!!