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
raginireddy thoutireddyraginireddy thoutireddy 

Cross object reference trigger

Hello,
I am a salesforce admin and new to apex. We have reached cross object references in our org and would like to create a trigger. 
I have to pull the  First name and Last name of the user in Manager lookup field on Account using a trigger. Can anyone help me creating a trigger? 
Best Answer chosen by raginireddy thoutireddy
Glyn Anderson (Slalom)Glyn Anderson (Slalom)
Let me know if you have any questions.
trigger AccountManagerName on Account ( before insert, before update )
{
    Map<Id,List<Account>> accountsByManagerIds = new Map<Id,List<Account>>();
    for ( Account account : Trigger.new )
    {
        if ( !accountsByManagerIds.containsKey( account.Manager__c ) )
        {
            accountsByManagerIds.put( account.Manager__c, new List<Account>() );
        }
        accountsByManagerIds.get( account.Manager__c ).add( account );
    }

    for ( User user :
        [   SELECT  Id, FirstName, LastName
            FROM    User
            WHERE   Id IN :accountsByManagerIds.keySet()
        ]
        )
    {
        for ( Account account : accountsByManagerIds.get( user.Id ) )
        {
            account.Manager_FirstName__c = user.FirstName;
            account.Manager_LastName__c = user.LastName;
        }
    }
}

All Answers

Glyn Anderson (Slalom)Glyn Anderson (Slalom)
Let me know if you have any questions.
trigger AccountManagerName on Account ( before insert, before update )
{
    Map<Id,List<Account>> accountsByManagerIds = new Map<Id,List<Account>>();
    for ( Account account : Trigger.new )
    {
        if ( !accountsByManagerIds.containsKey( account.Manager__c ) )
        {
            accountsByManagerIds.put( account.Manager__c, new List<Account>() );
        }
        accountsByManagerIds.get( account.Manager__c ).add( account );
    }

    for ( User user :
        [   SELECT  Id, FirstName, LastName
            FROM    User
            WHERE   Id IN :accountsByManagerIds.keySet()
        ]
        )
    {
        for ( Account account : accountsByManagerIds.get( user.Id ) )
        {
            account.Manager_FirstName__c = user.FirstName;
            account.Manager_LastName__c = user.LastName;
        }
    }
}
This was selected as the best answer
raginireddy thoutireddyraginireddy thoutireddy
Thank you Glyn,

If we want to deploy this trigger to production we need to write a test class and can you please help me with that as well?
Glyn Anderson (Slalom)Glyn Anderson (Slalom)
@isTest
public class AccountManagerNameTest
{
    @isTest
    public static void testAccountManagerName()
    {
        User manager =
            [   SELECT  Id, FirstName, LastName
                FROM    User
                WHERE   Id = :UserInfo.getUserId()
            ];

        Account account = new Account
        (   Name = 'Test Account'
        ,   Manager__c = manager.Id
        );

        Test.startTest();
        insert account;
        Test.stopTest();

        Account result =
            [   SELECT  Id, Manager_FirstName__c, Manager_LastName__c
                FROM    Account
                WHERE   Id = :account.Id
            ];

        System.assertEquals
        (   manager.FirstName
        ,   result.Manager_FirstName__c
        ,   'FirstName did not match.'
        );
        System.assertEquals
        (   manager.LastName
        ,   result.Manager_LastName__c
        ,   'LastName did not match.'
        );
    }
}