+ Start a Discussion
SFDC Coder 8SFDC Coder 8 

Access SOQL query from Custom Settings

Hi All,
I am new to Salesfroce.
I have written a batch apex to delete data from custom object.
Now, my requirement is to get that data from custom settings.

How to create that custom setting and what changes I have to do my below batch apex
global class batchDelete implements Database.Batchable<sobject>
{  
    
   
   global Database.QueryLocator start(Database.BatchableContext BC){
      
     return Database.getQueryLocator([SELECT Id,CreatedDate FROM Obj1__c WHERE createdDate = yesterday]);     
   }
   
global void execute(Database.BatchableContext BC,List<Obj1__c> rec){     
  //some code        
   
  delete rec;           

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

}

Please help me
Thanks in Advance!!
Rowallim TechnologyRowallim Technology
Hi SFDC Coder 8
You csn simply restore your daata from Recyclebin.
Thanks
SFDC Coder 8SFDC Coder 8
Hi Rowallim,
Thank you for your reply

Please explain little bit, How to do?
 
Rowallim TechnologyRowallim Technology
Hii SFDC Coder 8
 click on Home -> Recycle bin -> Select your data which you want to restore -> Undelete
Thanks
SFDC Coder 8SFDC Coder 8
Hi Roqallim,
I do not want this.
You can see SOQL query in my code. (Where CreatedDate=Yesterday)
There in where condition instead of Yesterday, I want to get that data from Custom settings .
What change I need to there
Rowallim TechnologyRowallim Technology
Hii SFDC Coder 8
whatever you have deleted ether using SOQL query that goes to Recycle Bin. You can get that in Recycle Bin.
 
V V Satyanarayana MaddipatiV V Satyanarayana Maddipati
Hi, 

I am assuming that your requirement is to make createdDate field criteria as configurable in the SOQL query like (Yesterday, Today, Last_week etc) . For this create a custom setting and  a custom field to store the value. Once Custom setting is created.

Click on Manage button available in the custom setting detail screen, there you can create records, Here Name is unique field and the custom field which you created is to store the value.

In the code you have to get the value w.r.t Name  by using the custom setting methods. Follow this link (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_custom_settings.htm%23apex_System_ListCustomSetting_instance_methods).

for eg : Assume your custom setting api name is :  Foundation_Countries__c and the custom field api name is Criteria__c .
and you have created a custom setting record with Name field populated with  "DateCriteria"

To get that particular record values follow below syntax:
Access Custom setting in Batch class :
Foundation_Countries__c myCS1 = Foundation_Countries__c.getValues('DateCriteria');
String criteriaVal = myCS1.Criteria__c;

Change you query in batch class as below : 
return Database.getQueryLocator('SELECT Id,CreatedDate FROM Obj1__c WHERE createdDate ='+ criteriaVal); 
Hop this will help.

Thanks
Satya.