function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
RossGRossG 

test method bug

Anyone see the error in the test method below?  The thing compiles fine but I see a bug in it that is the error "cannot modifu a list while it's being iterated"

 

Annoying thing is I did this last week but forgot how I resolved this.  It's obvious I just don't see it.  Anyone see what's wrong with this code?

 

Thanks

 

	static testMethod void testEngagementsCreatedByOppUpdate()
        {

             Account acct = new Account(name='test account');              
             insert acct;
		
	     Opportunity[] oppsToCreate2 = new Opportunity[]{};
		
                 for(Integer x=0; x<200;x++)
                 {
		      Opportunity opp = new Opportunity();
                      opp.AccountId=acct.Id;
                      opp.Name = 'Test'+x;
                      opp.Probability = 10;   
                      opp.Type = 'Trial';
                      opp.CloseDate = System.Today();
                      opp.Region__c = 'East';
                      opp.Territory__c = 'BE South';
                      opp.LeadSource = 'Chat';
                      opp.StageName = 'Evaluating';
                      opp.License_type__c = 'Subscription';
                      opp.Forecasted_Amount__c = 1;
                      opp.ELA__c = 'No';
                      opp.Services_Use_Case__c = 'test text'+x;
                      opp.Work_Location__c = 'Onsite';
                      opp.Size_of_Deal__c = '0-25K';
                      opp.Services_Planned__c = 'Client Services';
                      opp.Customer_s_Expected_Start_Date__c = 'This month';
                      opp.Expected_Services_Work_Hours__c = '5-10 hours';
                      opp.Term__c = 1;
		      oppsToCreate2.add(opp);
		 
		}

		insert oppsToCreate2;

               for(Opportunity opp: oppsToCreate2)
               {
                  opp.Probability=80;
                  opp.Services_to_be_Delivered_by__c = 'Training Team';
                  oppsToCreate2.add(opp);
               }
 
                update oppsToCreate2;

                List< SFL5_Projects__c > insertedEngagements2 = [SELECT Name, Id
                                      FROM SFL5_Projects__c 
                                      WHERE Opportunity__c IN :oppsToCreate2];
              
                Integer size2 = insertedEngagements2.size();
                system.assertEquals(200,size2);   		   
		Test.stopTest();	

	}	

 

Avidev9Avidev9

Ahh this happens when try to add / remove elements , while you are iterating over the same

 

static testMethod void testEngagementsCreatedByOppUpdate()
        {

             Account acct = new Account(name='test account');              
             insert acct;
		
	     Opportunity[] oppsToCreate2 = new Opportunity[]{};
		
                 for(Integer x=0; x<200;x++)
                 {
		      Opportunity opp = new Opportunity();
                      opp.AccountId=acct.Id;
                      opp.Name = 'Test'+x;
                      opp.Probability = 10;   
                      opp.Type = 'Trial';
                      opp.CloseDate = System.Today();
                      opp.Region__c = 'East';
                      opp.Territory__c = 'BE South';
                      opp.LeadSource = 'Chat';
                      opp.StageName = 'Evaluating';
                      opp.License_type__c = 'Subscription';
                      opp.Forecasted_Amount__c = 1;
                      opp.ELA__c = 'No';
                      opp.Services_Use_Case__c = 'test text'+x;
                      opp.Work_Location__c = 'Onsite';
                      opp.Size_of_Deal__c = '0-25K';
                      opp.Services_Planned__c = 'Client Services';
                      opp.Customer_s_Expected_Start_Date__c = 'This month';
                      opp.Expected_Services_Work_Hours__c = '5-10 hours';
                      opp.Term__c = 1;
		      oppsToCreate2.add(opp);
		 
		}

		insert oppsToCreate2;

               for(Opportunity opp: oppsToCreate2)
               {
                  opp.Probability=80;
                  opp.Services_to_be_Delivered_by__c = 'Training Team';
                  //oppsToCreate2.add(opp);// This is not needed, [error is coming from here]
               }
 
                update oppsToCreate2;

                List< SFL5_Projects__c > insertedEngagements2 = [SELECT Name, Id
                                      FROM SFL5_Projects__c 
                                      WHERE Opportunity__c IN :oppsToCreate2];
              
                Integer size2 = insertedEngagements2.size();
                system.assertEquals(200,size2);   		   
		Test.stopTest();	

	}	
deepabalisfdcdeepabalisfdc

Yes. Either u take another list ,not oppsToCreate2 to add opp and update that.
Actually no need to add in list. Just update oppsToCreate2. As you are iterating the list and setting the value .

 for(Opportunity opp: oppsToCreate2)
               {
                  opp.Probability=80;
                  opp.Services_to_be_Delivered_by__c = 'Training Team';
                  //oppsToCreate2.add(opp);// This is not needed, [error is coming from here]
               }