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()?

3 Answers 3

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
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');
3
  • Well, concatenation wont work as the combined length of two fields is greater than the size of Name field.
    – Yash Mehta
    Commented Jun 14, 2013 at 20:00
  • Did you read the rest of my answer ? Commented Jun 14, 2013 at 20:12
  • Yes I did, in my case. It will be adhoc and will be a few times per day on single record per transaction. So I think using SOQL should serve the purpose. Thanks for the response though!!
    – Yash Mehta
    Commented Jun 14, 2013 at 20:16
4

Maybe try to use a default setting formula for that:

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .