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
VijayasaharanVijayasaharan 

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

Hi All
I have searched for multiple post to find an answer for this common error but I am not sure what mistake I am commiting.. All I need to do query a local list and add ID's to a SET so that I dont execute multiple SOQL's. Appreciate your help on this

My CODE
I am using "Execute Highlight" option to see if my code works and it breaks in line 7
Line: 7, Column: 18
Initial term of field expression must be a concrete SObject: List<Allocation__c>
 
List <Allocation__C> lst =  new List <Allocation__c>();
lst = [Select  Percentage__c, Line_of_Business__c, Support_Contract__c
                          from Allocation__C WHERE Percentage__c = 50 LIMIT 1 ];
Set<ID> supConID = new Set<ID>(); --IS THIS THE CORRECT DATA STRUCTURE
for(Allocation__C al :lst) -- THIS IS WHERE THE ERROR HAPPENS, 
{
    supConID.Add(lst.Support_Contract__c);
}
List <Request__C> req = new List <Request__c>();
req = [Select ID, LOB_Allocations__c 
                         FROM Request__c WHERE ID IN (Select  Support_Contract__c
                          from Allocation__C WHERE Percentage__c = 50)];


List <Allocation__C> alloc = new <Allocation__c>();
List <Request__C> reqUpdate = new <Request__C>();
for(Request__C r: req)
{
    string lobstring = '';
    Request__c rtemp = new Request__c();
    for(Allocation__c a: [select Line_of_Business__c, Percentage__c FROM lst WHERE Support_Contract__c = :r.ID])
    {
        lobstring += (lobstring ==''?'':' / ')+a.Line_of_Business__c+'('+  a.Percentage__c + '%)';
    }
    rtemp.ID = r.ID;
    rtemp.LOB_Allocations__c = lobstring;
    reqUpdate.Add(rtemp);
    
}
update reqUpdate;
/*List<Request__C, string> fnl = new List<Request__c , string>;
for ( Allocation__C ac :lst)
{
    ac.Allocation_Notes__c = ac.Allocation_Notes__c;
    update ac;
}*/
Best Answer chosen by Vijayasaharan
Saurabh BSaurabh B
Hi Vijayasaharan,
At Line 5, you are already iterating Allocation__C object. So instead of doing "supConID.Add(lst.Support_Contract__c);" what you should be doing is this "supConID.Add(al.Support_Contract__c);"... See line 7 of updated code below .. This will create a set of all your "Support_Contract__c" Ids in supConID
 
List <Allocation__C> lst =  new List <Allocation__c>();
lst = [Select  Percentage__c, Line_of_Business__c, Support_Contract__c
                          from Allocation__C WHERE Percentage__c = 50 LIMIT 1 ];
Set<ID> supConID = new Set<ID>(); <b>--<i>IS THIS THE CORRECT DATA STRUCTURE</i></b>
for(Allocation__C al :lst) <b>-- THIS IS WHERE THE ERROR HAPPENS, </b>
{
    supConID.Add(al.Support_Contract__c);
}
List <Request__C> req = new List <Request__c>();
req = [Select ID, LOB_Allocations__c 
                         FROM Request__c WHERE ID IN (Select  Support_Contract__c
                          from Allocation__C WHERE Percentage__c = 50)];


List <Allocation__C> alloc = new <Allocation__c>();
List <Request__C> reqUpdate = new <Request__C>();
for(Request__C r: req)
{
    string lobstring = '';
    Request__c rtemp = new Request__c();
    for(Allocation__c a: [select Line_of_Business__c, Percentage__c FROM lst WHERE Support_Contract__c = :r.ID])
    {
        lobstring += (lobstring ==''?'':' / ')+a.Line_of_Business__c+'('+  a.Percentage__c + '%)';
    }
    rtemp.ID = r.ID;
    rtemp.LOB_Allocations__c = lobstring;
    reqUpdate.Add(rtemp);
    
}
update reqUpdate;

Please mark this as Best Answer if it helps you!

All Answers

Saurabh BSaurabh B
Hi Vijayasaharan,
At Line 5, you are already iterating Allocation__C object. So instead of doing "supConID.Add(lst.Support_Contract__c);" what you should be doing is this "supConID.Add(al.Support_Contract__c);"... See line 7 of updated code below .. This will create a set of all your "Support_Contract__c" Ids in supConID
 
List <Allocation__C> lst =  new List <Allocation__c>();
lst = [Select  Percentage__c, Line_of_Business__c, Support_Contract__c
                          from Allocation__C WHERE Percentage__c = 50 LIMIT 1 ];
Set<ID> supConID = new Set<ID>(); <b>--<i>IS THIS THE CORRECT DATA STRUCTURE</i></b>
for(Allocation__C al :lst) <b>-- THIS IS WHERE THE ERROR HAPPENS, </b>
{
    supConID.Add(al.Support_Contract__c);
}
List <Request__C> req = new List <Request__c>();
req = [Select ID, LOB_Allocations__c 
                         FROM Request__c WHERE ID IN (Select  Support_Contract__c
                          from Allocation__C WHERE Percentage__c = 50)];


List <Allocation__C> alloc = new <Allocation__c>();
List <Request__C> reqUpdate = new <Request__C>();
for(Request__C r: req)
{
    string lobstring = '';
    Request__c rtemp = new Request__c();
    for(Allocation__c a: [select Line_of_Business__c, Percentage__c FROM lst WHERE Support_Contract__c = :r.ID])
    {
        lobstring += (lobstring ==''?'':' / ')+a.Line_of_Business__c+'('+  a.Percentage__c + '%)';
    }
    rtemp.ID = r.ID;
    rtemp.LOB_Allocations__c = lobstring;
    reqUpdate.Add(rtemp);
    
}
update reqUpdate;

Please mark this as Best Answer if it helps you!
This was selected as the best answer
VijayasaharanVijayasaharan
Thank You Saurab. That was a silly mistake. I still have one more issue where I am trying to query a LIST to find the matching records and that throws an error
Line 21
  for(Allocation__c a: [select Line_of_Business__c, Percentage__c FROM lst WHERESupport_Contract__c = :r.ID])

The intention is to hit the database as few times as possible. What data structure should I use for me to query. In .NET LINQ we can do these, so I am assuming we can do something similar in here, may be I am not getting the correct syntax or not using the right data structure.
VijayasaharanVijayasaharan
I did workaround the issue by using the following code, but I feel this is not efficient
 
for(Request__C r: req)
{
    for(Allocation__c a: lst)
    {
        if(a.Support_Contract__c == r.ID)
        {

 
Saurabh BSaurabh B
Hi Vijayasaharan,
As long as you dont add SOQL query in 'For' loop, it should be fine.

Also, to reduce number of records you want to hit, you can try using Maps collection. See example below,
 
//directly create a Map of Request__c Id and sObject fields
Map<ID,Request__c> mapReqs = new Map<ID,Request__c>([ Select Id, Name From Request__c Where Id in :req]); 
for(Request__c r : mapReqs.values()) { //Similarly, to iterate over Keys, you can use mapReqs.keySet() method

     Your logic ... 

}

Hope this helps.