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
Semira@gmail.comSemira@gmail.com 

System.SObjectException: Invalid field

Hi friends, 

I'm recieving this error message and don't know why it is an invalid field. Can someone please help me? 
//fetch ContactID from opportunity in a set<ID> ContactID

if(!ContactID.isempty())
		{
			CampMember = new list<CampaignMember>([Select Id, CampaignID From CampaignMember where contactid IN: ContactID]);
		}

CampaignMember CM = new CampaignMember();


if(opportunity.contact__c != null)
{
   CM = (CampaignMember)CampMember[0].get(opportunity.contact__c);
}

The last line CM assignment is giving me the error. Why is this an invalid field? 
 
Bhaswanthnaga vivek vutukuriBhaswanthnaga vivek vutukuri

Take different set name like

set<Id> contactIds;

if(contactIds.size()>0)

{
\\ query contactId field
       CampMember = new list<CampaignMember>([Select Id, CampaignID,contactId From CampaignMemberwhere contactid IN: ContactID]);
}

 

if(opportunity.contact__c != null)
{
      for(CampaignMember c:campMember)
     {

        CampaignMember CM = new CampaignMember();
        if(opportunity.contat__c == c.contactId){ CM = c;}


}

 

 

Semira@gmail.comSemira@gmail.com
This won't work because I have a for loop going for Opportunity before the if statement. I didn't post the entire code, but implementing you code it would be nested for loop, one for opportunity and another for campaign. I'm trying to avoid nested for loop for best practices. 

This class is written for a trigger. My trigger is calling the class and assigning a list of opportunity.