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
Ravi sastriRavi sastri 

Lead Owner should change when Account owner field on Lead object is changed

Hello Admins and Developers,
I've a requirement where Lead owner should change when Account owner is changed on lead object. Here, Account Owner field is a picklist type. When an option from account owner picklist is selected, the same value should be displayed on the lead owner field. I'm pretty much confused how to crack this requirement. Any suggestion with Workflow rule or Trigger will be highly helpful.

Thanks in advance
Sastri
Jainam ContractorJainam Contractor
Hi Ravi,

I have one question, will there be a user for all the values in the Account Owner Picklist with the same name. Becaue OwnerId is a lookup field with the User object. 

If there is a UserName with the same name as the Account Owner Picklist, then we can have the same as the Lead Owner with the help of APEX trigger.

Below is the Trigger for the Same:

trigger UpdateLeadOwner on Lead (before insert, before update) {
    List<String> AccOwnerPicklist =  new List<String>();
    for(Lead L : Trigger.New){
        if(!isBLANK(L.AccountName)){   //Change the AccountName field to your own org field name. Here i have considered AccountName as one.
            AccOwnerPicklist.add(L.AccountName);
        }
    }
    List<User> UserList = [select id, name from user where name IN :AccOwnerPicklist()];
    
    if(UserList != NULL && UserList.size()>0){
        for(Lead L : Trigger.New){
            for(User U : UserList){
                if(L.AccountName == U.Name){
                    L.OwnerId = U.Id;
                }
            }
        }
    }
}

Before implementing this, make sure the User's exist with the same name as the Account Owner Picklist values.

Please let me know if you need more assistance and if it solves you problem, mark it as the solution.

Thanks,
Jainam Contractor,
Salesforce Consultant,
Varasi LLC
www.varasi.com
Ravi sastriRavi sastri
Hi Jainam,
Thanks for your concern. I've tested your code in DE and it works like a gem. As we are implementing this is a production org, triggers are not enabled. Can you suggest how to achieve this through Workflow rule or  process builder.

Thanks in advance
Sastri
Jainam ContractorJainam Contractor
Hi Ravi,

Sorry to say but this won't be possible using Workflow/ Process builder as we are querying the User Details using SOQL based on the Picklist value and SOQL's are not possible in WF/ PB. I would suggest you to Develop a trigger in Sandbox and deploy the same in PROD and make it work.

Let me know if you need any help.

Thanks,
Jainam.
Ravi sastriRavi sastri
Hi Jainam,
Thanks for your concern. Can you please help me in writing test class for the above code which you've given. I'm new to salesforce and I'm finding it difficult to achieve 75% test coverage. 


Thank you in advance
Ravi Sastri