+ Start a Discussion
Emily Petersen 13Emily Petersen 13 

I need to create an APEX code that updates the Account Owner based on the Contact ID of a name in another field that is not currently linked to a Contact ID

I need assistance writing APEX code to update the Account Owner based on the following:

Create workflow or process builder to change account owner once "commission end date" (Commission_End_Date__c) has passed. (I've already started the Process Builder, just need to add in the APEX code as the Action)
- If Account Strategist (Account_Strategist__c)(picklist field) is on an account, they are made the account owner. 
- If only a Media Strategist (Media_Strategist__c)(picklist field) is on an account, they are made the account owner. 
- If no Media Strategist or Account Strategist, Client Success (user) are made the account owner. 

I'm assuming I need to make some sort of map that has all of the names and their corresponding Contact IDs. I have the schematics in my mind, I just don't know how to follow through. Any help would be greatly appreciated!!

Best, 
Emily
Best Answer chosen by Emily Petersen 13
Ajay mishraAjay mishra
Hi Emily,

I have made some changes and tested in my Org. It is working well.

Please Replace the string "Name of Owner" with the Actual Names of Owner.
 
Public Class UpdatedAccountOwner
{
    @InvocableMethod
    Public Static void assignOwner(List<Account> accountLst)
    {
        Map<String,Account_Owner_Settings__c> mapAccountOwner = Account_Owner_Settings__c.getAll();
        
        for(Account accObj : accountLst)
        {
            if(accObj.Account_Strategist__c == 'Name of Owner')
            {
                accObj.OwnerId = mapAccountOwner.get('Name of Owner').Owner__c;    
            }  
            
            if(accObj.Account_Strategist__c == 'Name of Owner' )
            {
                accObj.OwnerId = mapAccountOwner.get('Name of Owner').Owner__c;
            }
            
            if(accObj.Account_Strategist__c == 'Name of Owner')
            {
                accObj.OwnerId = mapAccountOwner.get('Name of Owner').Owner__c;
            }
            
            if(accObj.Account_Strategist__c == 'Name of Owner')
            {
                accObj.OwnerId = mapAccountOwner.get('Name of Owner').Owner__c;
            }
            
            if(accObj.Media_Strategist__c == 'Name of Owner')
            {
                accObj.OwnerId = mapAccountOwner.get('Name of Owner').Owner__c;
            }
            
            if(accObj.Media_Strategist__c == 'Name of Owner')
            {
                accObj.OwnerId = mapAccountOwner.get('Name of Owner').Owner__c;
            }
            
            if(accObj.Media_Strategist__c == null & accObj.Account_Strategist__c == null)
            {
                accObj.OwnerId = mapAccountOwner.get('Name of Owner').Owner__c;
            }
        }        
    }
}

Please let me know if face any issue.

Thanks
Ajay
Email: mishraajay.m1@gmail.com
 

All Answers

Ajay mishraAjay mishra
Hi Emily,

If you create a Map and Put values in that map then you will not able to change them in production if any changes needed in future.

So, Please find some below Steps. In which you can overcome this limitation.
    
    1. First Create Custom Setting with Type List. Create Text field Owner
    2. Click on Manage in Custom Setting you created, and put your all the values in the Setting.
        Note: Keep in mind NAME is Key to get the Owner.
    3. Create Custom Labels for All the Owners using OwnerName1, OwnerName2...........OwnerName100. 100 is just for Example you can use as much you want. 
    
    
    
    /******* Now you are ready to get the values in your Owners ********/    
    
    /***** Get All Values of Custom Setting ******/
    Map<String,Account_Owner_Setting__c> mapCaseOwner = Account_Owner_Setting__c.getAll();
    
        
        for(Account accObj : /*List of Account Records*/)
        {
            if(accObj.Account_Strategist__c == label.OwnerName1)
            {
                accObj.OwnerId = mapCaseOwner.get(label.OwnerName1).Owner__c;    
            }  
            
            if(accObj.Account_Strategist__c == label.OwnerName2)
            {
                accObj.OwnerId = mapCaseOwner.get(label.OwnerName2).Owner__c;
            }

            if(accObj.Account_Strategist__c == label.OwnerName3)
            {
                accObj.OwnerId = mapCaseOwner.get(label.OwnerName3).Owner__c;
            }
            
            if(accObj.Account_Strategist__c == label.OwnerName4)
            {
                accObj.OwnerId = mapCaseOwner.get(label.OwnerName4).Owner__c;
            }
             
            if(accObj.Media_Strategist__c == label.OwnerName5)
            {
                accObj.OwnerId = mapCaseOwner.get(label.OwnerName5).Owner__c;
            }
             
            if(accObj.Media_Strategist__c == label.OwnerName6)
            {
                accObj.OwnerId = mapCaseOwner.get(label.OwnerName6).Owner__c;
            }
            
            if(accObj.Client_Success__c == label.OwnerName7)
            {
                accObj.OwnerId = mapCaseOwner.get(label.OwnerName7).Owner__c;
            }
        }        

Please let me know if you any question.

Thanks 
Ajay Mishra
Email: mishraajay.m1@gmail.com
Emily Petersen 13Emily Petersen 13
Thank you so much, Ajay!! I'm such a newbie that I'm not sure where to go from there. Do I load it in the Dev Console as a trigger? I built out a process because there are prior criterion that need to be addressed before this process takes place. So could I then just reference the APEX as the "action" in Process Builder?

Best,
Emily
Emily Petersen 13Emily Petersen 13
I'm getting "unexpected token: map". Do I need to enter something in the parenthesis? Account_Owner_Setting__c.getAll()

Do I need to enter something where you have *list of account records*?
07        for(Account accObj : /*List of Account Records*/) 

One more thing...Client Success is an actual User in the Account Owner field. So the logic is that if there's no Media Strategist and no Account Strategist, it will go to the Client Success user. I changed the code to this, will it work?

 if(accObj.Media_Strategist__c == null & accObj.Account_Strategist__c == null)
            {
                accObj.OwnerId = mapAccountOwner.get(label.OwnerName7).Owner__c;
            }
Ajay mishraAjay mishra
Hi Emily,

I have made some changes and tested in my Org. It is working well.

Please Replace the string "Name of Owner" with the Actual Names of Owner.
 
Public Class UpdatedAccountOwner
{
    @InvocableMethod
    Public Static void assignOwner(List<Account> accountLst)
    {
        Map<String,Account_Owner_Settings__c> mapAccountOwner = Account_Owner_Settings__c.getAll();
        
        for(Account accObj : accountLst)
        {
            if(accObj.Account_Strategist__c == 'Name of Owner')
            {
                accObj.OwnerId = mapAccountOwner.get('Name of Owner').Owner__c;    
            }  
            
            if(accObj.Account_Strategist__c == 'Name of Owner' )
            {
                accObj.OwnerId = mapAccountOwner.get('Name of Owner').Owner__c;
            }
            
            if(accObj.Account_Strategist__c == 'Name of Owner')
            {
                accObj.OwnerId = mapAccountOwner.get('Name of Owner').Owner__c;
            }
            
            if(accObj.Account_Strategist__c == 'Name of Owner')
            {
                accObj.OwnerId = mapAccountOwner.get('Name of Owner').Owner__c;
            }
            
            if(accObj.Media_Strategist__c == 'Name of Owner')
            {
                accObj.OwnerId = mapAccountOwner.get('Name of Owner').Owner__c;
            }
            
            if(accObj.Media_Strategist__c == 'Name of Owner')
            {
                accObj.OwnerId = mapAccountOwner.get('Name of Owner').Owner__c;
            }
            
            if(accObj.Media_Strategist__c == null & accObj.Account_Strategist__c == null)
            {
                accObj.OwnerId = mapAccountOwner.get('Name of Owner').Owner__c;
            }
        }        
    }
}

Please let me know if face any issue.

Thanks
Ajay
Email: mishraajay.m1@gmail.com
 
This was selected as the best answer