You need to sign in to do that
Don't have an account?

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
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
!$Setup.CustomSettingName__c.CustomFieldName__c.
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;
}