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
Salesforce Developer MOSalesforce Developer MO 

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

Hello,

 

Im receiving the error in the subject line. Im trying to loop through records in a custom object and load them into the Contact object. Here is the code...

 

public list<Contact> contactInsertItem {get;set;}
public list<Contact> contactInsertList {get;set;}

public SalesConnectContactQuickBuildController (ApexPages.StandardSetController controller) {
...
	List<CustomObject1__c> unmatchedContactList = new List<CustomObject1__c>([SELECT field1__c, field2__c, field3__c FROM CustomObject1__c WHERE Id in :tempSet]);
	
	for(integer i=0; i<unmatchedContactList.size();i++){
			
        	contactInsertItem = new list<Contact>();
        	//***error occurs here*** contactInsertItem.field1__c = unmatchedContactList[i].field1__c;
        	contactInsertItem.field2__c = unmatchedContactList[i].field2__c;
        	contactInsertItem.field3__c = unmatchedContactList[i].field3__c;
        	contactInsertItem.field4__c = unmatchedContactList[i].field4__c;
        	contactInsertItem.field5__c = unmatchedContactList[i].field5__c;
			
		contactInsertList.add(contactInsertItem);
	}
...
}

 What am I doing incorrectly?

Best Answer chosen by Admin (Salesforce Developers) 
MaxPowerForceMaxPowerForce

You are trying to reference properties on a list collection instead of on the object.

Try changing these items:

public list<Contact> contactInsertItem {get;set;}
contactInsertItem = new list<Contact>();

to this:

public Contact contactInsertItem {get;set;}
contactInsertItem = new Contact();

Alternatively, in your loop use:

         Contact con = new Contact();
           con.field1__c = unmatchedContactList[i].field1__c;
           con.field2__c = unmatchedContactList[i].field2__c;
           con.field3__c = unmatchedContactList[i].field3__c;
           con.field4__c = unmatchedContactList[i].field4__c;
           con.field5__c = unmatchedContactList[i].field5__c;
            
    contactInsertList.add(con);

All Answers

MaxPowerForceMaxPowerForce

You are trying to reference properties on a list collection instead of on the object.

Try changing these items:

public list<Contact> contactInsertItem {get;set;}
contactInsertItem = new list<Contact>();

to this:

public Contact contactInsertItem {get;set;}
contactInsertItem = new Contact();

Alternatively, in your loop use:

         Contact con = new Contact();
           con.field1__c = unmatchedContactList[i].field1__c;
           con.field2__c = unmatchedContactList[i].field2__c;
           con.field3__c = unmatchedContactList[i].field3__c;
           con.field4__c = unmatchedContactList[i].field4__c;
           con.field5__c = unmatchedContactList[i].field5__c;
            
    contactInsertList.add(con);

This was selected as the best answer
asish1989asish1989
public list<Contact> contactInsertItem {get;set;}// not required
public list<Contact> contactInsertList {get;set;}

public SalesConnectContactQuickBuildController (ApexPages.StandardSetController controller) {
...
	
	contactInsertList = new list<Contact>();
	for(CustomObject1__c customObj1 :[SELECT field1__c, field2__c, field3__c FROM CustomObject1__c WHERE Id in :tempSet]){
		Contact con = new Contact();
		con.field2__c = customObj1.field2__c;
		con.field3__c = customObj1.field3__c;
		con.field4__c = customObj1.field4__c;
		con.field5__c = customObj1.field5__c;
		
		contactInsertList.add(con);
	}