You need to sign in to do that
Don't have an account?
Patrick G. Brown
Receiving an Invalid Foreign Key Error with Custom Object
I have a pretty straightforward relationship between two custom Salesforce objects: Agent Contract (child) and Hierarchy (parent). There is a lookup field on the Agent Contract page that looks out to the Hierarchy object. What I'm trying to do is maintain a list of static values for certain fields in the Hierarchy table so that when a new Agent Contract is created or edited, and a user adds a lookup value in the Hiearchy_Name__c field (only if the field was previously blank) on the Agent Contract, the trigger will fire and will pull over some field values.
I am using a very similar trigger between Campaigns and Campaign Members and it works fine. However, this trigger is throwing an Invalid foreign key relationship: Agent_Contract__c.Hierarchy__c. Can anyone please tell me what I've done incorrectly?
I am using a very similar trigger between Campaigns and Campaign Members and it works fine. However, this trigger is throwing an Invalid foreign key relationship: Agent_Contract__c.Hierarchy__c. Can anyone please tell me what I've done incorrectly?
trigger agentContractHierarchy on Agent_Contract__c (before update) { Set<Id> hierarchyIds = new Set<Id>(); for (Agent_Contract__c ac: Trigger.new){ hierarchyIds.add(ac.Hierarchy__c.Id); } Map<Id, Hierarchy__c> hierarchyMap = new Map<Id, Hierarchy__c>([SELECT Id, SGA__c FROM Hierarchy__c WHERE Id IN :hierarchyIds]); for (Agent_Contract__c ac: Trigger.new) {Hierarchy__c associatedHierarchy = hierarchyMap.get(ac.hierarchyId); if(associatedHierarchy.Id==NULL) { //Do my thing } } }
as that line of code. With your replacement, I'm still getting the same Invalid Foreign Key error. Any other suggestions?
Best,
Patrick
Hope that helps.
In case of custom relationships, '__r' should be appended in order to access the parent records field information via child object records. please add __r wherever you refer the parent record from child record and let me know if ther error still exists.
Please note, Compaign and Compaign member are standard object which does not need __r notation.
Hope this helps!