13

I have created a Custom Settings 'Tal_Source_Map__c' which has 4 fields.

  • SF_Source_Type__c
  • SF_Source_Sub_Type__c
  • Tal_Source_Type__c
  • Tal_Source_Type__c

Combination of SF_Source_Type__c and SF_Source_Sub_Type__c will have a unique set of Tal_Source_Type__c and Tal_Source_Type__c.

How can I get Tal_Source_Type__c and Tal_Source_Type__c values using SF_Source_Type__c and SF_Source_Sub_Type__c?

Is it possible to use getInstance()?

Mark Pond
  • 22,949
  • 2
  • 56
  • 103
Yash Mehta
  • 1,515
  • 4
  • 24
  • 42

3 Answers3

14

You can use SOQL to query Custom Settings just like an object. Use a SQOL query to find the right value:

select Tal_Source_Type__c, Tal_Source_Type__c from Tal_Sourc_Map__c 
where SF_Source_Type__c = :someval and SF_Source_Sub_Type__c = :otherval
Daniel Hoechst
  • 25,020
  • 7
  • 79
  • 114
6

Could you not simply populate the name identifier of the custom setting with a unique concatenation of SF_Source_Type__c and SF_Source_Sub_Type__c ? Ifso you could simply do:

Tal_Source_Map__c.getInstance(someCustomObject.Type__c + someCustomObject.Sub_Type__c); //whereever you're getting the values from

More elaborate:

Depending on the type of execution context and bulkified processing you are planning to use this custom setting in there are things to consider. While Daniel Hoechst's answer is entirely correct that you can use soql to query, if you need multiple records in a single execution context you'll need to do multiple queries or compose dynamic soql queries (or this, or this, or this etc) which also has its limits.

If the amount of records is not too high, and you'll need multiple you can also opt to load them all into the apex memory like this example:

public  class testCS_Provider {

        /*for custom setting testCS with fields: 
                        name:text, 
                        subIdentifier1:text,
                        subIdentifier2:text, 
                        decimal1:number,
                        decimal2:number
        dummy code which relies on your data integrity requiring unique combinations of subidentifier1 & 2
        in reality you will want to extend with some more if's, errorhandeling / fallback values to your own preference
       */
       private static map<string, map<string, test_CS__c>> internal_storage;

       public static test_CS__c getRecord(string subId_1, string subId_2){
                if(internal_storage == null)populateInternalStorage();
                return internal_storage.get(subId_1).get(subId_2); //error prone if the custom setting does not contain these parameters!
        }

        private static void populateInternalStorage(){
               internal_storage = new map<string,map<string,test_CS__c>>();
               Map<string, test_CS__c> allRecords = test_CS__c.getAll(); //get everything from the SFCD cache
               for(test_CS__c tcs:allRecords.values()){
                     if(! internal_storage.containsKey(tcs.subIdentifier1__c)){
                             internal_storage.put(tcs.subIdentifier1__c,new map<string,test_CS__c>());
                       }
                     internal_storage.get(tcs.subIdentifier1__c).put(tcs.subIdentifier2__c,tcs);
                 }
        }
 }

Anywhere else in apex:

test_cs__c x  = testCS_Provider.getRecord('test','test');
Samuel De Rycke
  • 9,550
  • 8
  • 45
  • 73
4

Maybe try to use a default setting formula for that:

enter image description here

Sergej Utko
  • 22,020
  • 11
  • 59
  • 88