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
DoondiDoondi 

How to assign a OwnerId to an user based on custom field value?

Hi,
My logic is: If (Manager__c == Null && region__c == 'South' 
 then assign the newly created record to South_Zonal_Manager__c

My requirement is something like this, can someone help me.

Twist here for me is I have 4 regions (east, west, north, south)  and I have 4 different zonal managers

 
Adilson Arcoverde JrAdilson Arcoverde Jr
Hi Doondi,

I guess the best approach is to use some kind of polymophirm. Please try this code:
Map<String,String> regionToOwnerMap = new Map<String,String>() {
        'South' => 'South_Zonal_Manager__c',
        'North' => 'North_Zonal_Manager__c',
        'East' => 'East_Zonal_Manager__c',
        'West' => 'West_Zonal_Manager__c'
    };
        
    If (myObj__c.Manager__c == Null && myObj__c.get( regionToOwnerMap.get( myObj__c.region__c ) ) != null ) {
        myObj__c.OwnerId = String.valueOf(myObj__c.get( regionToOwnerMap.get( myObj__c.region__c ) ))
    }

Let me know if I can help with anything else. You if find this solution useful, please mark as Best Answer to help others developers too.

Best regards.