You need to sign in to do that
Don't have an account?
Apex Trigger Help - Resolve a Foreign Key Issue Question
I am working on my first apex trigger. The business process of this trigger is when the Technical contact (Technical_Contact__c)is selected from contacts via a lookup and a button is clicked, the phone number is added in the appropriate field (Technical_Phone__c)
I am receiving the following error:
Error: Compile Error: Invalid foreign key relationship: Contact.Manufacturing_Design_Sheet__c at line 21 column 10
Code
// When a new line item is added to an opportunity, this trigger copies the value of the
// associated product's color to the new record.
trigger TechContactTrigger on Manufacturing_Design_Sheet__c (before insert) {
// For every Manufacturing Design Sheet record, add its associated contact entry
// to a set so there are no duplicates.
Set<Id> ContactIds = new Set<Id>();
for (Manufacturing_Design_Sheet__c oli : Trigger.new)
ContactIds.add(oli.Id);
// Query the Contacts for their associated Phone Number and place the results
// in a map.
Map<Id, Contact> entries = new Map<Id, Contact>(
[select contact.phone from contact
where id in :ContactIds]);
// Now use the map to set the appropriate Phone Number on every Manufacturing Design Sheet processed
// by the trigger.
for (Manufacturing_Design_Sheet__c oli : Trigger.new)
oli.Technical_Phone__c = entries.get(oli.Technical_Contact__c).Manufacturing_Design_Sheet__c.Technical_phone__c;
}
Thank you
ckellie,
Here is my code and comments.
// For every Manufacturing Design Sheet record, add its associated contact entry
// to a set so there are no duplicates.
Set<Id> ContactIds = new Set<Id>();// Changed ContactIds.add(oli.Id);
for (Manufacturing_Design_Sheet__c oli : Trigger.new) {ContactIds.add(oli.ContactId__c);
}
// Query the Contacts for their associated Phone Number and place the results
// in a map.
// Modified the map to separate out the Id and the Phone.
Map<Id, Contact> entries = new Map<Id, Contact>([select Id, Phone from contact
where id in :ContactIds]);// Now use the map to set the appropriate Phone Number on every Manufacturing Design Sheet processed
// by the trigger.
for (Manufacturing_Design_Sheet__c oli : Trigger.new) {
// Modified this to only get the Phone number of the related Contact Id in the Map above.
// Remember when using a variable in a get statement
// on a custom object, don't forget the __c. Have made this mistake myself a few times as well. :smileyhappy:oli.Technical_Phone__c = entries.get(oli.ContactId__c).Phone;
}
}
Should work. Let me know if you need anything more.