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
Mamadou Diallo 14Mamadou Diallo 14 

Initial term of field expression must be a concrete SObject: List<Opportunity>

Hello, I'm creating a test class for a visualforce controller but I'm getting the "Initial term of field expression must be a concrete SObject: List<Opportunity>" error. 

I have a formula field on the opportunity object : Number_of_Employess_Summary__c

Here is my code
@isTest
public class MyController {
    static testMethod void ringMyBell(){
        Integer numAccts;
        Integer numOppsPerAcct;
        List<Account> accts = new List<Account>();
        
        for(Integer i=0;i<numAccts;i++) {
            Account a = new Account(Name='TestAccount' + i,
                                   RecordTypeId = '012800000003UY8', 
                                   Type = 'Prospect',  
                                   Number_Of_Employees__c = 190 + i
                                   );
            accts.add(a);
        }
        insert accts;
        
        List<Opportunity> opps = new List<Opportunity>();
        for (Integer j=0;j<numAccts;j++) {
            Account acct = accts[j];
            // For each account just inserted, add opportunities
            for (Integer k=0;k<numOppsPerAcct;k++) {
                opps.add(new Opportunity(Name=acct.Name + ' Opportunity ' + k,
                                       CloseDate=System.today().addMonths(1),
                                       StageName = 'Closed Won',
                                       Number_Of_Employees_From_Opp = 200 + k,
                                       AccountId=acct.Id));
            }
        }
        // Insert all opportunities for all accounts.
        insert opps;
        //Populate the formula field Eligible Employees Summary
        for(List<Opportunity> testOppAfterInsert: opps){
         testOppAfterInsert = [select Name, Owner.Name, Number_Of__Employees_Summary__c, Number_Of_Employees_From_Opp, Account.Number_Of__Employees__c, StageName from Opportunity 
                                          WHERE Id IN :opps.Id];
        }  
         MyController xyz = new MyController();
          }
}

Help needed please.

Thank you.
Best Answer chosen by Mamadou Diallo 14
Prateek Singh SengarPrateek Singh Sengar
One error i see is that on line 33 you didnt replaced as i mentioned.
It should be
for(Opportunity testOppAfterInsert: opps)
and not
for(testOppAfterInsert: opps)

Also if the only thing you want is to query some addition value you can replace line 33 to 36 with
//Populate the formula field Eligible Employees Summary
        List<Opportunity> testOppAfterInsert = new List<Opportunity>();
        testOppAfterInsert = [select Name, Owner.Name, Number_Of__Employees_Summary__c, Number_Of_Employees_From_Opp, Account.Number_Of__Employees__c, StageName from Opportunity
                               WHERE Id IN :opps];


 

All Answers

Prateek Singh SengarPrateek Singh Sengar
Hi Mamadou Diallo

On Line number 33 in for loop you have 
for(List<Opportunity> testOppAfterInsert: opps)
Please replace it with 
for(Opportunity testOppAfterInsert: opps)
The syntax of foreach loop expect the intial variable to be of concrete type and not a collection.


On a side note it looks like you are executing query inside for loop. This is a big No, Please follow the Apex best practices and ensure that your code is bulkified.

Hope this helps.
Mamadou Diallo 14Mamadou Diallo 14
Thank you Prateek for your help. I made the changes you recommended but I'm still getting the same error.
Prateek Singh SengarPrateek Singh Sengar
Can you please share the code on how it looks now. The above points should have fixed your error.
Mamadou Diallo 14Mamadou Diallo 14
@isTest
public class TestingRingTheBell {
    static testMethod void ringMyBell(){
        Integer numAccts;
        Integer numOppsPerAcct;
        List<Account> accts = new List<Account>();
        
        for(Integer i=0;i<numAccts;i++) {
            Account a = new Account(Name='TestAccount' + i,
                                   RecordTypeId = '012800000003UY8', 
                                   Type = 'Prospect',  
                                   Number_Of_Employees__c = 190 + i
                                   );
            accts.add(a);
        }
        insert accts;
        
        List<Opportunity> opps = new List<Opportunity>();
        for (Integer j=0;j<numAccts;j++) {
            Account acct = accts[j];
            // For each account just inserted, add opportunities
            for (Integer k=0;k<numOppsPerAcct;k++) {
                opps.add(new Opportunity(Name=acct.Name + ' Opportunity ' + k,
                                       CloseDate=System.today().addMonths(1),
                                       StageName = 'Closed Won',
                                       Number_Of_Employees_From_Opp = 200 + k,
                                       AccountId=acct.Id));
            }
        }
        // Insert all opportunities for all accounts.
        insert opps;
        //Populate the formula field Eligible Employees Summary
        for(testOppAfterInsert: opps){
         testOppAfterInsert = [select Name, Owner.Name, Number_Of__Employees_Summary__c, Number_Of_Employees_From_Opp, Account.Number_Of__Employees__c, StageName from Opportunity 
                                          WHERE Id IN :opps.Id];
        }  
         MyController xyz = new MyController();
          }
}

Thank you.
Mamadou Diallo 14Mamadou Diallo 14
Here is what I'm trying to do. I just want to query the formula field Number_Of__Employees_Summary__c. 
Prateek Singh SengarPrateek Singh Sengar
One error i see is that on line 33 you didnt replaced as i mentioned.
It should be
for(Opportunity testOppAfterInsert: opps)
and not
for(testOppAfterInsert: opps)

Also if the only thing you want is to query some addition value you can replace line 33 to 36 with
//Populate the formula field Eligible Employees Summary
        List<Opportunity> testOppAfterInsert = new List<Opportunity>();
        testOppAfterInsert = [select Name, Owner.Name, Number_Of__Employees_Summary__c, Number_Of_Employees_From_Opp, Account.Number_Of__Employees__c, StageName from Opportunity
                               WHERE Id IN :opps];


 
This was selected as the best answer
Mamadou Diallo 14Mamadou Diallo 14
Thank Prateek but I made the changes and I got the same error. Here is my code
 
@isTest
public class TestingRingTheBell {
    static testMethod void ringMyBell(){
        Integer numAccts;
        Integer numOppsPerAcct;
        List<Account> accts = new List<Account>();
        
        for(Integer i=0;i<numAccts;i++) {
            Account a = new Account(Name='TestAccount' + i,
                                   RecordTypeId = '012800000003UY8', 
                                   Type = 'Prospect',  
                                   Number_Of_Employees__c = 190 + i
                                   );
            accts.add(a);
        }
        insert accts;
        
        List<Opportunity> opps = new List<Opportunity>();
        for (Integer j=0;j<numAccts;j++) {
            Account acct = accts[j];
            // For each account just inserted, add opportunities
            for (Integer k=0;k<numOppsPerAcct;k++) {
                opps.add(new Opportunity(Name=acct.Name + ' Opportunity ' + k,
                                       CloseDate=System.today().addMonths(1),
                                       StageName = 'Closed Won',
                                       Number_Of_Employees_From_Opp = 200 + k,
                                       AccountId=acct.Id));
            }
        }
        // Insert all opportunities for all accounts.
        insert opps;
        //Populate the formula field Eligible Employees Summary
       List<Opportunity> testOppAfterInsert = new List<Opportunity>();
         testOppAfterInsert = [select Name, Owner.Name, Number_Of__Employees_Summary__c, Number_Of_Employees_From_Opp, Account.Number_Of__Employees__c, StageName from Opportunity 
                                          WHERE Id IN :opps.Id];
        }  
         MyController xyz = new MyController();
          }
}

 
Prateek Singh SengarPrateek Singh Sengar
You are still getting the following error or it has changed?

Initial term of field expression must be a concrete SObject: List<Opportunity>" error

And looks like you did not used my exact code. Please note the difference in where clause. I have mentioned opps and you are using opps.Id
Mamadou Diallo 14Mamadou Diallo 14
Thank you Prateek. It works. 

You are the best.
Mamadou Diallo 14Mamadou Diallo 14
Prateek, I no longer has the error message but still the code coverage is at 0%. Can you help on that?
Thanks.