You need to sign in to do that
Don't have an account?

how to reference user lookup field ID on account from contact in apex
from a variable myContact I'm trying to reference a user lookup field ID on that contact's account. The field name is Inside_Sales_Rep_Lookup__c.
I'm trying myContact.Account.Inside_Sales_Rep_Lookup__c.UserID.
I need help getting this right.
I'm trying myContact.Account.Inside_Sales_Rep_Lookup__c.UserID.
I need help getting this right.
Trigger.new will only return you the Id of the lookup object, you won't be able get second level from trigger.new. What my suggestion of your trigger will be, you need to perform a query to Account to get the Inside_Sales_Rep_Lookup__c value:
trigger Update_Owner on Contact (before insert, before update){
Set<Id> accIds = new Set<Id>();
for(Contact cont : Trigger.new){
accIds.add(cont.AccountId);
}
Map<Id, Account> accMap = new Map<Id, Account>();
for(Account acc: [Select Id, Inside_Sales_Rep_Lookup__c from Account Where Id in: accIds]){
accMap.put(acc.Id, acc);
}
for (Contact myContact : Trigger.new){
if (myContact.AccountID != null){
myContact.OwnerID = accMap.get(myContact.AccountId).Inside_Sales_Rep_Lookup__c;
}
}
}
Hope this help
All Answers
If my understand correctly the Inside_Sales_Rep_Lookup__c is the Lookup for User object in Account. So when you query it you don't need to specify the UserID. Maybe you can try this:
myContact.Account.Inside_Sales_Rep_Lookup__c
If you want to get the user name or other field from user object you need to use __r For example
myContact.Account.Inside_Sales_Rep_Lookup__r.Email
trigger Update_Owner on Contact (before insert, before update)
{
for (Contact myContact : Trigger.new)
{
if (myContact.AccountID != null)
{
myContact.OwnerID = myContact.Account.Inside_Sales_Rep_Lookup__c;
}
}
}
I'm just wanting the contact owner to be the user ID of the account.inside_Sales_Rep_Lookup. Currently, I'm getting the following error: Error:Apex trigger Update_Owner caused an unexpected exception, contact your administrator: Update_Owner: data changed by trigger for field Owner ID: owner cannot be blank
Trigger.new will only return you the Id of the lookup object, you won't be able get second level from trigger.new. What my suggestion of your trigger will be, you need to perform a query to Account to get the Inside_Sales_Rep_Lookup__c value:
trigger Update_Owner on Contact (before insert, before update){
Set<Id> accIds = new Set<Id>();
for(Contact cont : Trigger.new){
accIds.add(cont.AccountId);
}
Map<Id, Account> accMap = new Map<Id, Account>();
for(Account acc: [Select Id, Inside_Sales_Rep_Lookup__c from Account Where Id in: accIds]){
accMap.put(acc.Id, acc);
}
for (Contact myContact : Trigger.new){
if (myContact.AccountID != null){
myContact.OwnerID = accMap.get(myContact.AccountId).Inside_Sales_Rep_Lookup__c;
}
}
}
Hope this help