+ Start a Discussion
SzymonSzymon 

Access list custom setting data

I need to access ALL data sets in a list custom setting rather than name a specific data set:
CustomSettingName__c mc = CustomSettingName__c.getValues(data_set_name);

Tried the below but then the query will return an error:
list <CustomSetting__c> mc = CustomSetting__c.getAll().values();

Database.getQueryLocator([select Id FROM Case WHERE Country__c LIKE :mc.Country_Code__c]);
ERROR: Variable does not exist: Country_Code__c

Can someone help me out please?
 
Best Answer chosen by Szymon
Waqar Hussain SFWaqar Hussain SF
You can either use IN operator like
list <CustomSetting__c> mc = CustomSetting__c.getAll().values(); 
set<string> countryNames = new set<string>();
for(CustomSetting__c cs : ms){
countryNames.add(cs.Country_Code__c);
}
Database.getQueryLocator([select Id FROM Case WHERE Country__c IN : countryNames]);


Or if you want to use like, you can put like 
list <CustomSetting__c> mc = CustomSetting__c.getAll().values();
string countryName = '%'+mc[0].Country_Code__c+'%';
Database.getQueryLocator([select Id FROM Case WHERE Country__c Like : countryName]);


 

All Answers

Waqar Hussain SFWaqar Hussain SF
You can either use IN operator like
list <CustomSetting__c> mc = CustomSetting__c.getAll().values(); 
set<string> countryNames = new set<string>();
for(CustomSetting__c cs : ms){
countryNames.add(cs.Country_Code__c);
}
Database.getQueryLocator([select Id FROM Case WHERE Country__c IN : countryNames]);


Or if you want to use like, you can put like 
list <CustomSetting__c> mc = CustomSetting__c.getAll().values();
string countryName = '%'+mc[0].Country_Code__c+'%';
Database.getQueryLocator([select Id FROM Case WHERE Country__c Like : countryName]);


 
This was selected as the best answer
HANK LI (Freelance)HANK LI (Freelance)
Hi Szymon,

Your code fails because mc is defined as a list, not CustomSettingName__c Object ifself. The variable Country_Code__c belongs to the Object, not the List, that's why the program cannot find it.

To fix the program, you can think the following steps:

1> Create a List of String which is used to collect all the Country_Code__c from your custom setting:

list<string> countryNames = new list<string>();

2> Do a for loop to add your values to the list:

for(CustomSetting__c  c: mc){
    countryNames.add(c.Country_Code__c);
}

3> Use the list with you query:
Database.getQueryLocator([select Id FROM Case WHERE Country__c IN : countryNames]);


Best regards,
Hank
SzymonSzymon
Thamks Waqar Hussain SF, I prefer using IN operator and I can have it work when executing anonymously in dev console but not quite sure how to add it to my class. Previously it looked like this:
global class BatchDeletion implements Database.Batchable<sobject>, Schedulable{   
    global BatchDeletion(){}
    
    CustomSetting__c mc = CustomSetting__c.getValues('US');

    global Database.QueryLocator start(Database.BatchableContext bc){
        return Database.getQueryLocator([select Id FROM Case WHERE Country__c LIKE :mc.Country_Code__c]);
    } 

    //Execute method for the Schedulable interface
    global void execute(SchedulableContext sc){   
        //execute the batch
        BatchDeletion deleteCS = new BatchDeletion();
        ID batchprocessid = Database.executeBatch(deleteCS);
    }

//Execute method for the batchable interface
    global void execute(Database.BatchableContext BC, list<sObject> scope){     
    
        List<Deleted_record__c> lstObj  = new List<Deleted_record__c>();
        for(sObject objCase : scope){
            Deleted_record__c obj = new Deleted_record__c();
            obj.Deleted_ID__c = String.valueOf(objCase.get('id'));
            obj.Object_Type_From_Apex__c = 'Case';
            lstObj.add(obj);
        }
        
        insert lstObj; 
        
        delete scope;   
        
        DataBase.emptyRecycleBin(scope); 
    }

    global void finish(Database.BatchableContext BC){}
}

Thanks!
Waqar Hussain SFWaqar Hussain SF
Hi Syzmon,

Since you are getting only one record from custom setting, so doesn't look logical to use IN operatore. 
See
global class BatchDeletion implements Database.Batchable<sobject>, Schedulable{   
    global BatchDeletion(){}
    

    global Database.QueryLocator start(Database.BatchableContext bc){
		CustomSetting__c mc = CustomSetting__c.getValues('US');
        return Database.getQueryLocator([select Id FROM Case WHERE Country__c = :mc.Country_Code__c]);
    } 

    //Execute method for the Schedulable interface
    global void execute(SchedulableContext sc){   
        //execute the batch
        BatchDeletion deleteCS = new BatchDeletion();
        ID batchprocessid = Database.executeBatch(deleteCS);
    }

//Execute method for the batchable interface
    global void execute(Database.BatchableContext BC, list<sObject> scope){     
    
        List<Deleted_record__c> lstObj  = new List<Deleted_record__c>();
        for(sObject objCase : scope){
            Deleted_record__c obj = new Deleted_record__c();
            obj.Deleted_ID__c = String.valueOf(objCase.get('id'));
            obj.Object_Type_From_Apex__c = 'Case';
            lstObj.add(obj);
        }
        
        insert lstObj; 
        
        delete scope;   
        
        DataBase.emptyRecycleBin(scope); 
    }

    global void finish(Database.BatchableContext BC){}
}

 
SzymonSzymon
Hi Waqar, the idea is to get all records that meet certain criteria, e.g. Country. Country is specified in a list custom setting as separate data sets e.g. US, CA, DE etc. So IN seems like the logical way to go and your code workes fine for me, so thank you. I managed to get add it to my class and it's working as expected.
Waqar Hussain SFWaqar Hussain SF
Great. Glad to hear.