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
Patrick G. BrownPatrick 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?
 
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
     }
    }
}

 
jigarshahjigarshah
Issue is with line #8 within your code which is referring the Id field of the associated Hierarchy__c record incorrectly. Use the below code to replace line #8 within your code.
//Updated Correct Code
Hierarchy__c associatedHierarchy = hierarchyMap.get(ac.Hierarchy__c.Id);
Please do not forget to mark this thread as SOLVED and answer as the BEST ANSWER if this answer helps address your issue.
Patrick G. BrownPatrick G. Brown
jigarshah, that was actually a typo in my original post.  I'm very sorry.  Originally, I had:
 
Hierarchy__c associatedHierarchy = hierarchyMap.get(ac.hierarchy.Id);

as that line of code.  With your replacement, I'm still getting the same Invalid Foreign Key error.  Any other suggestions?

Best,

Patrick 


 
jigarshahjigarshah
Change line #4 to as below.
hierarchyIds.add(ac.Hierarchy__c);
If this does not work, you may need to change the before update event to after update. The reason being the relationship with associated Hierarchy__c records would possibly be established once the Agent Contract records are saved and committed to the database.

Hope that helps.
Malni Chandrasekaran 2Malni Chandrasekaran 2
Patrick,
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!