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
bodhibodhi 

FIELD_INTEGRITY_EXCEPTION, Opportunity: id value of incorrect type

 

if (myLead.IsConverted == true && myLead.IsConverted != oldLead.IsConverted) {
CampaignMember[] convertedCampaignMember = [SELECT CampaignMember.Id, CampaignMember.Opportunity__c 
FROM CampaignMember 
WHERE CampaignMember.Id = :myLead.ConvertedContactId
LIMIT 1];
            for (CampaignMember changeCampaignMember : convertedCampaignMember){
             changeCampaignMember.Opportunity__c = myLead.ConvertedContactId;
             update changeCampaignMember;
            }
            
Contact[] convertedContact = [SELECT Contact.Id, Contact.Opportunity__c 
FROM Contact
WHERE Contact.Id = :myLead.ConvertedContactId
LIMIT 1];
// if (!convertedContact.isEmpty()){
            for (Contact changeContact : convertedContact){
             changeContact.Opportunity__c = myLead.ConvertedContactId;
             update changeContact;
            }
// }
}

 

I am attempting to update a custom field in both CampaignMember and in Contact. I have checked and rechecked, and the fields are identical, i.e. both are lookups related to opportunity - one with a child relationship to contact, the other with a child relationship to CampaignMember. 

 

However, I am getting the "FIELD_INTEGRITY_EXCEPTION, Opportunity: id value of incorrect type" only on the "update changeContact"

and it appears to me that it should be the same type as the "update changeCampaignMember" 

 

What am I missing? Code follows:

 

if (myLead.IsConverted == true && myLead.IsConverted != oldLead.IsConverted) {

   CampaignMember[] convertedCampaignMember = [SELECT CampaignMember.Id, CampaignMember.Opportunity__c  FROM CampaignMember  WHERE CampaignMember.Id = :myLead.ConvertedContactId LIMIT 1];

for (CampaignMember changeCampaignMember : convertedCampaignMember){              changeCampaignMember.Opportunity__c = myLead.ConvertedContactId;            

  update changeCampaignMember;            }            

 

Contact[] convertedContact = [SELECT Contact.Id, Contact.Opportunity__c  FROM Contact WHERE Contact.Id = :myLead.ConvertedContactId LIMIT 1];

// if (!convertedContact.isEmpty()){            

for (Contact changeContact : convertedContact){            

  changeContact.Opportunity__c = myLead.ConvertedContactId;            

  update changeContact;            }
// } }

Best Answer chosen by Admin (Salesforce Developers) 
SuperfellSuperfell

If its a lookup to opportunity, then you can not pass in a contact Id for its value (myLead.convertedContactId)

All Answers

SuperfellSuperfell

This line looks suspect

changeContact.Opportunity__c = myLead.ConvertedContactId;      

 

If Opportunity__c is a lookup to opportunity, then you need to set it to an opportunityId, not a contactId.

bodhibodhi

I don't think I have that incorrect. In both cases, the "Custom Field Definition Detail" has "Related to Opportunity" for the Lookup options. The Child Relationship Name is what's different. One is Contacts, the other is CampaignMember. CampaignMember appears to be OK. Contacts does not.

 

Or do I really have them both set up incorrectly?

SuperfellSuperfell

If its a lookup to opportunity, then you can not pass in a contact Id for its value (myLead.convertedContactId)

This was selected as the best answer
bodhibodhi

yup - you are absolutely right - i had them BOTH wrong