+ Start a Discussion
Jay16Jay16 

Update Opportunity field using Custom Settings

Hi everyone, 

I would appreciate some guidence on how to achieve the following if possible: 

I have an opporutnity with 2 fields, region and division. 

I then have some records in Custom Settings, which contain the fields - region, division, sales leader name, sales leader id, 

an example would be - EMEA, IT, John Nix, 069D0000001KMqh

I'm trying to write a before insert trigger that takes the region and division from the opportunity, then looks at Sales_Leaders__c in Custom Settings and returns the sales leader id that is associated to that region and division (there will only be one). 

So far I've been looking at maps, the custom settings methods and SOQL queries but nothing seems to work. Here's an example: 

for (Opportunity newOpp : Trigger.new) {
           
  String div = newOpp.Opporuntity_Lead_Region__c;
  String reg = newOpp.Opportunity_Division__c;
  Id SL1 = newOpp.Sales_Leader_1__c;
           
          
         map <id,Sales_Leader_List__c> SL_Map = new map<id,Sales_Leader_List__c>();
         for (Sales_Leader_List__c sl:[select user_id__c from Sales_Leader_List__c where region__c = :reg AND division__C = :div])
         SL_Map.put(sl.user_id__c, sl);

a) this returns null
b) i have a SOQL query nested in a loop (trying to bulkify where possible)

Can anyone shed some light on this for me? I'm almost a complete noob to SF dev and have tried to figure this one out without leaning on the dev community but I'm spending way too much time on this now so could use a hand. If it can be achived using the Custom Settings methods that would be super awesome too.

Thanks in advance 

 

 

Best Answer chosen by Jay16
Jay16Jay16
This ended up being more straight forward than I thought, here's a solution if anyone needs it (unbulkified):

for (Opportunity newOpp : Trigger.new) {
           
             String reg = newOpp.Opporuntity_Lead_Region__c;
    String div = newOpp.Opportunity_Division__c;
           
              String c = [select user_id__c from Sales_Leader_List__c where region__c = :reg AND division__C = :div].user_id__c;
              if (c != null){
               newOpp.Sales_Leader_1__c = c;
              }

All Answers

Jim JamJim Jam
Best thing to do would be create your custom setting as a hierarchical one and then access the values using a formula field. The general sysntax for accessing the custom setting value would be .. 

!$Setup.CustomSettingName__c.CustomFieldName__c.
Jay16Jay16
Hi, thanks for the response - This was my first hope, however it's not possible to pre populate a lookup field without code. It would also mean I have to assign the value based on User or User Profile. I need to assign the value based on opportunity region and division, which is why I have opted for List instead. 
Jay16Jay16
This ended up being more straight forward than I thought, here's a solution if anyone needs it (unbulkified):

for (Opportunity newOpp : Trigger.new) {
           
             String reg = newOpp.Opporuntity_Lead_Region__c;
    String div = newOpp.Opportunity_Division__c;
           
              String c = [select user_id__c from Sales_Leader_List__c where region__c = :reg AND division__C = :div].user_id__c;
              if (c != null){
               newOpp.Sales_Leader_1__c = c;
              }
This was selected as the best answer