Skip to main content
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.
10 answers
  1. Aug 25, 2016, 3:42 PM
    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];

     
0/9000