2

I've created a list containing Contact ID values. I'm trying to add these values to a Contact Map which I will use to update Contact records.

I'm getting the error: "Initial term of field expression must be a concrete SObject: LIST on this line:" contacts.put(ocr.id, new contact(id=ocr.id, Major_Gift_Prospect_Contact__c=true));

I think I understand the message after researching it. How do I modify my code so I can add list items to the map.

Thanks,

Kevin

public with sharing class AdvancementOpportunity {

    public void UpdateMajorGiftProspectContacts(){


        // Update Contact Major_Gift_Prospect_Contact__c to True if appropriate Opportunity exists.
        // Conditions: Account.Name = 'Major Gift Opportunity' and DEPARTMENT_TYPE__C = 'Advancement' and DEPARTMENT_SUB_TYPE__C = 'Major Gifts'
        // Need to create this as a Bulk update.

        // Create Map to hold Contact records to update
        map< id, contact > contacts = new map< id, contact >();

        // Create a list of Contacts to update:
        for(List<OpportunityContactRole> ocr: [Select contact.id from OpportunityContactRole 
                                                WHERE Opportunity.DEPARTMENT_TYPE__C = 'Advancement' 
                                                and Opportunity.DEPARTMENT_Sub_Type__C = 'Major Gifts' 
                                                and Contact.Major_Gift_Prospect_Contact__c = false ])
            {

                // Add list of contacts to add to the Contact map
                contacts.put(ocr.id, new contact(id=ocr.id, Major_Gift_Prospect_Contact__c=true));

            }

            // Update Contact records
            update contacts.values(); 

        }
}

1 Answer 1

5

Your loop variable is a List<OpportunityContactRole> when I think what you actually want it to be is just an OpportunityContactRole so that you are iterating through each of the results from your SOQL query.

Change this line:

for(List<OpportunityContactRole> ocr: ... )

To:

for(OpportunityContactRole ocr: ...)
2
  • Thanks Koen and Alex that did the trick. Having said that, the reason I was trying to use the list in the for loop, I heard that: "The SOQL For Loop is good. In the for loop, it breaks records down into 200 records per operation." I was making this more robust if I happened to have a large number of records. Thanks! Commented Jan 6, 2015 at 20:47
  • Line of working code: contacts.put(ocr.contact.id, new contact(id=ocr.contact.id, Major_Gift_Prospect_Contact__c=true)); Commented Jan 6, 2015 at 21:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .