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

Invalid Foreign Key Relationship Error



I am writing a trigger on account object. Whenever a flag is updated in accounts, trigger updates the corresponding Owner in the User object. Follwing is the code:


trigger ServiceBreachTrigger on Account (after update) {
	for(Account a:{
		if(a.service.breach__c == true){
			user u = [Select Id,times_service_breached__c from User where Id = :a.OwnerId LIMIT 1];
			a.Service_Breach__c = false;
			if(u.times_service_breached__c == 0){
				u.times_service_breached__c = 1;
			update a;
			update u;

 I am getting referential integrity error which specifically states:


Invalid Foreign Key Relationship: Account .Service at line (a.Service_Breach__c = false;)


Any help?



Best Answer chosen by Admin (Salesforce Developers) 
Shashikant SharmaShashikant Sharma



As I am not aware of your object model but one thing that I can see in your code

in IF condition you are using

if(a.service.breach__c == true)   // you are using . (dot) means it is a relationship, I think you just mistyped it and it is a custom field on account object so please do not use . in between a field name


At other place just below it you are using

a.Service_Breach__c that means it is acustom field on the object,


About the error :  This is a compile time error when ever compiler sees any . (dot) after an object instance it looks for a field or relationship. If object insatance has another . that means it is a relationship and compiler finds a relationship with that name. if it do not find any then it shows this message  : Error: Compile Error: Invalid foreign key relationship: