+ Start a Discussion
Abraham kumar 4Abraham kumar 4 

Compile Error: Invalid foreign key relationship: Contact.Accounts__r

Hi All,

I am using below Trigger code to send notifications to external webservice
if(Trigger.isUpdate){
        contact old = Trigger.oldMap.get(c.Id);
        if (c.Email != old.Email||c.FirstName!=old.FirstName||c.LastName!=old.LastName||c.phone!=old.phone||c.Title__c!=old.Title__c||c.status__c!=old.status__c||c.AccountID!=old.AccountID||c.Accounts__r.Name!=old.Account__r.Name)
but my error is only in the last part  c.Accounts__r.Name!=old.Account__r.Name i want the trigger to fire when the account name is edited Like for Example when Account name "SFDC" is edited to "SFDC.COM" the related contacts of the account is also changed to SFDC.COM. i want the trigger to fire when this name change occurs in the account name field of contact object that is the reason for adding this  c.Accounts__r.Name!=old.Account__r.Name  but it is not working giving me Compile Error: Invalid foreign key relationship: Contact.Accounts__r

Please help me resolve this issue.

Thanks
Abraham
Muthuraj TMuthuraj T
Hi,
 
if(Trigger.isUpdate){
        contact old = Trigger.oldMap.get(c.Id);
        if (c.Email != old.Email||c.FirstName!=old.FirstName||c.LastName!=old.LastName||c.phone!=old.phone||c.Title__c!=old.Title__c||c.status__c!=old.status__c||c.AccountID!=old.AccountID||c.Account__r.Name!=old.Account__r.Name)

Try with the above code. Field should be Account__r, you have used like Accounts__r. This may be the issue. Please check the object api name.
Abraham kumar 4Abraham kumar 4
Hi Muthu,

Sorry for that.. It is Account__r But still im getting same error. 

Thanks
Abraham
Muthuraj TMuthuraj T
Try like this,
c.Account.Name!=old.Account.Name

 
Muthuraj TMuthuraj T
Hi Abraham,

Now, it will allow you to save. But it returns null. So you may need to query account.
Ashish DevAshish Dev
Also you might want to check equality in condition 
c.AccountID ==old.AccountID

instead of not equal.
Abraham kumar 4Abraham kumar 4
Hi Muthu,

This is getting saved but the when the account name is edited the trigger is not firing. Any idea y this is hapenning.


 
Abraham kumar 4Abraham kumar 4
Hi Ashish,

I want the trigger to fire when the account name is edited. It is working well when the account is changed ie AccountID is different but is not working when name of account is edited.ie) i want it to fire when name of account is edited the related contacts to that account get changed thats when it should fire

Many Thanks
Abraham
HARI KRISHNAHARI KRISHNA
Hi Abraham kumar,
Firstly modify your code like this c.AccountID ==old.AccountID. And use '&&' (and) operation in 'If condition'.So it will work.If you still have problem don't hesitate to discuss.
 
Abraham kumar 4Abraham kumar 4
Hi Hari,

Im not able to follow, can you please explain more briefly.
Many Thanks
Abraham
Abhishek BansalAbhishek Bansal
Hi Abraham,

Since Account is a lookup field so in trigger it will only be identified by its Id unless you fire a SOQL on it and fetch name from Account.
So i would suggest you to please update the code as below :
if(Trigger.isUpdate){
        contact old = Trigger.oldMap.get(c.Id);
        if (c.Email != old.Email||c.FirstName!=old.FirstName||c.LastName!=old.LastName||c.phone!=old.phone||c.Title__c!=old.Title__c||c.status__c!=old.status__c||c.AccountID!=old.AccountID)
It will cover both of your cases as chnaging the name of Account will automatically change its ID.

Please try with above code and let me know if you have any issue.

Thanks,
Abhishek
Abraham kumar 4Abraham kumar 4
Hi Abishek,

It Fires when the the account ID is changed, But editing the account name does not change the account id it changes the account name for all related contacts but does not changes the ID so its not firing.

thanks
Abraham
Abhishek BansalAbhishek Bansal
Have you made any custom field on Contact because if you are using standard Account field than it will atomatically chnage the id.
Please let me know whther you are using Custom field Account Name in your Contact oblject or not ?
If yes, than please share its API name.

Thanks,
Abhishek
Abraham kumar 4Abraham kumar 4
no Abhishek, in the account and contact both are standard names.
Abraham kumar 4Abraham kumar 4
Editing standard account name is not changing the Acount ID that is the problem
Abhishek BansalAbhishek Bansal
Hi Abraham,

It is not possible to provide you a solution right here.
It would be good if you can contact me on either gmail or Skype because what you are saying is looking strange to me, so we need to have a look at ur ORG.
Gmail : abhihske.bansal@metacube.com
Skype : abhishek.bansal2790

Thanks,
Abhishek.
Abraham kumar 4Abraham kumar 4
Thanks so much for your help Abhishek,

I have resolved this now by writing a trigger on the account object to trigger when account names are edited. As it is not able to be done from contact

many thanks
Abraham
HARI KRISHNAHARI KRISHNA
Hi Abraham,
Nice to here that.But next make sure your question should be more clear.Because there is no clarity on  which object you written trigger.
Abraham kumarAbraham kumar

Hi Hari,

Im Sorry the question was not clear to you i guess i have given the heading of this topic as "Compile Error: Invalid foreign key relationship: Contact.Accounts__r" which is clear that it is contact object. 

Thanks
Abraham