-4
trigger orderreceived on Quote__c (after update,after insert) { 
Profile pr = [select id from Profile where name='USA User'];
Set<id> QuoteId = new Set<id>();
    for(Quote__c q:Trigger.new){
    if(UserInfo.getProfileId()==pr.id){
     if (q.Order_Received__c!= Null && q.Order_Received__c == true )
        QuoteId.add(q.Id);
    }
    }
 list<task> tasklist=[SELECT Id,whatId,whoid FROM Task where whatid=:Quoteid ];
for(task t: tasklist){
t.status='completed';
tasklist.add(t);
}
update tasklist;
}

ERROR:

execution of AfterUpdate caused by: System.FinalException: Cannot modify a collection while it is being iterated.

  • 1
    Can you post your test class as well? BTW, it's not a good idea to perform DMLs inside a loop(task update in your trigger) – highfive Mar 27 '14 at 5:15
  • I have changed that one now i am getting this errorexecution of AfterUpdate caused by: System.FinalException: Cannot modify a collection while it is being iterated. – Teja Mar 27 '14 at 5:50
7

The loop:

for(task t: tasklist){
   t.status='completed';
   tasklist.add(t);
}

will generate System.finalException because you are modifying the collection: tasklist by adding members to it while you are iterating. You do not need the tasklist.add(t). Simply:

for(task t: tasklist){
    t.status='completed';
}

will do

  • plain and simple ... direct to the point ! – JOE Aug 2 '16 at 17:14
1

This the default behavior of Collections in Apex that they can't be modified during iterations. If we talk about Java, we have the option of obtaining the Iterator by calling iterator() method on the collection and then we may perform the modification in those iterations. But the Collection Framework of Apex doesn't seem to possess this feature even if they have the iterator() method available.

Rather you may refer to this post describing almost the same issue: ( here )

1) When you want to add/remove items from the collection during iteration. This trivial example will throw a "Cannot modify a collection while it is being iterated." exception in Apex:

List<String> testloop = new List<String>();
testloop.add('a');
testloop.add('b');
for (String astring : testloop) {
    testloop.remove(0);
}

But this will not:

List<String> testloop = new List<String>();
testloop.add('a');
testloop.add('b');
for (Integer i = 0; i < testloop.size(); i++) {
    testloop.remove(0);
}

(The second example, besides being horribly inefficient, also demonstrates why you need to be careful when modifying lists during iteration - it has a bug in it.)
-3
trigger orderreceived on Quote__c (after update,after insert) { 
Profile pr = [select id from Profile where name='USA User'];
list<task> ta=new list<task>();
Set<id> QuoteId = new Set<id>();
    for(Quote__c q:Trigger.new){
    if(UserInfo.getProfileId()==pr.id){
     if (q.Order_Received__c!= Null && q.Order_Received__c == true )
        QuoteId.add(q.Id);
    }
    }
 list<task> tasklist=[SELECT Id,whatId,whoid FROM Task where whatid=:Quoteid ];
for(task t: tasklist){
t.status='completed';
ta.add(t);
}
if(!ta.isEmpty())
update ta;
}
  • Please provide some more information when answering questions. – Boris Bachovski Mar 27 '14 at 8:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.