-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.

2
  • 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, 2014 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, 2014 at 5:50

3 Answers 3

9

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

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

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;
}
1
  • Please provide some more information when answering questions. Mar 27, 2014 at 8:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .