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
Mike DeMilleMike DeMille 

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. 
Best Answer chosen by Mike DeMille
Kai Herng LauKai Herng Lau
Hi Mike,

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

Kai Herng LauKai Herng Lau
Hi Mike,

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
 
Mike DeMilleMike DeMille
Yes, I believe you are correct.  Can you help with my trigger?  

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
Kai Herng LauKai Herng Lau
Hi Mike,

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
This was selected as the best answer
Mike DeMilleMike DeMille
Thank you for your help!