You need to sign in to do that
Don't have an account?
Parker Edelmann
I have a simple trigger that is giving me an Invalid foreign key relationship error
Hello all,
On the Account Object, I have a field called Pipeline Rep (with an API name of Pipeline_Rep__c, obviously) that is a lookup to user. The trigger is supposed to update all related contact's owner to the Pipeline Rep when it is changed. Unfortunately, I'm given an "Invalid foreign key relationship: Account.Pipeline_Rep__c" on lines 8 and 10. Here's the trigger:
Thanks,
Parker
On the Account Object, I have a field called Pipeline Rep (with an API name of Pipeline_Rep__c, obviously) that is a lookup to user. The trigger is supposed to update all related contact's owner to the Pipeline Rep when it is changed. Unfortunately, I'm given an "Invalid foreign key relationship: Account.Pipeline_Rep__c" on lines 8 and 10. Here's the trigger:
trigger UpdateContacttoPipelineRep on Account (after update) { List<Contact> Contacts_to_Update = new List<Contact>(); for(Contact evaluatecontact: [SELECT Id FROM Contact WHERE AccountId IN :Trigger.New]) { if(evaluatecontact.Account.Pipeline_Rep__c.Trigger.old !=evaluatecontact.Account.Pipeline_Rep__c){ System.debug('The Pipeline Rep was changed'); evaluatecontact.owner.Id = evaluatecontact.Account.Pipeline_Rep__c.Id; Contacts_to_Update.add(evaluatecontact); } } update Contacts_to_Update; }I am new to apex, having only recently finished the Developer Beginner Trail, minus the VisualForce module. I know this can be done with Process Builder, but I thought I'd give it a shot programmatically in my Developer Edition. I'd greatly appreciate it if anyone could tell me what the Invalid foreign key relationship error means, and how to fix it.
Thanks,
Parker
Please find the latest code with comments, which will help you to understand the code easily.
In your code,
evaluatecontact.Account.Pipeline_Rep__c.Trigger.old
We can't write like this. If you want to compare current and old values then use below:
if(acc.Pipeline_Rep__c != null && acc.Pipeline_Rep__c != Trigger.oldMap.get(acc.Id).Pipeline_Rep__c)
Please do let me know if it helps you.
Regards,
Mahesh
All Answers
Please find the below code:
Apex class Best Practice :
https://developer.salesforce.com/page/Apex_Code_Best_Practices
Please do let me know if it helps you.
Regards,
Mahesh
- You have a lot of If statements in your code that almost seem superfluous, can you explain why they're necessary?
- Why did my original trigger get the Invalid foreign key relationship error?
- Lastly, what does it mean?
I appreciate your answer to my question, keep up the good work.Thanks again,
Parker
Please find the latest code with comments, which will help you to understand the code easily.
In your code,
evaluatecontact.Account.Pipeline_Rep__c.Trigger.old
We can't write like this. If you want to compare current and old values then use below:
if(acc.Pipeline_Rep__c != null && acc.Pipeline_Rep__c != Trigger.oldMap.get(acc.Id).Pipeline_Rep__c)
Please do let me know if it helps you.
Regards,
Mahesh
Best Regards,
Parker
Thanks,
Parker
Please find the latest Trigger:
Test Class:
I also tested the Test Class and code coverage is 100%.
Please do let me know if it helps you.
Regards,
Mahesh