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
ErveErve 

Custom setting to assign Contact owner based on Product and State

Hi Everyone,

I Have a Custom setting with 3 Fields :
1.State 2. Products 3. UserID(to make Contact Owner)
I want to change the Contact Owner based on this custom setting values of State and Product.
For Ex : if State is NY and Product is Adidas  - Contact Owner should be User A 
If State is NY and Product is Nike - Contact Owner should be User B.

I have written below code but it is working only based on the State, what I want is to check the condition for Product as well along with State :

trigger ContactOwnerCustomSetting on Contact (before insert,before update) {
Map<string,ContactOwner__c> mpcons = ContactOwner__c.getAll();
for(Contact con:trigger.new){
ContactOwner__c co =ContactOwner__c.getValues(con.State__c);
con.ownerid = co.User__c;
}
}
Best Answer chosen by Erve
ayu sharma devayu sharma dev
Hello

It feels like the record you are testing do not have the satisfying data requirements in your custom setting. Make sure your custom setting has records for the Product, State and UserId for the records you are testing. 

I have updated the code. If any userId not found from the Map then default owner id will be assigned and also used lowercase key values in the map. 
 
trigger ContactOwnerCustomSetting on Contact (before insert,before update) {
    
    Map<String, Id> mapProdStateToUserId = new Map<String, Id>();
       
    ContactOwner__c co = ContactOwner__c.getInstance();
        
    for(ContactOwner__c co : ContactOwner__c.getall().values() ){
        
        mapProdStateToUserId.put( co.State__c.toLowerCase()+'_'+co.Product__c.toLowerCase(), co.User__c );
        
        system.debug('Map Values'+mapProdStateToUserId);
        
    }
    
    for(Contact con :Trigger.new){
        
        id co = mapProdStateToUserId.get(con.State__c.toLowerCase()+'_'+con.Products__c.toLowerCase()) ;
        if( co != null && co != '' ){
            con.OwnerID = co;
        }
    }    

}

Please try and let me know the results.

Ayush​​​​​​​

All Answers

ayu sharma devayu sharma dev
Hello Erve

I assume you are using List type Custom Setting as you are using getAll(). If this is the case and then using the below code can solve your problem.
 
Map<String, Id> mapProdStateToUserId = new Map<String, Id>();

for( ContactOwner__c co : ContactOwner__c.getall().values() ){
    mapProdStateToUserId.put( co.State+'_'+co.Product, co.User );
}

con.ownerid = mapProdStateToUserId.get( con.State+' '+con.Product );

I have created a unique key with "State+_+Product" so every con which matches this will have both Product and State of the Setting's User.

If problem still persists please let me know.

Thanks and Regards
Ayush
ErveErve
Hi Ayush,

Thanks for the response !! 

User-added image

I have modified the code as below but I am getting attached error :

trigger ContactOwnerCustomSetting on Contact (before insert,before update) {
    
Map<String, Id> mapProdStateToUserId = new Map<String, Id>();
   
    ContactOwner__c co = ContactOwner__c.getInstance();
    
for(ContactOwner__c co : ContactOwner__c.getall().values() ){
    
    mapProdStateToUserId.put( co.State__c+'_'+co.Product__c, co.User__c );
    
    system.debug('Map Values'+mapProdStateToUserId);
    
}

    for(Contact con :Trigger.new){
        
        id co = mapProdStateToUserId.get(con.State__c+'_'+con.Products__c) ;
        
        con.OwnerID = co;
        
        
    }    

}
ayu sharma devayu sharma dev
Hello

It feels like the record you are testing do not have the satisfying data requirements in your custom setting. Make sure your custom setting has records for the Product, State and UserId for the records you are testing. 

I have updated the code. If any userId not found from the Map then default owner id will be assigned and also used lowercase key values in the map. 
 
trigger ContactOwnerCustomSetting on Contact (before insert,before update) {
    
    Map<String, Id> mapProdStateToUserId = new Map<String, Id>();
       
    ContactOwner__c co = ContactOwner__c.getInstance();
        
    for(ContactOwner__c co : ContactOwner__c.getall().values() ){
        
        mapProdStateToUserId.put( co.State__c.toLowerCase()+'_'+co.Product__c.toLowerCase(), co.User__c );
        
        system.debug('Map Values'+mapProdStateToUserId);
        
    }
    
    for(Contact con :Trigger.new){
        
        id co = mapProdStateToUserId.get(con.State__c.toLowerCase()+'_'+con.Products__c.toLowerCase()) ;
        if( co != null && co != '' ){
            con.OwnerID = co;
        }
    }    

}

Please try and let me know the results.

Ayush​​​​​​​
This was selected as the best answer
ErveErve
Hi Ayush,

The data was there but it somehow did not trigger. I have used different contact then there was some string exception due to "co !=null" after removing this blank check I was able to run the code perfectly fine.

Thanks alot for you quick responses . 

Now there is only one thing which I require, code runs perfectly fine but is it bulkified . I mean can we achieve this with only one for loop or if we can change owner outside for loop?

Thanks,
Erve
 
ayu sharma devayu sharma dev
Erve

Don't worry your code is perfectly bulkified for now. As it is a before trigger we don't need to apply any DML just changing the values in Trigger.New will do the JOB for us. 
The owner must be assigned inside for loop on Trigger.New and using the map making it more efficient because we are not calling the Custom setting for each record separately instead we did it only once for a batch of records.

I hope it solves your problem. Glad to help you. Please close the thread if it solved your problem.

Thanks and Regards 
Ayush
ErveErve
Thanks for detailed answers. Highly Appreciated !!

Thanks,
Erve