1

I want to use custom setting in direct soql statement, but it turns out not working. Because of the requirement, I can't add another field in that object.

The only thing I can do is to use Custom Setting as it might be changed frequently.

Error : System.QueryException: No such column 'defaultPost' on entity 

Coding:

MySetting__c mysetting = MySetting__c.getValues('DefaultPost');
Boolean defaultPost = mysetting.booleanValue__c;

String soql = 'SELECT myfield__c'
              + ', Name'
          + ' FROM my_object__c'
          + ' WHERE my_object__c.status__c = \'Open\''
              + ' AND (my_object__c.post__c = true OR defaultPost =:true)';

How can I put Custom Setting in the query which will allow me to change frequently?

I don't want to change the coding so many times for every changes. That is my main purpose.

4

You can't use a variable as a filter in SOQL, it must be a field on the object that you're comparing against.

Why don't you just wrap your filter in an IF statement instead?

MySetting__c mysetting = MySetting__c.getValues('DefaultPost');
Boolean defaultPost = mysetting.booleanValue__c;

// If your default setting is true, you want to get the reocrds regardless if my_object__c.post__c is true (according to your logic), so there is no need to filter
String soql = 'SELECT myfield__c'
              + ', Name'
          + ' FROM my_object__c'
          + ' WHERE my_object__c.status__c = \'Open\'';

if (defaultPost == false)
{
    // You only need to include records where my_object__c.post__c is true when the custom setting is false
    soql += ' AND my_object__c.post__c = true';
}
  • 1
    Thanks @Bachovski, I came up with idea same as you. For now, I can only use this approach. Will accept your answer after 3 minutes. – kitokid Mar 28 '14 at 3:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.