+ Start a Discussion
Ram ArzaRam Arza 

Error: Compile Error: Invalid field OwnerId for SObject User

I have trouble comparing old and new Account OwnerID. I see an error message saying  "Error: Compile Error: Invalid field OwnerId for SObject User at line 13 column 46". Below is my code. Please assist.
 
trigger ContactOwnerID on Account (After Insert, After Update) {

List<Account>AccList = new List<Account>();
Map<id,user>OldUserMap = new Map<id,user>();
Map<id,user>NewUserMap = new Map<id,user>();
List<Contact>ConList = new List<Contact>();
List<Contact>UpdatedConList = new List<Contact>();
List<Contact>FinalConList = new List<Contact>();



For(Account A:Trigger.new){
If(A.OwnerId != Null && OldUserMap.get(A.id).OwnerId != NewUserMap.get(A.id).OwnerId){
AccList.add(A);
}
If(!AccList.isEmpty()){
ConList =[Select id, OwnerId, AccountId from Contact where AccountId =: AccList];
}
}
If(!ConList.isEmpty()){
For(Contact c:UpdatedConList){
c.OwnerId = c.account.ownerid;
FinalConList.add(c);
}
}
If(!FinalConList.isEmpty()){
Update FinalConList;
}
}

 
Best Answer chosen by Ram Arza
rajat Maheshwari 6rajat Maheshwari 6

Hi Ram,

Hope it will helps you :)

trigger ContactOwnerID on Account (After Insert, After Update) {

List<Account>AccList = new List<Account>();
Map<id,user>OldUserMap = new Map<id,user>();
Map<id,user>NewUserMap = new Map<id,user>();
List<Contact>ConList = new List<Contact>();
List<Contact>UpdatedConList = new List<Contact>();
List<Contact>FinalConList = new List<Contact>();



For(Account A:Trigger.new){
If(A.OwnerId != Null && OldUserMap.get(A.ownerId).id!= NewUserMap.get(A.ownerId).id){
AccList.add(A);
}
If(!AccList.isEmpty()){
ConList =[Select id, OwnerId, AccountId from Contact where AccountId =: AccList];
}
}
If(!ConList.isEmpty()){
For(Contact c:UpdatedConList){
c.OwnerId = c.account.ownerid;
FinalConList.add(c);
}
}
If(!FinalConList.isEmpty()){
Update FinalConList;
}
}





Issue : - You were trying to use field (OwnerId) for sObject User, so I have change .please check and let me know the results :)

All Answers

venkat-Dvenkat-D
Check Contact sharing settings, if it is Controlled by parent you can not have owner id on child object. 
Ram ArzaRam Arza
There are none, I think it has to do something with declaring ownerid field. It is a user lookup and i think there is some other syntax  for declaring them.
rajat Maheshwari 6rajat Maheshwari 6

Hi Ram,

Hope it will helps you :)

trigger ContactOwnerID on Account (After Insert, After Update) {

List<Account>AccList = new List<Account>();
Map<id,user>OldUserMap = new Map<id,user>();
Map<id,user>NewUserMap = new Map<id,user>();
List<Contact>ConList = new List<Contact>();
List<Contact>UpdatedConList = new List<Contact>();
List<Contact>FinalConList = new List<Contact>();



For(Account A:Trigger.new){
If(A.OwnerId != Null && OldUserMap.get(A.ownerId).id!= NewUserMap.get(A.ownerId).id){
AccList.add(A);
}
If(!AccList.isEmpty()){
ConList =[Select id, OwnerId, AccountId from Contact where AccountId =: AccList];
}
}
If(!ConList.isEmpty()){
For(Contact c:UpdatedConList){
c.OwnerId = c.account.ownerid;
FinalConList.add(c);
}
}
If(!FinalConList.isEmpty()){
Update FinalConList;
}
}





Issue : - You were trying to use field (OwnerId) for sObject User, so I have change .please check and let me know the results :)
This was selected as the best answer
Ram ArzaRam Arza
It did help. Thanks mate!