3

I am new to Apex triggers. I want to write a trigger to update account owner based on custom text field value which is similar to User Alias name ,if it matches then account owner changes to the user whose alias was matched.

So basically it matches the custom field to user alias then assign user id as account owner Id.

1

You need to write your trigger on Account object for the events BEFORE Insert/Update. I would suggest you to write you trigger code in a separate controller class since its the best practice while writing a trigger. Pass your List of Accounts to the controller class. Then save all the Alias Ids in a Set by running it over the Account List. Then get the list of users whose alias names are in the set.

After getting the list of users run over the loop of Account and then over the User List. Put a IF condition to check that if a User has same Alias as your Alias field in the Account then update the owner to that user. Since its a BEFORE trigger , you will not need to write any update statement. The Account will get updated with the User Id. Below is the Trigger and Class sample which you can use.

Trigger

    Trigger AccountTrigger on Account (Before Insert , Before Update) 
    {
        IF(Trigger.IsBefore)
        {
            IF(Trigger.IsInsert || Trigger.IsUpdate)        
            {
                AccountTriggerHandler.UpdateAccOwner(Trigger.New);
            }
        }
    }

Apex Class

Public Class AccountTriggerHandler
{
    Public Static Void UpdateAccOwner(List<Account> AccList)
    {
        Set<String> AliasNames = New Set<String>();

        FOR(Account A : AccList)
        {
            IF(A.User_Alias__c != NULL)
            {
                AliasNames.Add(A.User_Alias__c);
            }
        }

        List<User> UserList = [Select Id,Alias from USER where Alias =: AliasNames];

        FOR(Account A : AccList)
        {
            FOR(User U : UserList)
            {
                IF(A.User_Alias__c == U.Alias)
                {
                    A.OwnerId = U.Id;
                }
            }
        }
    }
}

Hope this helps you!

EDIT

Refer the below blogs on how to use MAPS. An advantage of using MAPS is tha tit will avoid you running into governor limits and will take lesser processing time.

http://blog.jeffdouglas.com/2011/01/06/fun-with-salesforce-collections/ https://www.sundoginteractive.com/blog/apex-why-maps-are-your-friend http://www.sfdc99.com/2014/01/25/use-maps-navigate-across-lists/

Regards..Ruchika

2
  • Would't be more optimal to Map<String,User> users = new Map<String,User> (); for(User U: [SELECT id,alias FROM User WHERE alias in: AliasNames]) { users.put(U.alias,U); } And then do a get? when you start working with large numbers of records this can get out of hand. – Alexander Aeons Torn Aug 23 '16 at 9:43
  • 1
    @AlexanderAeonsTorn : Yes you are correct. Maps are a more better way to go. But I thought since she is new then it would be more easier for her to first start off with Set. – Ruchi Aug 23 '16 at 9:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.